Skip to content

Commit d088cf0

Browse files
Development for stleary#516 completed with rebased repository
- Introduced JSONObject(int) constructor. - int > Initial capacity of the underlying data structure - Test for new introduced JSONArray(int) constructor. 1. Checked with input parameter: 0 2. Checked with input parameter: positive number 3. Checked with positive number input parameter and compared length 4. If input parameter is negative number JSONException is thrown: JSONArray initial capacity cannot be negative.
1 parent 19bb6fd commit d088cf0

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ public JSONArray(Object array) throws JSONException {
205205
}
206206
}
207207

208+
/**
209+
* Construct a JSONArray with the specified initial capacity.
210+
*
211+
* @param initialCapacity
212+
* the initial capacity of the JSONArray.
213+
* @throws JSONException
214+
* If the initial capacity is negative.
215+
*/
216+
public JSONArray(int initialCapacity) throws JSONException {
217+
if (initialCapacity < 0) {
218+
throw new JSONException(
219+
"JSONArray initial capacity cannot be negative.");
220+
}
221+
this.myArrayList = new ArrayList<Object>(initialCapacity);
222+
}
223+
208224
@Override
209225
public Iterator<Object> iterator() {
210226
return this.myArrayList.iterator();

src/test/java/org/json/junit/JSONArrayTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ of this software and associated documentation files (the "Software"), to deal
2626

2727
import static org.junit.Assert.assertEquals;
2828
import static org.junit.Assert.assertFalse;
29+
import static org.junit.Assert.assertNotNull;
2930
import static org.junit.Assert.assertNull;
3031
import static org.junit.Assert.assertTrue;
3132

@@ -1119,4 +1120,24 @@ public void toList() {
11191120
assertTrue("Removing an entry should succeed", list.remove(2) != null);
11201121
assertTrue("List should have 2 elements", list.size() == 2);
11211122
}
1123+
1124+
/**
1125+
* Create a JSONArray with specified initial capacity.
1126+
* Expects an exception if the initial capacity is specified as a negative integer
1127+
*/
1128+
@Test
1129+
public void testJSONArrayInt() {
1130+
assertNotNull(new JSONArray(0));
1131+
assertNotNull(new JSONArray(5));
1132+
// Check Size -> Even though the capacity of the JSONArray can be specified using a positive
1133+
// integer but the length of JSONArray always reflects upon the items added into it.
1134+
assertEquals(0l, (long)new JSONArray(10).length());
1135+
try {
1136+
assertNotNull("Should throw an exception", new JSONArray(-1));
1137+
} catch (JSONException e) {
1138+
assertEquals("Expected an exception message",
1139+
"JSONArray initial capacity cannot be negative.",
1140+
e.getMessage());
1141+
}
1142+
}
11221143
}

0 commit comments

Comments
 (0)