Skip to content

Commit 3cd5779

Browse files
committed
clean up toString() and implement toJSON() consistently
1 parent 85f40f1 commit 3cd5779

7 files changed

Lines changed: 74 additions & 76 deletions

File tree

core/src/processing/data/FloatDict.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ public Iterator<String> iterator() {
149149
}
150150
};
151151
}
152-
153-
152+
153+
154154
// Use this to iterate when you want to be able to remove elements along the way
155155
public Iterator<String> keyIterator() {
156156
return new Iterator<String>() {
@@ -211,8 +211,8 @@ public Iterator<Float> iterator() {
211211
}
212212
};
213213
}
214-
215-
214+
215+
216216
public Iterator<Float> valueIterator() {
217217
return new Iterator<Float>() {
218218
int index = -1;
@@ -370,7 +370,7 @@ private void checkMinMax(String functionName) {
370370
public int minIndex() {
371371
//checkMinMax("minIndex");
372372
if (count == 0) return -1;
373-
373+
374374
// Will still return NaN if there are 1 or more entries, and they're all NaN
375375
float m = Float.NaN;
376376
int mi = -1;
@@ -592,7 +592,7 @@ public void sortValuesReverse(boolean stable) {
592592
}
593593

594594

595-
protected void sortImpl(final boolean useKeys, final boolean reverse,
595+
protected void sortImpl(final boolean useKeys, final boolean reverse,
596596
final boolean stable) {
597597
Sort s = new Sort() {
598598
@Override
@@ -704,17 +704,20 @@ public void write(PrintWriter writer) {
704704
}
705705

706706

707+
/**
708+
* Return this dictionary as a String in JSON format.
709+
*/
710+
public String toJSON() {
711+
StringList items = new StringList();
712+
for (int i = 0; i < count; i++) {
713+
items.append(JSONObject.quote(keys[i])+ ": " + values[i]);
714+
}
715+
return "{ " + items.join(", ") + " }";
716+
}
717+
718+
707719
@Override
708720
public String toString() {
709-
StringBuilder sb = new StringBuilder();
710-
sb.append(getClass().getSimpleName() + " size=" + size() + " { ");
711-
for (int i = 0; i < size(); i++) {
712-
if (i != 0) {
713-
sb.append(", ");
714-
}
715-
sb.append("\"" + keys[i] + "\": " + values[i]);
716-
}
717-
sb.append(" }");
718-
return sb.toString();
721+
return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
719722
}
720723
}

core/src/processing/data/FloatList.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -879,23 +879,22 @@ public String join(String separator) {
879879

880880

881881
public void print() {
882-
for (int i = 0; i < size(); i++) {
882+
for (int i = 0; i < count; i++) {
883883
System.out.format("[%d] %f%n", i, data[i]);
884884
}
885885
}
886886

887887

888+
/**
889+
* Return this dictionary as a String in JSON format.
890+
*/
891+
public String toJSON() {
892+
return "[ " + join(", ") + " ]";
893+
}
894+
895+
888896
@Override
889897
public String toString() {
890-
StringBuilder sb = new StringBuilder();
891-
sb.append(getClass().getSimpleName() + " size=" + size() + " [ ");
892-
for (int i = 0; i < size(); i++) {
893-
if (i != 0) {
894-
sb.append(", ");
895-
}
896-
sb.append(i + ": " + data[i]);
897-
}
898-
sb.append(" ]");
899-
return sb.toString();
898+
return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
900899
}
901900
}

core/src/processing/data/IntDict.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ public void write(PrintWriter writer) {
665665
*/
666666
public String toJSON() {
667667
StringList items = new StringList();
668-
for (int i = 0; i < size(); i++) {
668+
for (int i = 0; i < count; i++) {
669669
items.append(JSONObject.quote(keys[i])+ ": " + values[i]);
670670
}
671671
return "{ " + items.join(", ") + " }";
@@ -675,17 +675,5 @@ public String toJSON() {
675675
@Override
676676
public String toString() {
677677
return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
678-
/*
679-
StringBuilder sb = new StringBuilder();
680-
sb.append(getClass().getSimpleName() + " size=" + size() + " { ");
681-
for (int i = 0; i < size(); i++) {
682-
if (i != 0) {
683-
sb.append(", ");
684-
}
685-
sb.append("\"" + keys[i] + "\": " + values[i]);
686-
}
687-
sb.append(" }");
688-
return sb.toString();
689-
*/
690678
}
691679
}

core/src/processing/data/IntList.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -867,23 +867,22 @@ public String join(String separator) {
867867

868868

869869
public void print() {
870-
for (int i = 0; i < size(); i++) {
870+
for (int i = 0; i < count; i++) {
871871
System.out.format("[%d] %d%n", i, data[i]);
872872
}
873873
}
874874

875875

876+
/**
877+
* Return this dictionary as a String in JSON format.
878+
*/
879+
public String toJSON() {
880+
return "[ " + join(", ") + " ]";
881+
}
882+
883+
876884
@Override
877885
public String toString() {
878-
StringBuilder sb = new StringBuilder();
879-
sb.append(getClass().getSimpleName() + " size=" + size() + " [ ");
880-
for (int i = 0; i < size(); i++) {
881-
if (i != 0) {
882-
sb.append(", ");
883-
}
884-
sb.append(i + ": " + data[i]);
885-
}
886-
sb.append(" ]");
887-
return sb.toString();
886+
return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
888887
}
889888
}

core/src/processing/data/StringDict.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -475,17 +475,20 @@ public void write(PrintWriter writer) {
475475
}
476476

477477

478+
/**
479+
* Return this dictionary as a String in JSON format.
480+
*/
481+
public String toJSON() {
482+
StringList items = new StringList();
483+
for (int i = 0; i < count; i++) {
484+
items.append(JSONObject.quote(keys[i])+ ": " + JSONObject.quote(values[i]));
485+
}
486+
return "{ " + items.join(", ") + " }";
487+
}
488+
489+
478490
@Override
479491
public String toString() {
480-
StringBuilder sb = new StringBuilder();
481-
sb.append(getClass().getSimpleName() + " size=" + size() + " { ");
482-
for (int i = 0; i < size(); i++) {
483-
if (i != 0) {
484-
sb.append(", ");
485-
}
486-
sb.append("\"" + keys[i] + "\": \"" + values[i] + "\"");
487-
}
488-
sb.append(" }");
489-
return sb.toString();
492+
return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
490493
}
491494
}

core/src/processing/data/StringList.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -750,23 +750,26 @@ public String join(String separator) {
750750

751751

752752
public void print() {
753-
for (int i = 0; i < size(); i++) {
753+
for (int i = 0; i < count; i++) {
754754
System.out.format("[%d] %s%n", i, data[i]);
755755
}
756756
}
757757

758758

759+
/**
760+
* Return this dictionary as a String in JSON format.
761+
*/
762+
public String toJSON() {
763+
StringList temp = new StringList();
764+
for (String item : this) {
765+
temp.append(JSONObject.quote(item));
766+
}
767+
return "[ " + temp.join(", ") + " ]";
768+
}
769+
770+
759771
@Override
760772
public String toString() {
761-
StringBuilder sb = new StringBuilder();
762-
sb.append(getClass().getSimpleName() + " size=" + size() + " [ ");
763-
for (int i = 0; i < size(); i++) {
764-
if (i != 0) {
765-
sb.append(", ");
766-
}
767-
sb.append(i + ": \"" + data[i] + "\"");
768-
}
769-
sb.append(" ]");
770-
return sb.toString();
773+
return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
771774
}
772775
}

core/todo.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,27 @@ X https://github.com/processing/processing/issues/4722
4444
started
4545
X add toJSON() method to IntDict
4646
X had to use JSONObject.quote() to wrap the text
47-
_ do the same for the other data classes
48-
_ note the difference between this and toJSONObject() or toJSONArray()
49-
_ or is that the better way to handle it? hm
47+
X do the same for the other data classes
48+
o note the difference between this and toJSONObject() or toJSONArray()
49+
o or is that the better way to handle it? hm
50+
X we can add JSONObject and JSONArray later perhaps
51+
X keep toJSONObject() for turning XxxxList into a numbered lookup
5052

5153
_ TRIANGLE_STRIP not working correctly with createShape() and default renderer
5254
_ https://github.com/processing/processing/issues/4678
5355

5456
_ should we drop the 'default' prefix from the ex handler so it's not static?
5557
_ http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html
58+
5659
_ listPaths(), listFiles()?
5760
_ https://github.com/processing/processing/issues/4622
61+
_ add increment() that takes IntDict to merge things
5862

5963
_ option to disable OpenGL ES on Linux?
6064
_ https://github.com/processing/processing/issues/4584
6165

6266
_ textAlign(CENTER) and pixelDensity(2) aligning incorrectly with Java2D
6367
_ https://github.com/processing/processing/issues/4020
64-
_ add increment() that takes IntDict to merge things
6568

6669
_ should fullScreen() set width and height to displayWidth/Height
6770
_ or is that being set/unset used for any state info?

0 commit comments

Comments
 (0)