Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added putAll(Collection) and putAll(Array) methods. #529

Merged
merged 4 commits into from Jul 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 71 additions & 13 deletions src/main/java/org/json/JSONArray.java
Expand Up @@ -173,9 +173,7 @@ public JSONArray(Collection<?> collection) {
this.myArrayList = new ArrayList<Object>();
} else {
this.myArrayList = new ArrayList<Object>(collection.size());
for (Object o: collection){
this.myArrayList.add(JSONObject.wrap(o));
}
this.addAll(collection);
}
}

Expand All @@ -193,16 +191,7 @@ public JSONArray(Collection<?> collection) {
*/
public JSONArray(Object array) throws JSONException {
this();
if (array.getClass().isArray()) {
int length = Array.getLength(array);
this.myArrayList.ensureCapacity(length);
for (int i = 0; i < length; i += 1) {
this.put(JSONObject.wrap(Array.get(array, i)));
}
} else {
throw new JSONException(
"JSONArray initial value should be a string or collection or array.");
}
this.addAll(array);
}

/**
Expand Down Expand Up @@ -1174,6 +1163,36 @@ public JSONArray put(int index, Object value) throws JSONException {
}
return this.put(value);
}

/**
* Put or replace a collection's elements in the JSONArray.
*
* @param collection
* A Collection.
* @return this.
*/
public JSONArray putAll(Collection<?> collection) {
this.addAll(collection);
return this;
}

/**
* Put or replace an array's elements in the JSONArray.
*
* @param array
* Array. If the parameter passed is null, or not an array, an
* exception will be thrown.
* @return this.
*
* @throws JSONException
* If not an array or if an array value is non-finite number.
* @throws NullPointerException
* Thrown if the array parameter is null.
*/
public JSONArray putAll(Object array) throws JSONException {
this.addAll(array);
return this;
}

/**
* Creates a JSONPointer using an initialization string and tries to
Expand Down Expand Up @@ -1500,6 +1519,45 @@ public List<Object> toList() {
public boolean isEmpty() {
return this.myArrayList.isEmpty();
}


/**
* Add a collection's elements to the JSONArray.
*
* @param collection
* A Collection.
*/
private void addAll(Collection<?> collection) {
this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size());
for (Object o: collection){
ethauvin marked this conversation as resolved.
Show resolved Hide resolved
this.myArrayList.add(JSONObject.wrap(o));
}
}

/**
* Add an array's elements to the JSONArray.
*
* @param array
* Array. If the parameter passed is null, or not an array, an
* exception will be thrown.
*
* @throws JSONException
* If not an array or if an array value is non-finite number.
* @throws NullPointerException
* Thrown if the array parameter is null.
*/
private void addAll(Object array) throws JSONException {
if (array.getClass().isArray()) {
int length = Array.getLength(array);
this.myArrayList.ensureCapacity(this.myArrayList.size() + length);
for (int i = 0; i < length; i += 1) {
this.put(JSONObject.wrap(Array.get(array, i)));
}
} else {
throw new JSONException(
"JSONArray initial value should be a string or collection or array.");
}
}

/**
* Create a new JSONException in a common format for incorrect conversions.
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/org/json/junit/JSONArrayTest.java
Expand Up @@ -225,6 +225,44 @@ public void verifyConstructor() {
expected.similar(jaObj));
}

/**
* Tests consecutive calls to putAll with array and collection.
*/
@Test
public void verifyPutAll() {
final JSONArray jsonArray = new JSONArray();

// array
int[] myInts = { 1, 2, 3, 4, 5 };
jsonArray.putAll(myInts);

assertEquals("int arrays lengths should be equal",
jsonArray.length(),
myInts.length);

for (int i = 0; i < myInts.length; i++) {
assertEquals("int arrays elements should be equal",
myInts[i],
jsonArray.getInt(i));
}

// collection
List<String> myList = Arrays.asList("one", "two", "three", "four", "five");
jsonArray.putAll(myList);

int len = myInts.length + myList.size();

assertEquals("arrays lengths should be equal",
jsonArray.length(),
len);

for (int i = 0; i < myList.size(); i++) {
assertEquals("collection elements should be equal",
myList.get(i),
jsonArray.getString(myInts.length + i));
}
}

/**
* Verifies that the put Collection has backwards compatibility with RAW types pre-java5.
*/
Expand Down