update comments and increase speed of merging JSONArray objects

This commit is contained in:
John J. Aylward 2020-07-29 16:13:54 -04:00
parent c175a9eb62
commit 5d828d2c0b

View File

@ -194,15 +194,16 @@ public class JSONArray implements Iterable<Object> {
/** /**
* Construct a JSONArray from another JSONArray. This is a shallow copy. * Construct a JSONArray from another JSONArray. This is a shallow copy.
* *
* @param collection * @param array
* A Collection. * A array.
*/ */
public JSONArray(JSONArray array) { public JSONArray(JSONArray array) {
if (array == null) { if (array == null) {
this.myArrayList = new ArrayList<Object>(); this.myArrayList = new ArrayList<Object>();
} else { } else {
this.myArrayList = new ArrayList<Object>(array.length()); // shallow copy directly the internal array lists as any wrapping
this.addAll(array.myArrayList); // should have been done already in the original JSONArray
this.myArrayList = new ArrayList<Object>(array.myArrayList);
} }
} }
@ -1213,7 +1214,7 @@ public class JSONArray implements Iterable<Object> {
* Put an Iterable's elements in to the JSONArray. * Put an Iterable's elements in to the JSONArray.
* *
* @param iter * @param iter
* A Collection. * An Iterable.
* @return this. * @return this.
*/ */
public JSONArray putAll(Iterable<?> iter) { public JSONArray putAll(Iterable<?> iter) {
@ -1229,7 +1230,9 @@ public class JSONArray implements Iterable<Object> {
* @return this. * @return this.
*/ */
public JSONArray putAll(JSONArray array) { public JSONArray putAll(JSONArray array) {
this.addAll(array.myArrayList); // directly copy the elements from the source array to this one
// as all wrapping should have been done already in the source.
this.myArrayList.addAll(array.myArrayList);
return this; return this;
} }
@ -1606,8 +1609,9 @@ public class JSONArray implements Iterable<Object> {
* Add an array's elements to the JSONArray. * Add an array's elements to the JSONArray.
* *
* @param array * @param array
* Array. If the parameter passed is null, or not an array, an * Array. If the parameter passed is null, or not an array,
* exception will be thrown. * JSONArray, Collection, or Iterable, an exception will be
* thrown.
* *
* @throws JSONException * @throws JSONException
* If not an array or if an array value is non-finite number. * If not an array or if an array value is non-finite number.
@ -1622,7 +1626,10 @@ public class JSONArray implements Iterable<Object> {
this.put(JSONObject.wrap(Array.get(array, i))); this.put(JSONObject.wrap(Array.get(array, i)));
} }
} else if (array instanceof JSONArray) { } else if (array instanceof JSONArray) {
this.addAll(((JSONArray)array).myArrayList); // use the built in array list `addAll` as all object
// wrapping should have been completed in the original
// JSONArray
this.myArrayList.addAll(((JSONArray)array).myArrayList);
} else if (array instanceof Collection) { } else if (array instanceof Collection) {
this.addAll((Collection<?>)array); this.addAll((Collection<?>)array);
} else if (array instanceof Iterable) { } else if (array instanceof Iterable) {