Skip to content

Commit aa0a5a7

Browse files
committed
Added putAll(Collection) and putAll(Array) methods.
1 parent 9de9743 commit aa0a5a7

1 file changed

Lines changed: 70 additions & 13 deletions

File tree

src/main/java/org/json/JSONArray.java

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,7 @@ public JSONArray(Collection<?> collection) {
173173
this.myArrayList = new ArrayList<Object>();
174174
} else {
175175
this.myArrayList = new ArrayList<Object>(collection.size());
176-
for (Object o: collection){
177-
this.myArrayList.add(JSONObject.wrap(o));
178-
}
176+
this.addAll(collection);
179177
}
180178
}
181179

@@ -193,16 +191,7 @@ public JSONArray(Collection<?> collection) {
193191
*/
194192
public JSONArray(Object array) throws JSONException {
195193
this();
196-
if (array.getClass().isArray()) {
197-
int length = Array.getLength(array);
198-
this.myArrayList.ensureCapacity(length);
199-
for (int i = 0; i < length; i += 1) {
200-
this.put(JSONObject.wrap(Array.get(array, i)));
201-
}
202-
} else {
203-
throw new JSONException(
204-
"JSONArray initial value should be a string or collection or array.");
205-
}
194+
this.addAll(array);
206195
}
207196

208197
/**
@@ -1174,6 +1163,36 @@ public JSONArray put(int index, Object value) throws JSONException {
11741163
}
11751164
return this.put(value);
11761165
}
1166+
1167+
/**
1168+
* Put or replace a collection's elements in the JSONArray.
1169+
*
1170+
* @param collection
1171+
* A Collection.
1172+
* @return this.
1173+
*/
1174+
public JSONArray putAll(Collection<?> collection) {
1175+
this.addAll(collection);
1176+
return this;
1177+
}
1178+
1179+
/**
1180+
* Put or replace an array's elements in the JSONArray.
1181+
*
1182+
* @param array
1183+
* Array. If the parameter passed is null, or not an array, an
1184+
* exception will be thrown.
1185+
* @return this.
1186+
*
1187+
* @throws JSONException
1188+
* If not an array or if an array value is non-finite number.
1189+
* @throws NullPointerException
1190+
* Thrown if the array parameter is null.
1191+
*/
1192+
public JSONArray putAll(Object array) throws JSONException {
1193+
this.addAll(array);
1194+
return this;
1195+
}
11771196

11781197
/**
11791198
* Creates a JSONPointer using an initialization string and tries to
@@ -1500,6 +1519,44 @@ public List<Object> toList() {
15001519
public boolean isEmpty() {
15011520
return this.myArrayList.isEmpty();
15021521
}
1522+
1523+
1524+
/**
1525+
* Add a collection's elements to the JSONArray.
1526+
*
1527+
* @param collection
1528+
* A Collection.
1529+
*/
1530+
private void addAll(Collection<?> collection) {
1531+
for (Object o: collection){
1532+
this.myArrayList.add(JSONObject.wrap(o));
1533+
}
1534+
}
1535+
1536+
/**
1537+
* Add an array's elements to the JSONArray.
1538+
*
1539+
* @param array
1540+
* Array. If the parameter passed is null, or not an array, an
1541+
* exception will be thrown.
1542+
*
1543+
* @throws JSONException
1544+
* If not an array or if an array value is non-finite number.
1545+
* @throws NullPointerException
1546+
* Thrown if the array parameter is null.
1547+
*/
1548+
private void addAll(Object array) throws JSONException {
1549+
if (array.getClass().isArray()) {
1550+
int length = Array.getLength(array);
1551+
this.myArrayList.ensureCapacity(length);
1552+
for (int i = 0; i < length; i += 1) {
1553+
this.put(JSONObject.wrap(Array.get(array, i)));
1554+
}
1555+
} else {
1556+
throw new JSONException(
1557+
"JSONArray initial value should be a string or collection or array.");
1558+
}
1559+
}
15031560

15041561
/**
15051562
* Create a new JSONException in a common format for incorrect conversions.

0 commit comments

Comments
 (0)