From 67e9d1314811cda600e8170b20087315ce765309 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Sun, 30 Oct 2016 16:12:40 -0400 Subject: [PATCH] clean up toString() and implement toJSON() consistently --- core/src/processing/data/FloatDict.java | 35 +++++++++++++----------- core/src/processing/data/FloatList.java | 21 +++++++------- core/src/processing/data/IntDict.java | 14 +--------- core/src/processing/data/IntList.java | 21 +++++++------- core/src/processing/data/StringDict.java | 23 +++++++++------- core/src/processing/data/StringList.java | 25 +++++++++-------- core/todo.txt | 11 +++++--- 7 files changed, 74 insertions(+), 76 deletions(-) diff --git a/core/src/processing/data/FloatDict.java b/core/src/processing/data/FloatDict.java index cfb9adc99..5c01e3d20 100644 --- a/core/src/processing/data/FloatDict.java +++ b/core/src/processing/data/FloatDict.java @@ -149,8 +149,8 @@ public class FloatDict { } }; } - - + + // Use this to iterate when you want to be able to remove elements along the way public Iterator keyIterator() { return new Iterator() { @@ -211,8 +211,8 @@ public class FloatDict { } }; } - - + + public Iterator valueIterator() { return new Iterator() { int index = -1; @@ -370,7 +370,7 @@ public class FloatDict { public int minIndex() { //checkMinMax("minIndex"); if (count == 0) return -1; - + // Will still return NaN if there are 1 or more entries, and they're all NaN float m = Float.NaN; int mi = -1; @@ -592,7 +592,7 @@ public class FloatDict { } - protected void sortImpl(final boolean useKeys, final boolean reverse, + protected void sortImpl(final boolean useKeys, final boolean reverse, final boolean stable) { Sort s = new Sort() { @Override @@ -704,17 +704,20 @@ public class FloatDict { } + /** + * Return this dictionary as a String in JSON format. + */ + public String toJSON() { + StringList items = new StringList(); + for (int i = 0; i < count; i++) { + items.append(JSONObject.quote(keys[i])+ ": " + values[i]); + } + return "{ " + items.join(", ") + " }"; + } + + @Override public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(getClass().getSimpleName() + " size=" + size() + " { "); - for (int i = 0; i < size(); i++) { - if (i != 0) { - sb.append(", "); - } - sb.append("\"" + keys[i] + "\": " + values[i]); - } - sb.append(" }"); - return sb.toString(); + return getClass().getSimpleName() + " size=" + size() + " " + toJSON(); } } diff --git a/core/src/processing/data/FloatList.java b/core/src/processing/data/FloatList.java index daf01cbfe..c3ee4c287 100644 --- a/core/src/processing/data/FloatList.java +++ b/core/src/processing/data/FloatList.java @@ -879,23 +879,22 @@ public class FloatList implements Iterable { public void print() { - for (int i = 0; i < size(); i++) { + for (int i = 0; i < count; i++) { System.out.format("[%d] %f%n", i, data[i]); } } + /** + * Return this dictionary as a String in JSON format. + */ + public String toJSON() { + return "[ " + join(", ") + " ]"; + } + + @Override public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(getClass().getSimpleName() + " size=" + size() + " [ "); - for (int i = 0; i < size(); i++) { - if (i != 0) { - sb.append(", "); - } - sb.append(i + ": " + data[i]); - } - sb.append(" ]"); - return sb.toString(); + return getClass().getSimpleName() + " size=" + size() + " " + toJSON(); } } diff --git a/core/src/processing/data/IntDict.java b/core/src/processing/data/IntDict.java index e50d1b55d..1f21b4b3f 100644 --- a/core/src/processing/data/IntDict.java +++ b/core/src/processing/data/IntDict.java @@ -665,7 +665,7 @@ public class IntDict { */ public String toJSON() { StringList items = new StringList(); - for (int i = 0; i < size(); i++) { + for (int i = 0; i < count; i++) { items.append(JSONObject.quote(keys[i])+ ": " + values[i]); } return "{ " + items.join(", ") + " }"; @@ -675,17 +675,5 @@ public class IntDict { @Override public String toString() { return getClass().getSimpleName() + " size=" + size() + " " + toJSON(); - /* - StringBuilder sb = new StringBuilder(); - sb.append(getClass().getSimpleName() + " size=" + size() + " { "); - for (int i = 0; i < size(); i++) { - if (i != 0) { - sb.append(", "); - } - sb.append("\"" + keys[i] + "\": " + values[i]); - } - sb.append(" }"); - return sb.toString(); - */ } } diff --git a/core/src/processing/data/IntList.java b/core/src/processing/data/IntList.java index 5147d7569..2d833dcf0 100644 --- a/core/src/processing/data/IntList.java +++ b/core/src/processing/data/IntList.java @@ -867,23 +867,22 @@ public class IntList implements Iterable { public void print() { - for (int i = 0; i < size(); i++) { + for (int i = 0; i < count; i++) { System.out.format("[%d] %d%n", i, data[i]); } } + /** + * Return this dictionary as a String in JSON format. + */ + public String toJSON() { + return "[ " + join(", ") + " ]"; + } + + @Override public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(getClass().getSimpleName() + " size=" + size() + " [ "); - for (int i = 0; i < size(); i++) { - if (i != 0) { - sb.append(", "); - } - sb.append(i + ": " + data[i]); - } - sb.append(" ]"); - return sb.toString(); + return getClass().getSimpleName() + " size=" + size() + " " + toJSON(); } } diff --git a/core/src/processing/data/StringDict.java b/core/src/processing/data/StringDict.java index 1e2128a12..5231c44d4 100644 --- a/core/src/processing/data/StringDict.java +++ b/core/src/processing/data/StringDict.java @@ -475,17 +475,20 @@ public class StringDict { } + /** + * Return this dictionary as a String in JSON format. + */ + public String toJSON() { + StringList items = new StringList(); + for (int i = 0; i < count; i++) { + items.append(JSONObject.quote(keys[i])+ ": " + JSONObject.quote(values[i])); + } + return "{ " + items.join(", ") + " }"; + } + + @Override public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(getClass().getSimpleName() + " size=" + size() + " { "); - for (int i = 0; i < size(); i++) { - if (i != 0) { - sb.append(", "); - } - sb.append("\"" + keys[i] + "\": \"" + values[i] + "\""); - } - sb.append(" }"); - return sb.toString(); + return getClass().getSimpleName() + " size=" + size() + " " + toJSON(); } } diff --git a/core/src/processing/data/StringList.java b/core/src/processing/data/StringList.java index 215bc7adb..9f21f3c3e 100644 --- a/core/src/processing/data/StringList.java +++ b/core/src/processing/data/StringList.java @@ -750,23 +750,26 @@ public class StringList implements Iterable { public void print() { - for (int i = 0; i < size(); i++) { + for (int i = 0; i < count; i++) { System.out.format("[%d] %s%n", i, data[i]); } } + /** + * Return this dictionary as a String in JSON format. + */ + public String toJSON() { + StringList temp = new StringList(); + for (String item : this) { + temp.append(JSONObject.quote(item)); + } + return "[ " + temp.join(", ") + " ]"; + } + + @Override public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(getClass().getSimpleName() + " size=" + size() + " [ "); - for (int i = 0; i < size(); i++) { - if (i != 0) { - sb.append(", "); - } - sb.append(i + ": \"" + data[i] + "\""); - } - sb.append(" ]"); - return sb.toString(); + return getClass().getSimpleName() + " size=" + size() + " " + toJSON(); } } diff --git a/core/todo.txt b/core/todo.txt index 6cd34c0c3..b50bd8abe 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -44,24 +44,27 @@ X https://github.com/processing/processing/issues/4722 started X add toJSON() method to IntDict X had to use JSONObject.quote() to wrap the text -_ do the same for the other data classes -_ note the difference between this and toJSONObject() or toJSONArray() -_ or is that the better way to handle it? hm +X do the same for the other data classes +o note the difference between this and toJSONObject() or toJSONArray() +o or is that the better way to handle it? hm +X we can add JSONObject and JSONArray later perhaps +X keep toJSONObject() for turning XxxxList into a numbered lookup _ TRIANGLE_STRIP not working correctly with createShape() and default renderer _ https://github.com/processing/processing/issues/4678 _ should we drop the 'default' prefix from the ex handler so it's not static? _ http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html + _ listPaths(), listFiles()? _ https://github.com/processing/processing/issues/4622 +_ add increment() that takes IntDict to merge things _ option to disable OpenGL ES on Linux? _ https://github.com/processing/processing/issues/4584 _ textAlign(CENTER) and pixelDensity(2) aligning incorrectly with Java2D _ https://github.com/processing/processing/issues/4020 -_ add increment() that takes IntDict to merge things _ should fullScreen() set width and height to displayWidth/Height _ or is that being set/unset used for any state info?