Skip to content

Commit 1849ab7

Browse files
committed
iterators for JSONArray types
1 parent 8fb8937 commit 1849ab7

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

core/src/processing/data/JSONArray.java

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ of this software and associated documentation files (the "Software"), to deal
4242
import java.io.Writer;
4343
import java.lang.reflect.Array;
4444
import java.util.ArrayList;
45+
import java.util.Iterator;
4546

4647
import processing.core.PApplet;
4748

@@ -561,6 +562,111 @@ public JSONObject getJSONObject(int index, JSONObject defaultValue) {
561562
}
562563

563564

565+
public Iterable<Boolean> booleanValues() {
566+
return () -> new Iterator<>() {
567+
int index = -1;
568+
569+
public Boolean next() {
570+
return getBoolean(++index);
571+
}
572+
573+
public boolean hasNext() {
574+
return index+1 < size();
575+
}
576+
};
577+
}
578+
579+
580+
public Iterable<Integer> intValues() {
581+
return () -> new Iterator<>() {
582+
int index = -1;
583+
584+
public Integer next() {
585+
return getInt(++index);
586+
}
587+
588+
public boolean hasNext() {
589+
return index+1 < size();
590+
}
591+
};
592+
}
593+
594+
595+
public Iterable<Long> longValues() {
596+
return () -> new Iterator<>() {
597+
int index = -1;
598+
599+
public Long next() {
600+
return getLong(++index);
601+
}
602+
603+
public boolean hasNext() {
604+
return index+1 < size();
605+
}
606+
};
607+
}
608+
609+
610+
public Iterable<Float> floatValues() {
611+
return () -> new Iterator<>() {
612+
int index = -1;
613+
614+
public Float next() {
615+
return getFloat(++index);
616+
}
617+
618+
public boolean hasNext() {
619+
return index+1 < size();
620+
}
621+
};
622+
}
623+
624+
625+
public Iterable<Double> doubleValues() {
626+
return () -> new Iterator<>() {
627+
int index = -1;
628+
629+
public Double next() {
630+
return getDouble(++index);
631+
}
632+
633+
public boolean hasNext() {
634+
return index+1 < size();
635+
}
636+
};
637+
}
638+
639+
640+
public Iterable<JSONArray> arrayValues() {
641+
return () -> new Iterator<>() {
642+
int index = -1;
643+
644+
public JSONArray next() {
645+
return getJSONArray(++index);
646+
}
647+
648+
public boolean hasNext() {
649+
return index+1 < size();
650+
}
651+
};
652+
}
653+
654+
655+
public Iterable<JSONObject> objectValues() {
656+
return () -> new Iterator<>() {
657+
int index = -1;
658+
659+
public JSONObject next() {
660+
return getJSONObject(++index);
661+
}
662+
663+
public boolean hasNext() {
664+
return index+1 < size();
665+
}
666+
};
667+
}
668+
669+
564670
/** Use toStringArray() instead. */
565671
@Deprecated
566672
public String[] getStringArray() {

core/todo.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
1294 (4.3.1)
22
X difference in text position with processing 4.3, JAVA2D vs P2D
33
X https://github.com/processing/processing4/issues/768
4+
X add iterators to JSONArray
45

56

67
contribs
@@ -13,6 +14,9 @@ X https://github.com/processing/processing4/pull/776
1314
X Fix `PShape.getSpecular()`, `getEmissive()`, and `getShininess()` from @hx2A
1415
X https://github.com/processing/processing4/issues/781
1516
X https://github.com/processing/processing4/pull/782
17+
_ Fix hash of keyEvent being added to pressedKeys
18+
_ https://github.com/processing/processing4/issues/779
19+
_ https://github.com/processing/processing4/pull/786
1620

1721

1822
_ JNA version of getting resolution

0 commit comments

Comments
 (0)