propane 3.8.0-java → 4.0.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/extensions.xml +1 -1
  3. data/.mvn/wrapper/maven-wrapper.properties +1 -1
  4. data/.travis.yml +1 -1
  5. data/CHANGELOG.md +7 -1
  6. data/README.md +7 -7
  7. data/Rakefile +6 -5
  8. data/lib/propane/app.rb +10 -17
  9. data/lib/propane/helper_methods.rb +6 -6
  10. data/lib/propane/version.rb +1 -1
  11. data/lib/propane-4.0.0.jar +0 -0
  12. data/library/pdf/itextpdf-5.5.13.2.jar +0 -0
  13. data/library/pdf/pdf.rb +7 -0
  14. data/library/slider/slider.rb +1 -1
  15. data/library/svg/batik-all-1.14.jar +0 -0
  16. data/library/svg/svg.rb +7 -0
  17. data/mvnw +3 -3
  18. data/mvnw.cmd +2 -2
  19. data/pom.rb +35 -8
  20. data/pom.xml +60 -9
  21. data/propane.gemspec +9 -7
  22. data/src/main/java/monkstone/ColorUtil.java +1 -1
  23. data/src/main/java/monkstone/FastNoiseModuleJava.java +127 -0
  24. data/src/main/java/monkstone/MathToolModule.java +31 -31
  25. data/src/main/java/monkstone/PropaneLibrary.java +3 -1
  26. data/src/main/java/monkstone/SmoothNoiseModuleJava.java +127 -0
  27. data/src/main/java/monkstone/fastmath/DegLutTables.java +25 -26
  28. data/src/main/java/monkstone/fastmath/Deglut.java +1 -1
  29. data/src/main/java/monkstone/filechooser/Chooser.java +2 -2
  30. data/src/main/java/monkstone/noise/LICENSE +121 -0
  31. data/src/main/java/monkstone/noise/OpenSimplex2F.java +914 -0
  32. data/src/main/java/monkstone/noise/OpenSimplex2S.java +1138 -0
  33. data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +1 -1
  34. data/src/main/java/monkstone/slider/CustomVerticalSlider.java +1 -1
  35. data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +1 -1
  36. data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +1 -1
  37. data/src/main/java/monkstone/slider/SliderBar.java +1 -1
  38. data/src/main/java/monkstone/slider/SliderGroup.java +1 -1
  39. data/src/main/java/monkstone/slider/WheelHandler.java +2 -2
  40. data/src/main/java/monkstone/vecmath/JRender.java +6 -6
  41. data/src/main/java/monkstone/vecmath/package-info.java +1 -1
  42. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +103 -83
  43. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +27 -41
  44. data/src/main/java/monkstone/videoevent/CaptureEvent.java +1 -1
  45. data/src/main/java/monkstone/videoevent/MovieEvent.java +1 -1
  46. data/src/main/java/monkstone/videoevent/package-info.java +1 -1
  47. data/src/main/java/processing/awt/PGraphicsJava2D.java +11 -4
  48. data/src/main/java/processing/awt/PImageAWT.java +8 -8
  49. data/src/main/java/processing/core/PApplet.java +245 -254
  50. data/src/main/java/processing/core/PConstants.java +155 -163
  51. data/src/main/java/processing/core/PGraphics.java +116 -109
  52. data/src/main/java/processing/core/PImage.java +3025 -3047
  53. data/src/main/java/processing/core/PMatrix.java +5 -2
  54. data/src/main/java/processing/data/DoubleDict.java +72 -43
  55. data/src/main/java/processing/data/DoubleList.java +6 -2
  56. data/src/main/java/processing/data/FloatDict.java +744 -756
  57. data/src/main/java/processing/data/FloatList.java +68 -26
  58. data/src/main/java/processing/data/IntDict.java +72 -45
  59. data/src/main/java/processing/data/IntList.java +63 -26
  60. data/src/main/java/processing/data/JSONArray.java +892 -931
  61. data/src/main/java/processing/data/JSONObject.java +1169 -1262
  62. data/src/main/java/processing/data/JSONTokener.java +30 -49
  63. data/src/main/java/processing/data/LongDict.java +699 -712
  64. data/src/main/java/processing/data/LongList.java +676 -700
  65. data/src/main/java/processing/data/Sort.java +1 -0
  66. data/src/main/java/processing/data/Table.java +4040 -3661
  67. data/src/main/java/processing/data/TableRow.java +16 -0
  68. data/src/main/java/processing/data/XML.java +1041 -956
  69. data/src/main/java/processing/event/TouchEvent.java +1 -1
  70. data/src/main/java/processing/opengl/FontTexture.java +2 -2
  71. data/src/main/java/processing/opengl/PGraphicsOpenGL.java +28 -31
  72. data/src/main/java/processing/opengl/PJOGL.java +8 -7
  73. data/src/main/java/processing/opengl/PShader.java +1 -6
  74. data/src/main/java/processing/opengl/PShapeOpenGL.java +23 -24
  75. data/src/main/java/processing/opengl/PSurfaceJOGL.java +6 -6
  76. data/src/main/java/processing/pdf/PGraphicsPDF.java +581 -0
  77. data/src/main/java/processing/svg/PGraphicsSVG.java +378 -0
  78. data/src/main/{java/processing/opengl → resources}/cursors/arrow.png +0 -0
  79. data/src/main/{java/processing/opengl → resources}/cursors/cross.png +0 -0
  80. data/src/main/{java/processing/opengl → resources}/cursors/hand.png +0 -0
  81. data/src/main/{java/processing/opengl → resources}/cursors/license.txt +0 -0
  82. data/src/main/{java/processing/opengl → resources}/cursors/move.png +0 -0
  83. data/src/main/{java/processing/opengl → resources}/cursors/text.png +0 -0
  84. data/src/main/{java/processing/opengl → resources}/cursors/wait.png +0 -0
  85. data/src/main/{java/processing/opengl → resources}/shaders/ColorFrag.glsl +0 -0
  86. data/src/main/{java/processing/opengl → resources}/shaders/ColorVert.glsl +0 -0
  87. data/src/main/{java/processing/opengl → resources}/shaders/LightFrag.glsl +0 -0
  88. data/src/main/{java/processing/opengl → resources}/shaders/LightVert.glsl +0 -0
  89. data/src/main/{java/processing/opengl → resources}/shaders/LineFrag.glsl +0 -0
  90. data/src/main/{java/processing/opengl → resources}/shaders/LineVert.glsl +0 -0
  91. data/src/main/{java/processing/opengl → resources}/shaders/MaskFrag.glsl +0 -0
  92. data/src/main/{java/processing/opengl → resources}/shaders/PointFrag.glsl +0 -0
  93. data/src/main/{java/processing/opengl → resources}/shaders/PointVert.glsl +0 -0
  94. data/src/main/{java/processing/opengl → resources}/shaders/TexFrag.glsl +0 -0
  95. data/src/main/{java/processing/opengl → resources}/shaders/TexLightFrag.glsl +0 -0
  96. data/src/main/{java/processing/opengl → resources}/shaders/TexLightVert.glsl +0 -0
  97. data/src/main/{java/processing/opengl → resources}/shaders/TexVert.glsl +0 -0
  98. data/test/test_helper.rb +1 -0
  99. data/test/vecmath_spec_test.rb +14 -3
  100. data/vendors/Rakefile +1 -1
  101. metadata +53 -53
  102. data/lib/propane-3.8.0.jar +0 -0
  103. data/src/main/java/monkstone/noise/Noise.java +0 -116
  104. data/src/main/java/monkstone/noise/NoiseGenerator.java +0 -63
  105. data/src/main/java/monkstone/noise/NoiseMode.java +0 -15
  106. data/src/main/java/monkstone/noise/SimplexNoise.java +0 -470
  107. data/src/main/java/monkstone/noise/ValueNoise.java +0 -170
@@ -33,7 +33,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
33
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
34
  SOFTWARE.
35
35
  */
36
-
37
36
  import java.io.File;
38
37
  import java.io.IOException;
39
38
  import java.io.PrintWriter;
@@ -42,6 +41,7 @@ import java.io.StringWriter;
42
41
  import java.io.Writer;
43
42
  import java.lang.reflect.Array;
44
43
  import java.util.ArrayList;
44
+ import java.util.Arrays;
45
45
 
46
46
  import processing.core.PApplet;
47
47
 
@@ -99,116 +99,111 @@ import processing.core.PApplet;
99
99
  */
100
100
  public class JSONArray {
101
101
 
102
- /**
103
- * The arrayList where the JSONArray's properties are kept.
104
- */
105
- private final ArrayList<Object> myArrayList;
106
-
107
-
108
- /**
109
- * Construct an empty JSONArray.
110
- */
111
- public JSONArray() {
112
- this.myArrayList = new ArrayList<>();
113
- }
114
-
115
-
116
- /**
117
- * @nowebref
118
- */
119
- public JSONArray(Reader reader) {
120
- this(new JSONTokener(reader));
121
- }
122
-
123
-
124
- /**
125
- * Construct a JSONArray from a JSONTokener.
126
- *
127
- * @param x A JSONTokener
128
- * @throws RuntimeException If there is a syntax error.
129
- * @nowebref
130
- */
131
- protected JSONArray(JSONTokener x) {
132
- this();
133
- if (x.nextClean() != '[') {
134
- throw new RuntimeException("A JSONArray text must start with '['");
102
+ /**
103
+ * The arrayList where the JSONArray's properties are kept.
104
+ */
105
+ private final ArrayList<Object> myArrayList;
106
+
107
+ /**
108
+ * Construct an empty JSONArray.
109
+ */
110
+ public JSONArray() {
111
+ this.myArrayList = new ArrayList<>();
135
112
  }
136
- if (x.nextClean() != ']') {
137
- x.back();
138
- for (;;) {
139
- if (x.nextClean() == ',') {
140
- x.back();
141
- myArrayList.add(JSONObject.NULL);
142
- } else {
143
- x.back();
144
- myArrayList.add(x.nextValue());
113
+
114
+ /**
115
+ * @param reader
116
+ * @nowebref
117
+ */
118
+ public JSONArray(Reader reader) {
119
+ this(new JSONTokener(reader));
120
+ }
121
+
122
+ /**
123
+ * Construct a JSONArray from a JSONTokener.
124
+ *
125
+ * @param x A JSONTokener
126
+ * @throws RuntimeException If there is a syntax error.
127
+ * @nowebref
128
+ */
129
+ protected JSONArray(JSONTokener x) {
130
+ this();
131
+ if (x.nextClean() != '[') {
132
+ throw new RuntimeException("A JSONArray text must start with '['");
145
133
  }
146
- switch (x.nextClean()) {
147
- case ';':
148
- case ',':
149
- if (x.nextClean() == ']') {
150
- return;
151
- }
152
- x.back();
153
- break;
154
- case ']':
155
- return;
156
- default:
157
- throw new RuntimeException("Expected a ',' or ']'");
134
+ if (x.nextClean() != ']') {
135
+ x.back();
136
+ for (;;) {
137
+ if (x.nextClean() == ',') {
138
+ x.back();
139
+ myArrayList.add(JSONObject.NULL);
140
+ } else {
141
+ x.back();
142
+ myArrayList.add(x.nextValue());
143
+ }
144
+ switch (x.nextClean()) {
145
+ case ';', ',' -> {
146
+ if (x.nextClean() == ']') {
147
+ return;
148
+ }
149
+ x.back();
150
+ }
151
+ case ']' -> {
152
+ return;
153
+ }
154
+ default ->
155
+ throw new RuntimeException("Expected a ',' or ']'");
156
+ }
157
+ }
158
158
  }
159
- }
160
159
  }
161
- }
162
160
 
163
-
164
- /**
165
- * @nowebref
166
- */
167
- public JSONArray(IntList list) {
168
- myArrayList = new ArrayList<>();
169
- for (int item : list.values()) {
170
- myArrayList.add(Integer.valueOf(item));
161
+ /**
162
+ * @param list
163
+ * @nowebref
164
+ */
165
+ public JSONArray(IntList list) {
166
+ myArrayList = new ArrayList<>();
167
+ for (int item : list.values()) {
168
+ myArrayList.add(item);
169
+ }
171
170
  }
172
- }
173
171
 
174
-
175
- /**
176
- * @nowebref
177
- */
178
- public JSONArray(FloatList list) {
179
- myArrayList = new ArrayList<>();
180
- for (float item : list.values()) {
181
- myArrayList.add(Float.valueOf(item));
172
+ /**
173
+ * @param list
174
+ * @nowebref
175
+ */
176
+ public JSONArray(FloatList list) {
177
+ myArrayList = new ArrayList<>();
178
+ for (float item : list.values()) {
179
+ myArrayList.add(item);
180
+ }
182
181
  }
183
- }
184
182
 
185
-
186
- /**
187
- * @nowebref
188
- */
189
- public JSONArray(StringList list) {
190
- myArrayList = new ArrayList<>();
191
- for (String item : list.values()) {
192
- myArrayList.add(item);
193
- }
194
- }
195
-
196
-
197
- /**
198
- * Construct a JSONArray from a source JSON text.
199
- * @param source A string that begins with
200
- * <code>[</code>&nbsp;<small>(left bracket)</small>
201
- * and ends with <code>]</code>&nbsp;<small>(right bracket)</small>.
202
- * @return {@code null} if there is a syntax error.
203
- */
204
- static public JSONArray parse(String source) {
205
- try {
206
- return new JSONArray(new JSONTokener(source));
207
- } catch (Exception e) {
208
- return null;
183
+ /**
184
+ * @param list
185
+ * @nowebref
186
+ */
187
+ public JSONArray(StringList list) {
188
+ myArrayList = new ArrayList<>();
189
+ myArrayList.addAll(Arrays.asList(list.values()));
209
190
  }
210
- }
211
191
 
192
+ /**
193
+ * Construct a JSONArray from a source JSON text.
194
+ *
195
+ * @param source A string that begins with <code>[</code>&nbsp;<small>(left
196
+ * bracket)</small>
197
+ * and ends with <code>]</code>&nbsp;<small>(right bracket)</small>.
198
+ * @return {@code null} if there is a syntax error.
199
+ */
200
+ static public JSONArray parse(String source) {
201
+ try {
202
+ return new JSONArray(new JSONTokener(source));
203
+ } catch (Exception e) {
204
+ return null;
205
+ }
206
+ }
212
207
 
213
208
  // /**
214
209
  // * Construct a JSONArray from a Collection.
@@ -223,411 +218,409 @@ public class JSONArray {
223
218
  // }
224
219
  // }
225
220
  // }
226
-
227
-
228
- // TODO not decided whether we keep this one, but used heavily by JSONObject
229
- /**
230
- * Construct a JSONArray from an array
231
- * @throws RuntimeException If not an array.
232
- */
233
- protected JSONArray(Object array) {
234
- this();
235
- if (array.getClass().isArray()) {
236
- int length = Array.getLength(array);
237
- for (int i = 0; i < length; i += 1) {
238
- this.append(JSONObject.wrap(Array.get(array, i)));
239
- }
240
- } else {
241
- throw new RuntimeException("JSONArray initial value should be a string or collection or array.");
242
- }
243
- }
244
-
245
-
246
- /**
247
- * Get the optional object value associated with an index.
248
- * @param index must be between 0 and length() - 1
249
- * @return An object value, or null if there is no
250
- * object at that index.
251
- */
252
- private Object opt(int index) {
253
- if (index < 0 || index >= this.size()) {
254
- return null;
255
- }
256
- return myArrayList.get(index);
257
- }
258
-
259
-
260
- /**
261
- * Get the object value associated with an index.
262
- * @param index must be between 0 and length() - 1
263
- * @return An object value.
264
- * @throws RuntimeException If there is no value for the index.
265
- */
266
- public Object get(int index) {
267
- Object object = opt(index);
268
- if (object == null) {
269
- throw new RuntimeException("JSONArray[" + index + "] not found.");
270
- }
271
- return object;
272
- }
273
-
274
-
275
- /**
276
- * Get the string associated with an index.
277
- *
278
- * @webref jsonarray:method
279
- * @brief Gets the String value associated with an index
280
- * @param index must be between 0 and length() - 1
281
- * @return A string value.
282
- * @throws RuntimeException If there is no string value for the index.
283
- * @see JSONArray#getInt(int)
284
- * @see JSONArray#getFloat(int)
285
- * @see JSONArray#getBoolean(int)
286
- */
287
- public String getString(int index) {
288
- Object object = this.get(index);
289
- if (object instanceof String) {
290
- return (String)object;
291
- }
292
- throw new RuntimeException("JSONArray[" + index + "] not a string.");
293
- }
294
-
295
-
296
- /**
297
- * Get the optional string associated with an index.
298
- * The defaultValue is returned if the key is not found.
299
- *
300
- * @param index The index must be between 0 and length() - 1.
301
- * @param defaultValue The default value.
302
- * @return A String value.
303
- */
304
- public String getString(int index, String defaultValue) {
305
- Object object = this.opt(index);
306
- return JSONObject.NULL.equals(object) ? defaultValue : object.toString();
307
- }
308
-
309
-
310
- /**
311
- * Get the int value associated with an index.
312
- *
313
- * @webref jsonarray:method
314
- * @brief Gets the int value associated with an index
315
- * @param index must be between 0 and length() - 1
316
- * @return The value.
317
- * @throws RuntimeException If the key is not found or if the value is not a number.
318
- * @see JSONArray#getFloat(int)
319
- * @see JSONArray#getString(int)
320
- * @see JSONArray#getBoolean(int)
321
- */
322
- public int getInt(int index) {
323
- Object object = this.get(index);
324
- try {
325
- return object instanceof Number
326
- ? ((Number)object).intValue()
327
- : Integer.parseInt((String)object);
328
- } catch (Exception e) {
329
- throw new RuntimeException("JSONArray[" + index + "] is not a number.");
330
- }
331
- }
332
-
333
-
334
- /**
335
- * Get the optional int value associated with an index.
336
- * The defaultValue is returned if there is no value for the index,
337
- * or if the value is not a number and cannot be converted to a number.
338
- * @param index The index must be between 0 and length() - 1.
339
- * @param defaultValue The default value.
340
- * @return The value.
341
- */
342
- public int getInt(int index, int defaultValue) {
343
- try {
344
- return getInt(index);
345
- } catch (Exception e) {
346
- return defaultValue;
221
+ // TODO not decided whether we keep this one, but used heavily by JSONObject
222
+ /**
223
+ * Construct a JSONArray from an array
224
+ *
225
+ * @param array
226
+ * @throws RuntimeException If not an array.
227
+ */
228
+ protected JSONArray(Object array) {
229
+ this();
230
+ if (array.getClass().isArray()) {
231
+ int length = Array.getLength(array);
232
+ for (int i = 0; i < length; i += 1) {
233
+ this.append(JSONObject.wrap(Array.get(array, i)));
234
+ }
235
+ } else {
236
+ throw new RuntimeException("JSONArray initial value should be a string or collection or array.");
237
+ }
347
238
  }
348
- }
349
-
350
-
351
- /**
352
- * Get the long value associated with an index.
353
- *
354
- * @param index The index must be between 0 and length() - 1
355
- * @return The value.
356
- * @throws RuntimeException If the key is not found or if the value cannot
357
- * be converted to a number.
358
- */
359
- public long getLong(int index) {
360
- Object object = this.get(index);
361
- try {
362
- return object instanceof Number
363
- ? ((Number)object).longValue()
364
- : Long.parseLong((String)object);
365
- } catch (Exception e) {
366
- throw new RuntimeException("JSONArray[" + index + "] is not a number.");
239
+
240
+ /**
241
+ * Get the optional object value associated with an index.
242
+ *
243
+ * @param index must be between 0 and length() - 1
244
+ * @return An object value, or null if there is no object at that index.
245
+ */
246
+ private Object opt(int index) {
247
+ if (index < 0 || index >= this.size()) {
248
+ return null;
249
+ }
250
+ return myArrayList.get(index);
367
251
  }
368
- }
369
-
370
-
371
- /**
372
- * Get the optional long value associated with an index.
373
- * The defaultValue is returned if there is no value for the index,
374
- * or if the value is not a number and cannot be converted to a number.
375
- * @param index The index must be between 0 and length() - 1.
376
- * @param defaultValue The default value.
377
- * @return The value.
378
- */
379
- public long getLong(int index, long defaultValue) {
380
- try {
381
- return this.getLong(index);
382
- } catch (Exception e) {
383
- return defaultValue;
252
+
253
+ /**
254
+ * Get the object value associated with an index.
255
+ *
256
+ * @param index must be between 0 and length() - 1
257
+ * @return An object value.
258
+ * @throws RuntimeException If there is no value for the index.
259
+ */
260
+ public Object get(int index) {
261
+ Object object = opt(index);
262
+ if (object == null) {
263
+ throw new RuntimeException("JSONArray[" + index + "] not found.");
264
+ }
265
+ return object;
384
266
  }
385
- }
386
-
387
-
388
- /**
389
- * Get a value from an index as a float. JSON uses 'double' values
390
- * internally, so this is simply getDouble() cast to a float.
391
- *
392
- * @webref jsonarray:method
393
- * @brief Gets the float value associated with an index
394
- * @param index must be between 0 and length() - 1
395
- * @see JSONArray#getInt(int)
396
- * @see JSONArray#getString(int)
397
- * @see JSONArray#getBoolean(int)
398
- */
399
- public float getFloat(int index) {
400
- return (float) getDouble(index);
401
- }
402
-
403
-
404
- public float getFloat(int index, float defaultValue) {
405
- try {
406
- return getFloat(index);
407
- } catch (Exception e) {
408
- return defaultValue;
267
+
268
+ /**
269
+ * Get the string associated with an index.
270
+ *
271
+ * @webref jsonarray:method
272
+ * @brief Gets the String value associated with an index
273
+ * @param index must be between 0 and length() - 1
274
+ * @return A string value.
275
+ * @throws RuntimeException If there is no string value for the index.
276
+ * @see JSONArray#getInt(int)
277
+ * @see JSONArray#getFloat(int)
278
+ * @see JSONArray#getBoolean(int)
279
+ */
280
+ public String getString(int index) {
281
+ Object object = this.get(index);
282
+ if (object instanceof String string) {
283
+ return string;
284
+ }
285
+ throw new RuntimeException("JSONArray[" + index + "] not a string.");
409
286
  }
410
- }
411
-
412
-
413
- /**
414
- * Get the double value associated with an index.
415
- *
416
- * @param index must be between 0 and length() - 1
417
- * @return The value.
418
- * @throws RuntimeException If the key is not found or if the value cannot
419
- * be converted to a number.
420
- */
421
- public double getDouble(int index) {
422
- Object object = this.get(index);
423
- try {
424
- return object instanceof Number
425
- ? ((Number)object).doubleValue()
426
- : Double.parseDouble((String)object);
427
- } catch (Exception e) {
428
- throw new RuntimeException("JSONArray[" + index + "] is not a number.");
287
+
288
+ /**
289
+ * Get the optional string associated with an index. The defaultValue is
290
+ * returned if the key is not found.
291
+ *
292
+ * @param index The index must be between 0 and length() - 1.
293
+ * @param defaultValue The default value.
294
+ * @return A String value.
295
+ */
296
+ public String getString(int index, String defaultValue) {
297
+ Object object = this.opt(index);
298
+ return JSONObject.NULL.equals(object) ? defaultValue : object.toString();
429
299
  }
430
- }
431
-
432
-
433
- /**
434
- * Get the optional double value associated with an index.
435
- * The defaultValue is returned if there is no value for the index,
436
- * or if the value is not a number and cannot be converted to a number.
437
- *
438
- * @param index subscript
439
- * @param defaultValue The default value.
440
- * @return The value.
441
- */
442
- public double getDouble(int index, double defaultValue) {
443
- try {
444
- return this.getDouble(index);
445
- } catch (Exception e) {
446
- return defaultValue;
300
+
301
+ /**
302
+ * Get the int value associated with an index.
303
+ *
304
+ * @webref jsonarray:method
305
+ * @brief Gets the int value associated with an index
306
+ * @param index must be between 0 and length() - 1
307
+ * @return The value.
308
+ * @throws RuntimeException If the key is not found or if the value is not a
309
+ * number.
310
+ * @see JSONArray#getFloat(int)
311
+ * @see JSONArray#getString(int)
312
+ * @see JSONArray#getBoolean(int)
313
+ */
314
+ public int getInt(int index) {
315
+ Object object = this.get(index);
316
+ try {
317
+ return object instanceof Number
318
+ ? ((Number) object).intValue()
319
+ : Integer.parseInt((String) object);
320
+ } catch (NumberFormatException e) {
321
+ throw new RuntimeException("JSONArray[" + index + "] is not a number.");
322
+ }
447
323
  }
448
- }
449
-
450
-
451
- /**
452
- * Get the boolean value associated with an index.
453
- * The string values "true" and "false" are converted to boolean.
454
- *
455
- * @webref jsonarray:method
456
- * @brief Gets the boolean value associated with an index
457
- * @param index must be between 0 and length() - 1
458
- * @return The truth.
459
- * @throws RuntimeException If there is no value for the index or if the
460
- * value is not convertible to boolean.
461
- * @see JSONArray#getInt(int)
462
- * @see JSONArray#getFloat(int)
463
- * @see JSONArray#getString(int)
464
- */
465
- public boolean getBoolean(int index) {
466
- Object object = this.get(index);
467
- if (object.equals(Boolean.FALSE) ||
468
- (object instanceof String &&
469
- ((String)object).equalsIgnoreCase("false"))) {
470
- return false;
471
- } else if (object.equals(Boolean.TRUE) ||
472
- (object instanceof String &&
473
- ((String)object).equalsIgnoreCase("true"))) {
474
- return true;
324
+
325
+ /**
326
+ * Get the optional int value associated with an index. The defaultValue is
327
+ * returned if there is no value for the index, or if the value is not a
328
+ * number and cannot be converted to a number.
329
+ *
330
+ * @param index The index must be between 0 and length() - 1.
331
+ * @param defaultValue The default value.
332
+ * @return The value.
333
+ */
334
+ public int getInt(int index, int defaultValue) {
335
+ try {
336
+ return getInt(index);
337
+ } catch (Exception e) {
338
+ return defaultValue;
339
+ }
475
340
  }
476
- throw new RuntimeException("JSONArray[" + index + "] is not a boolean.");
477
- }
478
-
479
-
480
- /**
481
- * Get the optional boolean value associated with an index.
482
- * It returns the defaultValue if there is no value at that index or if
483
- * it is not a Boolean or the String "true" or "false" (case insensitive).
484
- *
485
- * @param index The index must be between 0 and length() - 1.
486
- * @param defaultValue A boolean default.
487
- * @return The truth.
488
- */
489
- public boolean getBoolean(int index, boolean defaultValue) {
490
- try {
491
- return getBoolean(index);
492
- } catch (Exception e) {
493
- return defaultValue;
341
+
342
+ /**
343
+ * Get the long value associated with an index.
344
+ *
345
+ * @param index The index must be between 0 and length() - 1
346
+ * @return The value.
347
+ * @throws RuntimeException If the key is not found or if the value cannot
348
+ * be converted to a number.
349
+ */
350
+ public long getLong(int index) {
351
+ Object object = this.get(index);
352
+ try {
353
+ return object instanceof Number
354
+ ? ((Number) object).longValue()
355
+ : Long.parseLong((String) object);
356
+ } catch (NumberFormatException e) {
357
+ throw new RuntimeException("JSONArray[" + index + "] is not a number.");
358
+ }
494
359
  }
495
- }
496
-
497
-
498
- /**
499
- * Get the JSONArray associated with an index.
500
- *
501
- * @webref jsonobject:method
502
- * @brief Gets the JSONArray associated with an index value
503
- * @param index must be between 0 and length() - 1
504
- * @return A JSONArray value.
505
- * @throws RuntimeException If there is no value for the index. or if the
506
- * value is not a JSONArray
507
- * @see JSONArray#getJSONObject(int)
508
- * @see JSONArray#setJSONObject(int, JSONObject)
509
- * @see JSONArray#setJSONArray(int, JSONArray)
510
- */
511
- public JSONArray getJSONArray(int index) {
512
- Object object = this.get(index);
513
- if (object instanceof JSONArray) {
514
- return (JSONArray)object;
360
+
361
+ /**
362
+ * Get the optional long value associated with an index. The defaultValue is
363
+ * returned if there is no value for the index, or if the value is not a
364
+ * number and cannot be converted to a number.
365
+ *
366
+ * @param index The index must be between 0 and length() - 1.
367
+ * @param defaultValue The default value.
368
+ * @return The value.
369
+ */
370
+ public long getLong(int index, long defaultValue) {
371
+ try {
372
+ return this.getLong(index);
373
+ } catch (Exception e) {
374
+ return defaultValue;
375
+ }
515
376
  }
516
- throw new RuntimeException("JSONArray[" + index + "] is not a JSONArray.");
517
- }
518
377
 
378
+ /**
379
+ * Get a value from an index as a float.JSON uses 'double' values
380
+ * internally, so this is simply getDouble() cast to a float.
381
+ *
382
+ * @return
383
+ * @webref jsonarray:method
384
+ * @brief Gets the float value associated with an index
385
+ * @param index must be between 0 and length() - 1
386
+ * @see JSONArray#getInt(int)
387
+ * @see JSONArray#getString(int)
388
+ * @see JSONArray#getBoolean(int)
389
+ */
390
+ public float getFloat(int index) {
391
+ return (float) getDouble(index);
392
+ }
519
393
 
520
- public JSONArray getJSONArray(int index, JSONArray defaultValue) {
521
- try {
522
- return getJSONArray(index);
523
- } catch (Exception e) {
524
- return defaultValue;
394
+ public float getFloat(int index, float defaultValue) {
395
+ try {
396
+ return getFloat(index);
397
+ } catch (Exception e) {
398
+ return defaultValue;
399
+ }
525
400
  }
526
- }
527
-
528
-
529
- /**
530
- * Get the JSONObject associated with an index.
531
- *
532
- * @webref jsonobject:method
533
- * @brief Gets the JSONObject associated with an index value
534
- * @param index the index value of the object to get
535
- * @return A JSONObject value.
536
- * @throws RuntimeException If there is no value for the index or if the
537
- * value is not a JSONObject
538
- * @see JSONArray#getJSONArray(int)
539
- * @see JSONArray#setJSONObject(int, JSONObject)
540
- * @see JSONArray#setJSONArray(int, JSONArray)
541
- */
542
- public JSONObject getJSONObject(int index) {
543
- Object object = this.get(index);
544
- if (object instanceof JSONObject) {
545
- return (JSONObject)object;
401
+
402
+ /**
403
+ * Get the double value associated with an index.
404
+ *
405
+ * @param index must be between 0 and length() - 1
406
+ * @return The value.
407
+ * @throws RuntimeException If the key is not found or if the value cannot
408
+ * be converted to a number.
409
+ */
410
+ public double getDouble(int index) {
411
+ Object object = this.get(index);
412
+ try {
413
+ return object instanceof Number
414
+ ? ((Number) object).doubleValue()
415
+ : Double.parseDouble((String) object);
416
+ } catch (NumberFormatException e) {
417
+ throw new RuntimeException("JSONArray[" + index + "] is not a number.");
418
+ }
546
419
  }
547
- throw new RuntimeException("JSONArray[" + index + "] is not a JSONObject.");
548
- }
549
420
 
421
+ /**
422
+ * Get the optional double value associated with an index. The defaultValue
423
+ * is returned if there is no value for the index, or if the value is not a
424
+ * number and cannot be converted to a number.
425
+ *
426
+ * @param index subscript
427
+ * @param defaultValue The default value.
428
+ * @return The value.
429
+ */
430
+ public double getDouble(int index, double defaultValue) {
431
+ try {
432
+ return this.getDouble(index);
433
+ } catch (Exception e) {
434
+ return defaultValue;
435
+ }
436
+ }
550
437
 
551
- public JSONObject getJSONObject(int index, JSONObject defaultValue) {
552
- try {
553
- return getJSONObject(index);
554
- } catch (Exception e) {
555
- return defaultValue;
438
+ /**
439
+ * Get the boolean value associated with an index. The string values "true"
440
+ * and "false" are converted to boolean.
441
+ *
442
+ * @webref jsonarray:method
443
+ * @brief Gets the boolean value associated with an index
444
+ * @param index must be between 0 and length() - 1
445
+ * @return The truth.
446
+ * @throws RuntimeException If there is no value for the index or if the
447
+ * value is not convertible to boolean.
448
+ * @see JSONArray#getInt(int)
449
+ * @see JSONArray#getFloat(int)
450
+ * @see JSONArray#getString(int)
451
+ */
452
+ public boolean getBoolean(int index) {
453
+ Object object = this.get(index);
454
+ if (object.equals(Boolean.FALSE)
455
+ || (object instanceof String
456
+ && ((String) object).equalsIgnoreCase("false"))) {
457
+ return false;
458
+ } else if (object.equals(Boolean.TRUE)
459
+ || (object instanceof String
460
+ && ((String) object).equalsIgnoreCase("true"))) {
461
+ return true;
462
+ }
463
+ throw new RuntimeException("JSONArray[" + index + "] is not a boolean.");
556
464
  }
557
- }
558
-
559
-
560
- /**
561
- * Get this entire array as a String array.
562
- *
563
- * @webref jsonarray:method
564
- * @brief Gets the entire array as an array of Strings
565
- * @see JSONArray#getIntArray()
566
- */
567
- public String[] getStringArray() {
568
- String[] outgoing = new String[size()];
569
- for (int i = 0; i < size(); i++) {
570
- outgoing[i] = getString(i);
465
+
466
+ /**
467
+ * Get the optional boolean value associated with an index. It returns the
468
+ * defaultValue if there is no value at that index or if it is not a Boolean
469
+ * or the String "true" or "false" (case insensitive).
470
+ *
471
+ * @param index The index must be between 0 and length() - 1.
472
+ * @param defaultValue A boolean default.
473
+ * @return The truth.
474
+ */
475
+ public boolean getBoolean(int index, boolean defaultValue) {
476
+ try {
477
+ return getBoolean(index);
478
+ } catch (Exception e) {
479
+ return defaultValue;
480
+ }
571
481
  }
572
- return outgoing;
573
- }
574
-
575
-
576
- /**
577
- * Get this entire array as an int array. Everything must be an int.
578
- *
579
- * @webref jsonarray:method
580
- * @brief Gets the entire array as array of ints
581
- * @see JSONArray#getStringArray()
582
- */
583
- public int[] getIntArray() {
584
- int[] outgoing = new int[size()];
585
- for (int i = 0; i < size(); i++) {
586
- outgoing[i] = getInt(i);
482
+
483
+ /**
484
+ * Get the JSONArray associated with an index.
485
+ *
486
+ * @webref jsonobject:method
487
+ * @brief Gets the JSONArray associated with an index value
488
+ * @param index must be between 0 and length() - 1
489
+ * @return A JSONArray value.
490
+ * @throws RuntimeException If there is no value for the index. or if the
491
+ * value is not a JSONArray
492
+ * @see JSONArray#getJSONObject(int)
493
+ * @see JSONArray#setJSONObject(int, JSONObject)
494
+ * @see JSONArray#setJSONArray(int, JSONArray)
495
+ */
496
+ public JSONArray getJSONArray(int index) {
497
+ Object object = this.get(index);
498
+ if (object instanceof JSONArray jSONArray) {
499
+ return jSONArray;
500
+ }
501
+ throw new RuntimeException("JSONArray[" + index + "] is not a JSONArray.");
587
502
  }
588
- return outgoing;
589
- }
590
503
 
504
+ public JSONArray getJSONArray(int index, JSONArray defaultValue) {
505
+ try {
506
+ return getJSONArray(index);
507
+ } catch (Exception e) {
508
+ return defaultValue;
509
+ }
510
+ }
591
511
 
592
- /** Get this entire array as a long array. Everything must be an long. */
593
- public long[] getLongArray() {
594
- long[] outgoing = new long[size()];
595
- for (int i = 0; i < size(); i++) {
596
- outgoing[i] = getLong(i);
512
+ /**
513
+ * Get the JSONObject associated with an index.
514
+ *
515
+ * @webref jsonobject:method
516
+ * @brief Gets the JSONObject associated with an index value
517
+ * @param index the index value of the object to get
518
+ * @return A JSONObject value.
519
+ * @throws RuntimeException If there is no value for the index or if the
520
+ * value is not a JSONObject
521
+ * @see JSONArray#getJSONArray(int)
522
+ * @see JSONArray#setJSONObject(int, JSONObject)
523
+ * @see JSONArray#setJSONArray(int, JSONArray)
524
+ */
525
+ public JSONObject getJSONObject(int index) {
526
+ Object object = this.get(index);
527
+ if (object instanceof JSONObject jSONObject) {
528
+ return jSONObject;
529
+ }
530
+ throw new RuntimeException("JSONArray[" + index + "] is not a JSONObject.");
597
531
  }
598
- return outgoing;
599
- }
600
532
 
533
+ public JSONObject getJSONObject(int index, JSONObject defaultValue) {
534
+ try {
535
+ return getJSONObject(index);
536
+ } catch (Exception e) {
537
+ return defaultValue;
538
+ }
539
+ }
601
540
 
602
- /** Get this entire array as a float array. Everything must be an float. */
603
- public float[] getFloatArray() {
604
- float[] outgoing = new float[size()];
605
- for (int i = 0; i < size(); i++) {
606
- outgoing[i] = getFloat(i);
541
+ /**
542
+ * Get this entire array as a String array.
543
+ *
544
+ * @return
545
+ * @webref jsonarray:method
546
+ * @brief Gets the entire array as an array of Strings
547
+ * @see JSONArray#getIntArray()
548
+ */
549
+ public String[] getStringArray() {
550
+ String[] outgoing = new String[size()];
551
+ for (int i = 0; i < size(); i++) {
552
+ outgoing[i] = getString(i);
553
+ }
554
+ return outgoing;
607
555
  }
608
- return outgoing;
609
- }
610
556
 
557
+ /**
558
+ * Get this entire array as an int array.Everything must be an int.
559
+ *
560
+ * @return
561
+ * @webref jsonarray:method
562
+ * @brief Gets the entire array as array of ints
563
+ * @see JSONArray#getStringArray()
564
+ */
565
+ public int[] getIntArray() {
566
+ int[] outgoing = new int[size()];
567
+ for (int i = 0; i < size(); i++) {
568
+ outgoing[i] = getInt(i);
569
+ }
570
+ return outgoing;
571
+ }
611
572
 
612
- /** Get this entire array as a double array. Everything must be an double. */
613
- public double[] getDoubleArray() {
614
- double[] outgoing = new double[size()];
615
- for (int i = 0; i < size(); i++) {
616
- outgoing[i] = getDouble(i);
573
+ /**
574
+ * * Get this entire array as a long array.Everything must be an long.
575
+ *
576
+ * @return
577
+ */
578
+ public long[] getLongArray() {
579
+ long[] outgoing = new long[size()];
580
+ for (int i = 0; i < size(); i++) {
581
+ outgoing[i] = getLong(i);
582
+ }
583
+ return outgoing;
617
584
  }
618
- return outgoing;
619
- }
620
585
 
586
+ /**
587
+ * * Get this entire array as a float array.Everything must be an float.
588
+ *
589
+ * @return
590
+ */
591
+ public float[] getFloatArray() {
592
+ float[] outgoing = new float[size()];
593
+ for (int i = 0; i < size(); i++) {
594
+ outgoing[i] = getFloat(i);
595
+ }
596
+ return outgoing;
597
+ }
621
598
 
622
- /** Get this entire array as a boolean array. Everything must be a boolean. */
623
- public boolean[] getBooleanArray() {
624
- boolean[] outgoing = new boolean[size()];
625
- for (int i = 0; i < size(); i++) {
626
- outgoing[i] = getBoolean(i);
599
+ /**
600
+ * * Get this entire array as a double array.Everything must be an double.
601
+ *
602
+ * @return
603
+ */
604
+ public double[] getDoubleArray() {
605
+ double[] outgoing = new double[size()];
606
+ for (int i = 0; i < size(); i++) {
607
+ outgoing[i] = getDouble(i);
608
+ }
609
+ return outgoing;
627
610
  }
628
- return outgoing;
629
- }
630
611
 
612
+ /**
613
+ * * Get this entire array as a boolean array.Everything must be a boolean.
614
+ *
615
+ * @return
616
+ */
617
+ public boolean[] getBooleanArray() {
618
+ boolean[] outgoing = new boolean[size()];
619
+ for (int i = 0; i < size(); i++) {
620
+ outgoing[i] = getBoolean(i);
621
+ }
622
+ return outgoing;
623
+ }
631
624
 
632
625
  // /**
633
626
  // * Get the optional boolean value associated with an index.
@@ -692,89 +685,81 @@ public class JSONArray {
692
685
  // public String optString(int index) {
693
686
  // return this.optString(index, "");
694
687
  // }
688
+ /**
689
+ * Append an String value. This increases the array's length by one.
690
+ *
691
+ * @webref jsonarray:method
692
+ * @brief Appends a value, increasing the array's length by one
693
+ * @param value a String value
694
+ * @return this.
695
+ * @see JSONArray#size()
696
+ * @see JSONArray#remove(int)
697
+ */
698
+ public JSONArray append(String value) {
699
+ this.append((Object) value);
700
+ return this;
701
+ }
695
702
 
703
+ /**
704
+ * Append an int value. This increases the array's length by one.
705
+ *
706
+ * @param value an int value
707
+ * @return this.
708
+ */
709
+ public JSONArray append(int value) {
710
+ this.append(Integer.valueOf(value));
711
+ return this;
712
+ }
696
713
 
697
- /**
698
- * Append an String value. This increases the array's length by one.
699
- *
700
- * @webref jsonarray:method
701
- * @brief Appends a value, increasing the array's length by one
702
- * @param value a String value
703
- * @return this.
704
- * @see JSONArray#size()
705
- * @see JSONArray#remove(int)
706
- */
707
- public JSONArray append(String value) {
708
- this.append((Object)value);
709
- return this;
710
- }
711
-
712
-
713
- /**
714
- * Append an int value. This increases the array's length by one.
715
- *
716
- * @param value an int value
717
- * @return this.
718
- */
719
- public JSONArray append(int value) {
720
- this.append(Integer.valueOf(value));
721
- return this;
722
- }
723
-
724
-
725
- /**
726
- * Append an long value. This increases the array's length by one.
727
- *
728
- * @nowebref
729
- * @param value A long value.
730
- * @return this.
731
- */
732
- public JSONArray append(long value) {
733
- this.append(Long.valueOf(value));
734
- return this;
735
- }
736
-
737
-
738
- /**
739
- * Append a float value. This increases the array's length by one.
740
- * This will store the value as a double, since there are no floats in JSON.
741
- *
742
- * @param value a float value
743
- * @throws RuntimeException if the value is not finite.
744
- * @return this.
745
- */
746
- public JSONArray append(float value) {
747
- return append((double) value);
748
- }
749
-
750
-
751
- /**
752
- * Append a double value. This increases the array's length by one.
753
- *
754
- * @nowebref
755
- * @param value A double value.
756
- * @throws RuntimeException if the value is not finite.
757
- * @return this.
758
- */
759
- public JSONArray append(double value) {
760
- Double d = value;
761
- JSONObject.testValidity(d);
762
- this.append(d);
763
- return this;
764
- }
765
-
766
-
767
- /**
768
- * Append a boolean value. This increases the array's length by one.
769
- *
770
- * @param value a boolean value
771
- * @return this.
772
- */
773
- public JSONArray append(boolean value) {
774
- this.append(value ? Boolean.TRUE : Boolean.FALSE);
775
- return this;
776
- }
714
+ /**
715
+ * Append an long value. This increases the array's length by one.
716
+ *
717
+ * @nowebref
718
+ * @param value A long value.
719
+ * @return this.
720
+ */
721
+ public JSONArray append(long value) {
722
+ this.append(Long.valueOf(value));
723
+ return this;
724
+ }
777
725
 
726
+ /**
727
+ * Append a float value. This increases the array's length by one. This will
728
+ * store the value as a double, since there are no floats in JSON.
729
+ *
730
+ * @param value a float value
731
+ * @throws RuntimeException if the value is not finite.
732
+ * @return this.
733
+ */
734
+ public JSONArray append(float value) {
735
+ return append((double) value);
736
+ }
737
+
738
+ /**
739
+ * Append a double value. This increases the array's length by one.
740
+ *
741
+ * @nowebref
742
+ * @param value A double value.
743
+ * @throws RuntimeException if the value is not finite.
744
+ * @return this.
745
+ */
746
+ public JSONArray append(double value) {
747
+ Double d = value;
748
+ JSONObject.testValidity(d);
749
+ this.append(d);
750
+ return this;
751
+ }
752
+
753
+ /**
754
+ * Append a boolean value. This increases the array's length by one.
755
+ *
756
+ * @param value a boolean value
757
+ * @return this.
758
+ */
759
+ public JSONArray append(boolean value) {
760
+ this.append(value ? Boolean.TRUE : Boolean.FALSE);
761
+ return this;
762
+ }
778
763
 
779
764
  // /**
780
765
  // * Put a value in the JSONArray, where the value will be a
@@ -786,8 +771,6 @@ public class JSONArray {
786
771
  // this.append(new JSONArray(value));
787
772
  // return this;
788
773
  // }
789
-
790
-
791
774
  // /**
792
775
  // * Put a value in the JSONArray, where the value will be a
793
776
  // * JSONObject which is produced from a Map.
@@ -798,38 +781,36 @@ public class JSONArray {
798
781
  // this.append(new JSONObject(value));
799
782
  // return this;
800
783
  // }
784
+ /**
785
+ * @param value a JSONArray value
786
+ * @return
787
+ */
788
+ public JSONArray append(JSONArray value) {
789
+ myArrayList.add(value);
790
+ return this;
791
+ }
801
792
 
793
+ /**
794
+ * @param value a JSONObject value
795
+ * @return
796
+ */
797
+ public JSONArray append(JSONObject value) {
798
+ myArrayList.add(value);
799
+ return this;
800
+ }
802
801
 
803
- /**
804
- * @param value a JSONArray value
805
- */
806
- public JSONArray append(JSONArray value) {
807
- myArrayList.add(value);
808
- return this;
809
- }
810
-
811
-
812
- /**
813
- * @param value a JSONObject value
814
- */
815
- public JSONArray append(JSONObject value) {
816
- myArrayList.add(value);
817
- return this;
818
- }
819
-
820
-
821
- /**
822
- * Append an object value. This increases the array's length by one.
823
- * @param value An object value. The value should be a
824
- * Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
825
- * JSONObject.NULL object.
826
- * @return this.
827
- */
828
- protected JSONArray append(Object value) {
829
- myArrayList.add(value);
830
- return this;
831
- }
832
-
802
+ /**
803
+ * Append an object value. This increases the array's length by one.
804
+ *
805
+ * @param value An object value. The value should be a Boolean, Double,
806
+ * Integer, JSONArray, JSONObject, Long, or String, or the JSONObject.NULL
807
+ * object.
808
+ * @return this.
809
+ */
810
+ protected JSONArray append(Object value) {
811
+ myArrayList.add(value);
812
+ return this;
813
+ }
833
814
 
834
815
  // /**
835
816
  // * Put a value in the JSONArray, where the value will be a
@@ -844,120 +825,114 @@ public class JSONArray {
844
825
  // this.set(index, new JSONArray(value));
845
826
  // return this;
846
827
  // }
828
+ /**
829
+ * Put or replace a String value. If the index is greater than the length of
830
+ * the JSONArray, then null elements will be added as necessary to pad it
831
+ * out.
832
+ *
833
+ * @webref jsonarray:method
834
+ * @brief Put a String value in the JSONArray
835
+ * @param index an index value
836
+ * @param value the value to assign
837
+ * @return this.
838
+ * @throws RuntimeException If the index is negative.
839
+ * @see JSONArray#setInt(int, int)
840
+ * @see JSONArray#setFloat(int, float)
841
+ * @see JSONArray#setBoolean(int, boolean)
842
+ */
843
+ public JSONArray setString(int index, String value) {
844
+ this.set(index, value);
845
+ return this;
846
+ }
847
847
 
848
+ /**
849
+ * Put or replace an int value. If the index is greater than the length of
850
+ * the JSONArray, then null elements will be added as necessary to pad it
851
+ * out.
852
+ *
853
+ * @webref jsonarray:method
854
+ * @brief Put an int value in the JSONArray
855
+ * @param index an index value
856
+ * @param value the value to assign
857
+ * @return this.
858
+ * @throws RuntimeException If the index is negative.
859
+ * @see JSONArray#setFloat(int, float)
860
+ * @see JSONArray#setString(int, String)
861
+ * @see JSONArray#setBoolean(int, boolean)
862
+ */
863
+ public JSONArray setInt(int index, int value) {
864
+ this.set(index, value);
865
+ return this;
866
+ }
848
867
 
849
- /**
850
- * Put or replace a String value. If the index is greater than the length of
851
- * the JSONArray, then null elements will be added as necessary to pad
852
- * it out.
853
- *
854
- * @webref jsonarray:method
855
- * @brief Put a String value in the JSONArray
856
- * @param index an index value
857
- * @param value the value to assign
858
- * @return this.
859
- * @throws RuntimeException If the index is negative.
860
- * @see JSONArray#setInt(int, int)
861
- * @see JSONArray#setFloat(int, float)
862
- * @see JSONArray#setBoolean(int, boolean)
863
- */
864
- public JSONArray setString(int index, String value) {
865
- this.set(index, value);
866
- return this;
867
- }
868
-
869
-
870
- /**
871
- * Put or replace an int value. If the index is greater than the length of
872
- * the JSONArray, then null elements will be added as necessary to pad
873
- * it out.
874
- *
875
- * @webref jsonarray:method
876
- * @brief Put an int value in the JSONArray
877
- * @param index an index value
878
- * @param value the value to assign
879
- * @return this.
880
- * @throws RuntimeException If the index is negative.
881
- * @see JSONArray#setFloat(int, float)
882
- * @see JSONArray#setString(int, String)
883
- * @see JSONArray#setBoolean(int, boolean)
884
- */
885
- public JSONArray setInt(int index, int value) {
886
- this.set(index, Integer.valueOf(value));
887
- return this;
888
- }
889
-
890
-
891
- /**
892
- * Put or replace a long value. If the index is greater than the length of
893
- * the JSONArray, then null elements will be added as necessary to pad
894
- * it out.
895
- * @param index The subscript.
896
- * @param value A long value.
897
- * @return this.
898
- * @throws RuntimeException If the index is negative.
899
- */
900
- public JSONArray setLong(int index, long value) {
901
- return set(index, Long.valueOf(value));
902
- }
903
-
904
-
905
- /**
906
- * Put or replace a float value. If the index is greater than the length
907
- * of the JSONArray, then null elements will be added as necessary to pad
908
- * it out. There are no 'double' values in JSON, so this is passed to
909
- * setDouble(value).
910
- *
911
- * @webref jsonarray:method
912
- * @brief Put a float value in the JSONArray
913
- * @param index an index value
914
- * @param value the value to assign
915
- * @return this.
916
- * @throws RuntimeException If the index is negative or if the value is
917
- * not finite.
918
- * @see JSONArray#setInt(int, int)
919
- * @see JSONArray#setString(int, String)
920
- * @see JSONArray#setBoolean(int, boolean)
921
- */
922
- public JSONArray setFloat(int index, float value) {
923
- return setDouble(index, value);
924
- }
925
-
926
-
927
- /**
928
- * Put or replace a double value. If the index is greater than the length of
929
- * the JSONArray, then null elements will be added as necessary to pad
930
- * it out.
931
- * @param index The subscript.
932
- * @param value A double value.
933
- * @return this.
934
- * @throws RuntimeException If the index is negative or if the value is
935
- * not finite.
936
- */
937
- public JSONArray setDouble(int index, double value) {
938
- return set(index, Double.valueOf(value));
939
- }
940
-
941
-
942
- /**
943
- * Put or replace a boolean value in the JSONArray. If the index is greater
944
- * than the length of the JSONArray, then null elements will be added as
945
- * necessary to pad it out.
946
- *
947
- * @webref jsonarray:method
948
- * @brief Put a boolean value in the JSONArray
949
- * @param index an index value
950
- * @param value the value to assign
951
- * @return this.
952
- * @throws RuntimeException If the index is negative.
953
- * @see JSONArray#setInt(int, int)
954
- * @see JSONArray#setFloat(int, float)
955
- * @see JSONArray#setString(int, String)
956
- */
957
- public JSONArray setBoolean(int index, boolean value) {
958
- return set(index, value ? Boolean.TRUE : Boolean.FALSE);
959
- }
868
+ /**
869
+ * Put or replace a long value. If the index is greater than the length of
870
+ * the JSONArray, then null elements will be added as necessary to pad it
871
+ * out.
872
+ *
873
+ * @param index The subscript.
874
+ * @param value A long value.
875
+ * @return this.
876
+ * @throws RuntimeException If the index is negative.
877
+ */
878
+ public JSONArray setLong(int index, long value) {
879
+ return set(index, value);
880
+ }
881
+
882
+ /**
883
+ * Put or replace a float value. If the index is greater than the length of
884
+ * the JSONArray, then null elements will be added as necessary to pad it
885
+ * out. There are no 'double' values in JSON, so this is passed to
886
+ * setDouble(value).
887
+ *
888
+ * @webref jsonarray:method
889
+ * @brief Put a float value in the JSONArray
890
+ * @param index an index value
891
+ * @param value the value to assign
892
+ * @return this.
893
+ * @throws RuntimeException If the index is negative or if the value is not
894
+ * finite.
895
+ * @see JSONArray#setInt(int, int)
896
+ * @see JSONArray#setString(int, String)
897
+ * @see JSONArray#setBoolean(int, boolean)
898
+ */
899
+ public JSONArray setFloat(int index, float value) {
900
+ return setDouble(index, value);
901
+ }
960
902
 
903
+ /**
904
+ * Put or replace a double value. If the index is greater than the length of
905
+ * the JSONArray, then null elements will be added as necessary to pad it
906
+ * out.
907
+ *
908
+ * @param index The subscript.
909
+ * @param value A double value.
910
+ * @return this.
911
+ * @throws RuntimeException If the index is negative or if the value is not
912
+ * finite.
913
+ */
914
+ public JSONArray setDouble(int index, double value) {
915
+ return set(index, value);
916
+ }
917
+
918
+ /**
919
+ * Put or replace a boolean value in the JSONArray. If the index is greater
920
+ * than the length of the JSONArray, then null elements will be added as
921
+ * necessary to pad it out.
922
+ *
923
+ * @webref jsonarray:method
924
+ * @brief Put a boolean value in the JSONArray
925
+ * @param index an index value
926
+ * @param value the value to assign
927
+ * @return this.
928
+ * @throws RuntimeException If the index is negative.
929
+ * @see JSONArray#setInt(int, int)
930
+ * @see JSONArray#setFloat(int, float)
931
+ * @see JSONArray#setString(int, String)
932
+ */
933
+ public JSONArray setBoolean(int index, boolean value) {
934
+ return set(index, value ? Boolean.TRUE : Boolean.FALSE);
935
+ }
961
936
 
962
937
  // /**
963
938
  // * Put a value in the JSONArray, where the value will be a
@@ -972,106 +947,103 @@ public class JSONArray {
972
947
  // this.set(index, new JSONObject(value));
973
948
  // return this;
974
949
  // }
950
+ /**
951
+ * @return @webref jsonarray:method
952
+ * @brief Sets the JSONArray value associated with an index value
953
+ * @param index the index value to target
954
+ * @param value the value to assign
955
+ * @see JSONArray#setJSONObject(int, JSONObject)
956
+ * @see JSONArray#getJSONObject(int)
957
+ * @see JSONArray#getJSONArray(int)
958
+ */
959
+ public JSONArray setJSONArray(int index, JSONArray value) {
960
+ set(index, value);
961
+ return this;
962
+ }
975
963
 
976
- /**
977
- * @webref jsonarray:method
978
- * @brief Sets the JSONArray value associated with an index value
979
- * @param index the index value to target
980
- * @param value the value to assign
981
- * @see JSONArray#setJSONObject(int, JSONObject)
982
- * @see JSONArray#getJSONObject(int)
983
- * @see JSONArray#getJSONArray(int)
984
- */
985
- public JSONArray setJSONArray(int index, JSONArray value) {
986
- set(index, value);
987
- return this;
988
- }
989
-
990
- /**
991
- * @webref jsonarray:method
992
- * @brief Sets the JSONObject value associated with an index value
993
- * @param index the index value to target
994
- * @param value the value to assign
995
- * @see JSONArray#setJSONArray(int, JSONArray)
996
- * @see JSONArray#getJSONObject(int)
997
- * @see JSONArray#getJSONArray(int)
998
- */
999
- public JSONArray setJSONObject(int index, JSONObject value) {
1000
- set(index, value);
1001
- return this;
1002
- }
1003
-
1004
-
1005
- /**
1006
- * Put or replace an object value in the JSONArray. If the index is greater
1007
- * than the length of the JSONArray, then null elements will be added as
1008
- * necessary to pad it out.
1009
- * @param index The subscript.
1010
- * @param value The value to put into the array. The value should be a
1011
- * Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
1012
- * JSONObject.NULL object.
1013
- * @return this.
1014
- * @throws RuntimeException If the index is negative or if the the value is
1015
- * an invalid number.
1016
- */
1017
- private JSONArray set(int index, Object value) {
1018
- JSONObject.testValidity(value);
1019
- if (index < 0) {
1020
- throw new RuntimeException("JSONArray[" + index + "] not found.");
964
+ /**
965
+ * @return @webref jsonarray:method
966
+ * @brief Sets the JSONObject value associated with an index value
967
+ * @param index the index value to target
968
+ * @param value the value to assign
969
+ * @see JSONArray#setJSONArray(int, JSONArray)
970
+ * @see JSONArray#getJSONObject(int)
971
+ * @see JSONArray#getJSONArray(int)
972
+ */
973
+ public JSONArray setJSONObject(int index, JSONObject value) {
974
+ set(index, value);
975
+ return this;
1021
976
  }
1022
- if (index < this.size()) {
1023
- this.myArrayList.set(index, value);
1024
- } else {
1025
- while (index != this.size()) {
1026
- this.append(JSONObject.NULL);
1027
- }
1028
- this.append(value);
977
+
978
+ /**
979
+ * Put or replace an object value in the JSONArray. If the index is greater
980
+ * than the length of the JSONArray, then null elements will be added as
981
+ * necessary to pad it out.
982
+ *
983
+ * @param index The subscript.
984
+ * @param value The value to put into the array. The value should be a
985
+ * Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
986
+ * JSONObject.NULL object.
987
+ * @return this.
988
+ * @throws RuntimeException If the index is negative or if the the value is
989
+ * an invalid number.
990
+ */
991
+ private JSONArray set(int index, Object value) {
992
+ JSONObject.testValidity(value);
993
+ if (index < 0) {
994
+ throw new RuntimeException("JSONArray[" + index + "] not found.");
995
+ }
996
+ if (index < this.size()) {
997
+ this.myArrayList.set(index, value);
998
+ } else {
999
+ while (index != this.size()) {
1000
+ this.append(JSONObject.NULL);
1001
+ }
1002
+ this.append(value);
1003
+ }
1004
+ return this;
1029
1005
  }
1030
- return this;
1031
- }
1032
-
1033
-
1034
- /**
1035
- * Get the number of elements in the JSONArray, included nulls.
1036
- *
1037
- * @webref jsonarray:method
1038
- * @brief Gets the number of elements in the JSONArray
1039
- * @return The length (or size).
1040
- * @see JSONArray#append(String)
1041
- * @see JSONArray#remove(int)
1042
- */
1043
- public int size() {
1044
- return myArrayList.size();
1045
- }
1046
-
1047
-
1048
- /**
1049
- * Determine if the value is null.
1050
- * @webref
1051
- * @param index must be between 0 and length() - 1
1052
- * @return true if the value at the index is null, or if there is no value.
1053
- */
1054
- public boolean isNull(int index) {
1055
- return JSONObject.NULL.equals(this.opt(index));
1056
- }
1057
-
1058
-
1059
- /**
1060
- * Remove an index and close the hole.
1061
- *
1062
- * @webref jsonarray:method
1063
- * @brief Removes an element
1064
- * @param index the index value of the element to be removed
1065
- * @return The value that was associated with the index, or null if there was no value.
1066
- * @see JSONArray#size()
1067
- * @see JSONArray#append(String)
1068
- */
1069
- public Object remove(int index) {
1070
- Object o = this.opt(index);
1071
- this.myArrayList.remove(index);
1072
- return o;
1073
- }
1074
1006
 
1007
+ /**
1008
+ * Get the number of elements in the JSONArray, included nulls.
1009
+ *
1010
+ * @webref jsonarray:method
1011
+ * @brief Gets the number of elements in the JSONArray
1012
+ * @return The length (or size).
1013
+ * @see JSONArray#append(String)
1014
+ * @see JSONArray#remove(int)
1015
+ */
1016
+ public int size() {
1017
+ return myArrayList.size();
1018
+ }
1019
+
1020
+ /**
1021
+ * Determine if the value is null.
1022
+ *
1023
+ * @webref
1024
+ * @param index must be between 0 and length() - 1
1025
+ * @return true if the value at the index is null, or if there is no value.
1026
+ */
1027
+ public boolean isNull(int index) {
1028
+ return JSONObject.NULL.equals(this.opt(index));
1029
+ }
1030
+
1031
+ /**
1032
+ * Remove an index and close the hole.
1033
+ *
1034
+ * @webref jsonarray:method
1035
+ * @brief Removes an element
1036
+ * @param index the index value of the element to be removed
1037
+ * @return The value that was associated with the index, or null if there
1038
+ * was no value.
1039
+ * @see JSONArray#size()
1040
+ * @see JSONArray#append(String)
1041
+ */
1042
+ public Object remove(int index) {
1043
+ Object o = this.opt(index);
1044
+ this.myArrayList.remove(index);
1045
+ return o;
1046
+ }
1075
1047
 
1076
1048
  // /**
1077
1049
  // * Produce a JSONObject by combining a JSONArray of names with the values
@@ -1092,81 +1064,73 @@ public class JSONArray {
1092
1064
  // }
1093
1065
  // return jo;
1094
1066
  // }
1095
-
1096
-
1097
1067
  // protected boolean save(OutputStream output) {
1098
1068
  // return write(PApplet.createWriter(output), null);
1099
1069
  // }
1070
+ public boolean save(File file, String options) {
1071
+ boolean success;
1072
+ try (PrintWriter writer = PApplet.createWriter(file)) {
1073
+ success = write(writer, options);
1074
+ }
1075
+ return success;
1076
+ }
1100
1077
 
1078
+ public boolean write(PrintWriter output) {
1079
+ return write(output, null);
1080
+ }
1101
1081
 
1102
- public boolean save(File file, String options) {
1103
- PrintWriter writer = PApplet.createWriter(file);
1104
- boolean success = write(writer, options);
1105
- writer.close();
1106
- return success;
1107
- }
1108
-
1109
-
1110
- public boolean write(PrintWriter output) {
1111
- return write(output, null);
1112
- }
1113
-
1114
-
1115
- public boolean write(PrintWriter output, String options) {
1116
- int indentFactor = 2;
1117
- if (options != null) {
1118
- String[] opts = PApplet.split(options, ',');
1119
- for (String opt : opts) {
1120
- if (opt.equals("compact")) {
1121
- indentFactor = -1;
1122
- } else if (opt.startsWith("indent=")) {
1123
- indentFactor = PApplet.parseInt(opt.substring(7), -2);
1124
- if (indentFactor == -2) {
1125
- throw new IllegalArgumentException("Could not read a number from " + opt);
1126
- }
1127
- } else {
1128
- System.err.println("Ignoring " + opt);
1082
+ public boolean write(PrintWriter output, String options) {
1083
+ int indentFactor = 2;
1084
+ if (options != null) {
1085
+ String[] opts = PApplet.split(options, ',');
1086
+ for (String opt : opts) {
1087
+ if (opt.equals("compact")) {
1088
+ indentFactor = -1;
1089
+ } else if (opt.startsWith("indent=")) {
1090
+ indentFactor = PApplet.parseInt(opt.substring(7), -2);
1091
+ if (indentFactor == -2) {
1092
+ throw new IllegalArgumentException("Could not read a number from " + opt);
1093
+ }
1094
+ } else {
1095
+ System.err.println("Ignoring " + opt);
1096
+ }
1097
+ }
1129
1098
  }
1130
- }
1099
+ output.print(format(indentFactor));
1100
+ output.flush();
1101
+ return true;
1131
1102
  }
1132
- output.print(format(indentFactor));
1133
- output.flush();
1134
- return true;
1135
- }
1136
-
1137
-
1138
- /**
1139
- * Return the JSON data formatted with two spaces for indents.
1140
- * Chosen to do this since it's the most common case (e.g. with println()).
1141
- * Same as format(2). Use the format() function for more options.
1142
- */
1143
- @Override
1144
- public String toString() {
1145
- try {
1146
- return format(2);
1147
- } catch (Exception e) {
1148
- return null;
1149
- }
1150
- }
1151
-
1152
-
1153
- /**
1154
- * Make a pretty-printed JSON text of this JSONArray.
1155
- * Warning: This method assumes that the data structure is acyclical.
1156
- * @param indentFactor The number of spaces to add to each level of
1157
- * indentation. Use -1 to specify no indentation and no newlines.
1158
- * @return a printable, displayable, transmittable
1159
- * representation of the object, beginning
1160
- * with <code>[</code>&nbsp;<small>(left bracket)</small> and ending
1161
- * with <code>]</code>&nbsp;<small>(right bracket)</small>.
1162
- */
1163
- public String format(int indentFactor) {
1164
- StringWriter sw = new StringWriter();
1165
- synchronized (sw.getBuffer()) {
1166
- return this.writeInternal(sw, indentFactor, 0).toString();
1103
+
1104
+ /**
1105
+ * Return the JSON data formatted with two spaces for indents. Chosen to do
1106
+ * this since it's the most common case (e.g. with println()). Same as
1107
+ * format(2). Use the format() function for more options.
1108
+ */
1109
+ @Override
1110
+ public String toString() {
1111
+ try {
1112
+ return format(2);
1113
+ } catch (Exception e) {
1114
+ return null;
1115
+ }
1167
1116
  }
1168
- }
1169
1117
 
1118
+ /**
1119
+ * Make a pretty-printed JSON text of this JSONArray. Warning: This method
1120
+ * assumes that the data structure is acyclical.
1121
+ *
1122
+ * @param indentFactor The number of spaces to add to each level of
1123
+ * indentation. Use -1 to specify no indentation and no newlines.
1124
+ * @return a printable, displayable, transmittable representation of the
1125
+ * object, beginning with <code>[</code>&nbsp;<small>(left bracket)</small>
1126
+ * and ending with <code>]</code>&nbsp;<small>(right bracket)</small>.
1127
+ */
1128
+ public String format(int indentFactor) {
1129
+ StringWriter sw = new StringWriter();
1130
+ synchronized (sw.getBuffer()) {
1131
+ return this.writeInternal(sw, indentFactor, 0).toString();
1132
+ }
1133
+ }
1170
1134
 
1171
1135
  // /**
1172
1136
  // * Write the contents of the JSONArray as JSON text to a writer. For
@@ -1179,82 +1143,79 @@ public class JSONArray {
1179
1143
  // protected Writer write(Writer writer) {
1180
1144
  // return this.write(writer, -1, 0);
1181
1145
  // }
1182
-
1183
-
1184
- /**
1185
- * Write the contents of the JSONArray as JSON text to a writer.
1186
- * <p>
1187
- * Warning: This method assumes that the data structure is acyclic.
1188
- *
1189
- * @param indentFactor
1190
- * The number of spaces to add to each level of indentation.
1191
- * Use -1 to specify no indentation and no newlines.
1192
- * @param indent
1193
- * The indention of the top level.
1194
- * @return The writer.
1195
- * @throws RuntimeException
1196
- */
1197
- protected Writer writeInternal(Writer writer, int indentFactor, int indent) {
1198
- try {
1199
- boolean commanate = false;
1200
- int length = this.size();
1201
- writer.write('[');
1202
-
1203
- // Use -1 to signify 'no indent'
1204
- int thisFactor = (indentFactor == -1) ? 0 : indentFactor;
1205
-
1206
- if (length == 1) {
1207
- JSONObject.writeValue(writer, this.myArrayList.get(0),
1208
- indentFactor, indent);
1146
+ /**
1147
+ * Write the contents of the JSONArray as JSON text to a writer.
1148
+ * <p>
1149
+ * Warning: This method assumes that the data structure is acyclic.
1150
+ *
1151
+ * @param writer
1152
+ * @param indentFactor The number of spaces to add to each level of
1153
+ * indentation. Use -1 to specify no indentation and no newlines.
1154
+ * @param indent The indention of the top level.
1155
+ * @return The writer.
1156
+ * @throws RuntimeException
1157
+ */
1158
+ protected Writer writeInternal(Writer writer, int indentFactor, int indent) {
1159
+ try {
1160
+ boolean commanate = false;
1161
+ int length = this.size();
1162
+ writer.write('[');
1163
+
1164
+ // Use -1 to signify 'no indent'
1165
+ int thisFactor = (indentFactor == -1) ? 0 : indentFactor;
1166
+
1167
+ if (length == 1) {
1168
+ JSONObject.writeValue(writer, this.myArrayList.get(0),
1169
+ indentFactor, indent);
1209
1170
  // thisFactor, indent);
1210
- } else if (length != 0) {
1211
- final int newIndent = indent + thisFactor;
1212
-
1213
- for (int i = 0; i < length; i += 1) {
1214
- if (commanate) {
1215
- writer.write(',');
1216
- }
1217
- if (indentFactor != -1) {
1218
- writer.write('\n');
1219
- }
1220
- JSONObject.indent(writer, newIndent);
1171
+ } else if (length != 0) {
1172
+ final int newIndent = indent + thisFactor;
1173
+
1174
+ for (int i = 0; i < length; i += 1) {
1175
+ if (commanate) {
1176
+ writer.write(',');
1177
+ }
1178
+ if (indentFactor != -1) {
1179
+ writer.write('\n');
1180
+ }
1181
+ JSONObject.indent(writer, newIndent);
1221
1182
  // JSONObject.writeValue(writer, this.myArrayList.get(i),
1222
1183
  // thisFactor, newIndent);
1223
- JSONObject.writeValue(writer, this.myArrayList.get(i),
1224
- indentFactor, newIndent);
1225
- commanate = true;
1184
+ JSONObject.writeValue(writer, this.myArrayList.get(i),
1185
+ indentFactor, newIndent);
1186
+ commanate = true;
1187
+ }
1188
+ if (indentFactor != -1) {
1189
+ writer.write('\n');
1190
+ }
1191
+ JSONObject.indent(writer, indent);
1192
+ }
1193
+ writer.write(']');
1194
+ return writer;
1195
+ } catch (IOException e) {
1196
+ throw new RuntimeException(e);
1226
1197
  }
1227
- if (indentFactor != -1) {
1228
- writer.write('\n');
1229
- }
1230
- JSONObject.indent(writer, indent);
1231
- }
1232
- writer.write(']');
1233
- return writer;
1234
- } catch (IOException e) {
1235
- throw new RuntimeException(e);
1236
1198
  }
1237
- }
1238
-
1239
-
1240
- /**
1241
- * Make a string from the contents of this JSONArray. The
1242
- * <code>separator</code> string is inserted between each element.
1243
- * Warning: This method assumes that the data structure is acyclic.
1244
- * @param separator A string that will be inserted between the elements.
1245
- * @return a string.
1246
- * @throws RuntimeException If the array contains an invalid number.
1247
- */
1248
- public String join(String separator) {
1249
- int len = this.size();
1250
- StringBuilder sb = new StringBuilder();
1251
-
1252
- for (int i = 0; i < len; i += 1) {
1253
- if (i > 0) {
1254
- sb.append(separator);
1255
- }
1256
- sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
1199
+
1200
+ /**
1201
+ * Make a string from the contents of this JSONArray. The
1202
+ * <code>separator</code> string is inserted between each element. Warning:
1203
+ * This method assumes that the data structure is acyclic.
1204
+ *
1205
+ * @param separator A string that will be inserted between the elements.
1206
+ * @return a string.
1207
+ * @throws RuntimeException If the array contains an invalid number.
1208
+ */
1209
+ public String join(String separator) {
1210
+ int len = this.size();
1211
+ StringBuilder sb = new StringBuilder();
1212
+
1213
+ for (int i = 0; i < len; i += 1) {
1214
+ if (i > 0) {
1215
+ sb.append(separator);
1216
+ }
1217
+ sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
1218
+ }
1219
+ return sb.toString();
1257
1220
  }
1258
- return sb.toString();
1259
- }
1260
1221
  }