Skip to content

Commit

Permalink
settings are ordered in export
Browse files Browse the repository at this point in the history
  • Loading branch information
CarstenHollmann committed Jan 17, 2022
1 parent a3b79ce commit 100eff9
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toSet;

import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
Expand Down Expand Up @@ -46,6 +47,8 @@

public class JsonSettingsEncoder {
private final JsonNodeFactory nodeFactory = Json.nodeFactory();
private final Comparator<Entry<SettingDefinition<?>, SettingValue<?>>> comparator =
new SettingDefinitionComparator();

public Map<SettingDefinitionGroup, Set<SettingDefinition<?>>> sortByGroup(Set<SettingDefinition<?>> defs) {
return defs.stream().collect(groupingBy(SettingDefinition::getGroup, toSet()));
Expand Down Expand Up @@ -161,11 +164,21 @@ private void encodeValue(ObjectNode o, Entry<SettingDefinition<?>, SettingValue<

public JsonNode encodeValues(Map<SettingDefinition<?>, SettingValue<?>> settings) {
return settings.entrySet().stream()
.sorted(Entry.comparingByKey())
.sorted(comparator)
.collect(nodeFactory::objectNode, this::encodeValue, ObjectNode::setAll);
}

private TextNode textNode(Object value) {
return nodeFactory.textNode(String.valueOf(value));
}

private static class SettingDefinitionComparator implements Comparator<Entry<SettingDefinition<?>, SettingValue<?>>> {

@Override
public int compare(Entry<SettingDefinition<?>, SettingValue<?>> o1,
Entry<SettingDefinition<?>, SettingValue<?>> o2) {
return o1.getKey().getKey().compareTo(o2.getKey().getKey());
}

}
}

0 comments on commit 100eff9

Please sign in to comment.