Automatically sort YAML/JSON storage - closes #113

This commit is contained in:
Luck 2017-01-08 11:02:13 +00:00
parent a23f995717
commit 773bfe1407
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 10 additions and 6 deletions

View File

@ -40,7 +40,7 @@ import java.util.Locale;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PriorityComparator implements Comparator<LocalizedNode> {
private static final PriorityComparator INSTANCE = new PriorityComparator();
public static Comparator<LocalizedNode> get() {
public static PriorityComparator get() {
return INSTANCE;
}
public static Comparator<LocalizedNode> reverse() {
@ -97,14 +97,17 @@ public class PriorityComparator implements Comparator<LocalizedNode> {
return o1.getWildcardLevel() > o2.getWildcardLevel() ? 1 : -1;
}
return compareStrings(o1.getPermission(), o2.getPermission()) == 1 ? -1 : 1;
}
public int compareStrings(String o1, String o2) {
try {
CollationKey o1c = collationKeyCache.get(o1.getPermission());
CollationKey o2c = collationKeyCache.get(o2.getPermission());
return o1c.compareTo(o2c) == 1 ? -1 : 1;
CollationKey o1c = collationKeyCache.get(o1);
CollationKey o2c = collationKeyCache.get(o2);
return o1c.compareTo(o2c) == 1 ? 1 : -1;
} catch (Exception e) {
// ignored
}
return 1;
}
}

View File

@ -75,6 +75,7 @@ import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
@ -1166,7 +1167,7 @@ public abstract class PermissionHolder {
}
public static Map<String, Boolean> exportToLegacy(Set<Node> nodes) {
Map<String, Boolean> m = new HashMap<>();
Map<String, Boolean> m = new TreeMap<>((o1, o2) -> PriorityComparator.get().compareStrings(o1, o2));
for (Node node : nodes) {
m.put(node.toSerializedNode(), node.getValue());
}