From 1849ab76bcd1d10bee397c99775efd3415f4fa4b Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Wed, 27 Sep 2023 20:14:12 -0400 Subject: [PATCH] iterators for JSONArray types --- core/src/processing/data/JSONArray.java | 106 ++++++++++++++++++++++++ core/todo.txt | 4 + 2 files changed, 110 insertions(+) diff --git a/core/src/processing/data/JSONArray.java b/core/src/processing/data/JSONArray.java index 2823dcdf3f..f6157e23af 100644 --- a/core/src/processing/data/JSONArray.java +++ b/core/src/processing/data/JSONArray.java @@ -42,6 +42,7 @@ of this software and associated documentation files (the "Software"), to deal import java.io.Writer; import java.lang.reflect.Array; import java.util.ArrayList; +import java.util.Iterator; import processing.core.PApplet; @@ -561,6 +562,111 @@ public JSONObject getJSONObject(int index, JSONObject defaultValue) { } + public Iterable booleanValues() { + return () -> new Iterator<>() { + int index = -1; + + public Boolean next() { + return getBoolean(++index); + } + + public boolean hasNext() { + return index+1 < size(); + } + }; + } + + + public Iterable intValues() { + return () -> new Iterator<>() { + int index = -1; + + public Integer next() { + return getInt(++index); + } + + public boolean hasNext() { + return index+1 < size(); + } + }; + } + + + public Iterable longValues() { + return () -> new Iterator<>() { + int index = -1; + + public Long next() { + return getLong(++index); + } + + public boolean hasNext() { + return index+1 < size(); + } + }; + } + + + public Iterable floatValues() { + return () -> new Iterator<>() { + int index = -1; + + public Float next() { + return getFloat(++index); + } + + public boolean hasNext() { + return index+1 < size(); + } + }; + } + + + public Iterable doubleValues() { + return () -> new Iterator<>() { + int index = -1; + + public Double next() { + return getDouble(++index); + } + + public boolean hasNext() { + return index+1 < size(); + } + }; + } + + + public Iterable arrayValues() { + return () -> new Iterator<>() { + int index = -1; + + public JSONArray next() { + return getJSONArray(++index); + } + + public boolean hasNext() { + return index+1 < size(); + } + }; + } + + + public Iterable objectValues() { + return () -> new Iterator<>() { + int index = -1; + + public JSONObject next() { + return getJSONObject(++index); + } + + public boolean hasNext() { + return index+1 < size(); + } + }; + } + + /** Use toStringArray() instead. */ @Deprecated public String[] getStringArray() { diff --git a/core/todo.txt b/core/todo.txt index 6d2d592f2c..5c888546e6 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -1,6 +1,7 @@ 1294 (4.3.1) X difference in text position with processing 4.3, JAVA2D vs P2D X https://github.com/processing/processing4/issues/768 +X add iterators to JSONArray contribs @@ -13,6 +14,9 @@ X https://github.com/processing/processing4/pull/776 X Fix `PShape.getSpecular()`, `getEmissive()`, and `getShininess()` from @hx2A X https://github.com/processing/processing4/issues/781 X https://github.com/processing/processing4/pull/782 +_ Fix hash of keyEvent being added to pressedKeys +_ https://github.com/processing/processing4/issues/779 +_ https://github.com/processing/processing4/pull/786 _ JNA version of getting resolution