Skip to content

Commit

Permalink
Remove a stream usage in PropertyOrdering, rework ordering strats
Browse files Browse the repository at this point in the history
Signed-off-by: Gyúróczki Gergő <gergonoorbi@gmail.com>
  • Loading branch information
Degubi committed Sep 8, 2019
1 parent 1dd96ea commit a203a9d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.function.Consumer;

/**
* Resolved properties from JSONB config.
Expand Down Expand Up @@ -156,7 +155,7 @@ private String getGlobalConfigJsonbDateFormat() {
}).orElse(JsonbDateFormat.DEFAULT_FORMAT);
}

private Function<Collection<PropertyModel>, List<PropertyModel>> initOrderStrategy() {
private Consumer<List<PropertyModel>> initOrderStrategy() {
Optional<String> strategy = getPropertyOrderStrategy();

return strategy.map(StrategiesProvider::getOrderingFunction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
*/
public class PropertyOrdering {

private final Function<Collection<PropertyModel>, List<PropertyModel>> propertyOrderStrategy;
private final Consumer<List<PropertyModel>> propertyOrderStrategy;

/**
* Creates a new instance.
*
* @param propertyOrderStrategy Property order strategy. Must be not null.
*/
public PropertyOrdering(Function<Collection<PropertyModel>, List<PropertyModel>> propertyOrderStrategy) {
public PropertyOrdering(Consumer<List<PropertyModel>> propertyOrderStrategy) {
this.propertyOrderStrategy = Objects.requireNonNull(propertyOrderStrategy);
}

Expand All @@ -49,7 +49,7 @@ public PropertyOrdering(Function<Collection<PropertyModel>, List<PropertyModel>>
*/
public List<PropertyModel> orderProperties(List<PropertyModel> properties, ClassModel classModel) {
Map<String, PropertyModel> byReadName = new HashMap<>();
properties.stream().forEach(propertyModel -> byReadName.put(propertyModel.getPropertyName(), propertyModel));
properties.forEach(propertyModel -> byReadName.put(propertyModel.getPropertyName(), propertyModel));

String[] order = classModel.getClassCustomization().getPropertyOrder();
List<PropertyModel> sortedProperties = new ArrayList<>();
Expand All @@ -63,7 +63,9 @@ public List<PropertyModel> orderProperties(List<PropertyModel> properties, Class
}
}

sortedProperties.addAll(propertyOrderStrategy.apply(byReadName.values()));
List<PropertyModel> readNamesToSort = new ArrayList<>(byReadName.values());
propertyOrderStrategy.accept(readNamesToSort);
sortedProperties.addAll(readNamesToSort);
return sortedProperties;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
******************************************************************************/
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.*;
Expand All @@ -24,22 +23,22 @@
import org.eclipse.yasson.internal.properties.MessageKeys;

import java.nio.CharBuffer;
import java.util.function.Function;
import java.util.function.Consumer;
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){
public static Consumer<List<PropertyModel>> getOrderingFunction(String strategy){
switch(strategy) {
case LEXICOGRAPHICAL:
return createSortingOrdererFunction(comparing(PropertyModel::getWriteName));
return props -> props.sort(comparing(PropertyModel::getWriteName));
case ANY:
return ArrayList::new;
return props -> {};
case REVERSE:
return createSortingOrdererFunction(comparing(PropertyModel::getWriteName).reversed());
return props -> props.sort(comparing(PropertyModel::getWriteName).reversed());
default:
throw new JsonbException(Messages.getMessage(MessageKeys.PROPERTY_ORDER, strategy));
}
Expand All @@ -65,10 +64,6 @@ public static PropertyNamingStrategy getPropertyNamingStrategy(String 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);
Expand Down

0 comments on commit a203a9d

Please sign in to comment.