Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Vosburgh2015-12-23 12:59:48 -0500
committerBrian Vosburgh2015-12-23 12:59:48 -0500
commit1d6126f4b364a7e6529a79c131f624a6e38805a0 (patch)
tree67d6ea2897f04f43e70a91e8551908f6b8cc736e
parent4427a68f59f21dcb8722f992e6c52bbdb101fd3c (diff)
downloadwebtools.dali-1d6126f4b364a7e6529a79c131f624a6e38805a0.tar.gz
webtools.dali-1d6126f4b364a7e6529a79c131f624a6e38805a0.tar.xz
webtools.dali-1d6126f4b364a7e6529a79c131f624a6e38805a0.zip
sort JSON produced by StringBuilderTools.appendJSON(StringBuilder, Map)
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/StringBuilderTools.java45
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/StringBuilderToolsTests.java16
2 files changed, 21 insertions, 40 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/StringBuilderTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/StringBuilderTools.java
index 7eaee51c74..51144059bc 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/StringBuilderTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/StringBuilderTools.java
@@ -12,6 +12,8 @@ package org.eclipse.jpt.common.utility.internal;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
import org.eclipse.jpt.common.utility.internal.comparator.ComparatorAdapter;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
@@ -2083,40 +2085,29 @@ public final class StringBuilderTools {
/**
* Assume non-<code>null</code> map.
+ * Sort output by key.
*/
private static void appendJSON_(StringBuilder sb, Map<?, ?> map) {
- int resetIndex = sb.length();
- boolean reset = false;
+ Set<?> keys = map.keySet();
+ for (Object key : keys) {
+ if ( ! (key instanceof String)) {
+ appendJSON__(sb, map); // object reflection
+ return;
+ }
+ }
sb.append('{');
- Iterator<?> stream = map.entrySet().iterator();
- if (stream.hasNext()) {
- do {
- Map.Entry<?, ?> entry = (Map.Entry<?, ?>) stream.next();
- Object key = entry.getKey();
- if (key instanceof String) {
- appendJSON(sb, (String) key);
- }
- else if (key instanceof char[]) { // unlikely (as not very useful)
- appendJSON(sb, (char[]) key);
- }
- else {
- sb.setLength(resetIndex);
- reset = true;
- break;
- }
+ if ( ! keys.isEmpty()) {
+ @SuppressWarnings("unchecked")
+ TreeSet<String> sortedKeys = (TreeSet<String>) new TreeSet<>(map.keySet());
+ for (String key : sortedKeys) {
+ appendJSON(sb, key);
sb.append(':');
- appendJSON(sb, entry.getValue());
+ appendJSON(sb, map.get(key));
sb.append(',');
- } while (stream.hasNext());
- if ( ! reset) {
- sb.setLength(sb.length() - 1); // strip off extra comma
}
+ sb.setLength(sb.length() - 1); // strip off extra comma
}
- if (reset) {
- appendJSON__(sb, map); // object reflection
- } else {
- sb.append('}');
- }
+ sb.append('}');
}
/**
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/StringBuilderToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/StringBuilderToolsTests.java
index a729a8bc1a..276a50e405 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/StringBuilderToolsTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/StringBuilderToolsTests.java
@@ -448,24 +448,14 @@ public class StringBuilderToolsTests
assertEquals("{\"left\":\"name\",\"right\":\"Fred\"}", sb.toString());
}
- public void testAppendJSONMap_charArray() throws Exception {
- StringBuilder sb = new StringBuilder();
- Map<char[], String> map = new HashMap<>();
- map.put("left".toCharArray(), "name");
- map.put("right".toCharArray(), "Fred");
- StringBuilderTools.appendJSON(sb, map);
- assertEquals("{\"left\":\"name\",\"right\":\"Fred\"}", sb.toString());
- sb = new StringBuilder();
- StringBuilderTools.appendJSON(sb, (Object) map);
- assertEquals("{\"left\":\"name\",\"right\":\"Fred\"}", sb.toString());
- }
-
public void testAppendJSONMap_reflection() throws Exception {
StringBuilder sb = new StringBuilder();
Map<Object, String> map = new HashMap<>();
map.put(Integer.valueOf(42), "Fred");
+ assertFalse(map.keySet().isEmpty()); // trigger creation of key set
+ assertFalse(map.entrySet().isEmpty()); // trigger creation of entry set
StringBuilderTools.appendJSON(sb, map);
- String expected = "{\"entrySet\":[{\"hash\":42,\"key\":42,\"next\":null,\"value\":\"Fred\"}],\"keySet\":null,\"loadFactor\":0.75,\"modCount\":1,\"size\":1,\"table\":[null,null,null,null,null,null,null,null,null,null,{\"hash\":42,\"key\":42,\"next\":null,\"value\":\"Fred\"},null,null,null,null,null],\"threshold\":12,\"values\":null}";
+ String expected = "{\"entrySet\":[{\"hash\":42,\"key\":42,\"next\":null,\"value\":\"Fred\"}],\"keySet\":[42],\"loadFactor\":0.75,\"modCount\":1,\"size\":1,\"table\":[null,null,null,null,null,null,null,null,null,null,{\"hash\":42,\"key\":42,\"next\":null,\"value\":\"Fred\"},null,null,null,null,null],\"threshold\":12,\"values\":null}";
assertEquals(expected, sb.toString());
sb = new StringBuilder();
StringBuilderTools.appendJSON(sb, (Object) map);

Back to the top