-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor strategies to functions instead of using inheritance (#311)
* Refactor strategies to functions instead of using inheritance Signed-off-by: Gyúróczki Gergő <gergonoorbi@gmail.com> * Fix & refactor some stuff Signed-off-by: Gyúróczki Gergő <gergonoorbi@gmail.com> * Yiked optional method Signed-off-by: Gyúróczki Gergő <gergonoorbi@gmail.com> * Reduce diff size Signed-off-by: Degubi <gergonoorbi@gmail.com> * Remove unnecessary propertyName.length() from .substring call Signed-off-by: Gyúróczki Gergő <gergonoorbi@gmail.com> * Add basic benchmark for serializing a pojo with 10 fields Signed-off-by: Gyúróczki Gergő <gergonoorbi@gmail.com> * Fix wrong class name after renaming Signed-off-by: Gyúróczki Gergő <gergonoorbi@gmail.com>
- Loading branch information
Showing
19 changed files
with
255 additions
and
523 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/main/java/org/eclipse/yasson/internal/model/customization/StrategiesProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016, 2018 Oracle and/or its affiliates. All rights reserved. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 | ||
* which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* Contributors: | ||
* Gyúróczki Gergő | ||
******************************************************************************/ | ||
package org.eclipse.yasson.internal.model.customization; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static java.util.Comparator.comparing; | ||
import static javax.json.bind.config.PropertyNamingStrategy.*; | ||
import static javax.json.bind.config.PropertyOrderStrategy.*; | ||
|
||
import javax.json.bind.JsonbException; | ||
import javax.json.bind.config.PropertyNamingStrategy; | ||
import org.eclipse.yasson.internal.model.PropertyModel; | ||
import org.eclipse.yasson.internal.properties.Messages; | ||
import org.eclipse.yasson.internal.properties.MessageKeys; | ||
|
||
import java.nio.CharBuffer; | ||
import java.util.function.Function; | ||
import java.util.*; | ||
|
||
public final class StrategiesProvider { | ||
private StrategiesProvider() {} | ||
|
||
public static final PropertyNamingStrategy CASE_INSENSITIVE_STRATEGY = Objects::requireNonNull; | ||
|
||
public static Function<Collection<PropertyModel>, List<PropertyModel>> getOrderingFunction(String strategy){ | ||
switch(strategy) { | ||
case LEXICOGRAPHICAL: | ||
return createSortingOrdererFunction(comparing(PropertyModel::getWriteName)); | ||
case ANY: | ||
return ArrayList::new; | ||
case REVERSE: | ||
return createSortingOrdererFunction(comparing(PropertyModel::getWriteName).reversed()); | ||
default: | ||
throw new JsonbException(Messages.getMessage(MessageKeys.PROPERTY_ORDER, strategy)); | ||
} | ||
} | ||
|
||
public static PropertyNamingStrategy getPropertyNamingStrategy(String strategy) { | ||
switch(strategy) { | ||
case LOWER_CASE_WITH_UNDERSCORES: | ||
return createLowerCaseStrategyWithSeparator('_'); | ||
case LOWER_CASE_WITH_DASHES: | ||
return createLowerCaseStrategyWithSeparator('-'); | ||
case UPPER_CAMEL_CASE: | ||
return createUpperCamelCaseStrategy(); | ||
case UPPER_CAMEL_CASE_WITH_SPACES: | ||
return createUpperCamelCaseWithSpaceStrategy(); | ||
case IDENTITY: | ||
return Objects::requireNonNull; | ||
case CASE_INSENSITIVE: | ||
return CASE_INSENSITIVE_STRATEGY; | ||
default: | ||
throw new JsonbException("No property naming strategy was found for: " + strategy); | ||
} | ||
} | ||
|
||
|
||
private static Function<Collection<PropertyModel>, List<PropertyModel>> createSortingOrdererFunction(Comparator<PropertyModel> comparator){ | ||
return props -> props.stream().sorted(comparator).collect(toList()); | ||
} | ||
|
||
private static PropertyNamingStrategy createUpperCamelCaseStrategy() { | ||
return propertyName -> { | ||
Objects.requireNonNull(propertyName); | ||
char first = Character.toUpperCase(propertyName.charAt(0)); | ||
|
||
return first + propertyName.substring(1); | ||
}; | ||
} | ||
|
||
private static PropertyNamingStrategy createUpperCamelCaseWithSpaceStrategy() { | ||
return propertyName -> { | ||
String upperCased = createUpperCamelCaseStrategy().translateName(propertyName); | ||
CharBuffer buffer = CharBuffer.allocate(upperCased.length() * 2); | ||
char last = Character.MIN_VALUE; | ||
|
||
for(int i = 0; i < upperCased.length(); ++i) { | ||
char current = upperCased.charAt(i); | ||
|
||
if (i > 0 && Character.isUpperCase(current) && isLowerCaseCharacter(last)) { | ||
buffer.append(' '); | ||
} | ||
last = current; | ||
buffer.append(current); | ||
} | ||
return new String(buffer.array(), 0, buffer.position()); | ||
}; | ||
} | ||
|
||
private static PropertyNamingStrategy createLowerCaseStrategyWithSeparator(char separator) { | ||
return propertyName -> { | ||
Objects.requireNonNull(propertyName); | ||
CharBuffer charBuffer = CharBuffer.allocate(propertyName.length() * 2); | ||
char last = Character.MIN_VALUE; | ||
|
||
for(int i = 0; i < propertyName.length(); ++i) { | ||
char current = propertyName.charAt(i); | ||
|
||
if (i > 0 && Character.isUpperCase(current) && isLowerCaseCharacter(last)) { | ||
charBuffer.append(separator); | ||
} | ||
last = current; | ||
charBuffer.append(Character.toLowerCase(current)); | ||
} | ||
return new String(charBuffer.array(), 0, charBuffer.position()); | ||
}; | ||
} | ||
|
||
private static boolean isLowerCaseCharacter(char character) { | ||
return Character.isAlphabetic(character) && Character.isLowerCase(character); | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
.../java/org/eclipse/yasson/internal/model/customization/naming/CaseInsensitiveStrategy.java
This file was deleted.
Oops, something went wrong.
53 changes: 0 additions & 53 deletions
53
.../java/org/eclipse/yasson/internal/model/customization/naming/DefaultNamingStrategies.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.