@@ -55,7 +55,8 @@ public JsonArray(int capacity) {
5555 }
5656
5757 /**
58- * Creates a deep copy of this element and all its children
58+ * Creates a deep copy of this element and all its children.
59+ *
5960 * @since 2.8.2
6061 */
6162 @ Override
@@ -129,6 +130,7 @@ public void addAll(JsonArray array) {
129130
130131 /**
131132 * Replaces the element at the specified position in this array with the specified element.
133+ *
132134 * @param index index of the element to replace
133135 * @param element element to be stored at the specified position
134136 * @return the element previously at the specified position
@@ -141,6 +143,7 @@ public JsonElement set(int index, JsonElement element) {
141143 /**
142144 * Removes the first occurrence of the specified element from this array, if it is present.
143145 * If the array does not contain the element, it is unchanged.
146+ *
144147 * @param element element to be removed from this array, if present
145148 * @return true if this array contained the specified element, false otherwise
146149 * @since 2.3
@@ -153,6 +156,7 @@ public boolean remove(JsonElement element) {
153156 * Removes the element at the specified position in this array. Shifts any subsequent elements
154157 * to the left (subtracts one from their indices). Returns the element that was removed from
155158 * the array.
159+ *
156160 * @param index index the index of the element to be removed
157161 * @return the element previously at the specified position
158162 * @throws IndexOutOfBoundsException if the specified index is outside the array bounds
@@ -164,6 +168,7 @@ public JsonElement remove(int index) {
164168
165169 /**
166170 * Returns true if this array contains the specified element.
171+ *
167172 * @return true if this array contains the specified element.
168173 * @param element whose presence in this array is to be tested
169174 * @since 2.3
@@ -182,9 +187,9 @@ public int size() {
182187 }
183188
184189 /**
185- * Returns true if the array is empty
190+ * Returns true if the array is empty.
186191 *
187- * @return true if the array is empty
192+ * @return true if the array is empty.
188193 */
189194 public boolean isEmpty () {
190195 return elements .isEmpty ();
@@ -202,195 +207,184 @@ public Iterator<JsonElement> iterator() {
202207 }
203208
204209 /**
205- * Returns the ith element of the array.
210+ * Returns the i-th element of the array.
206211 *
207212 * @param i the index of the element that is being sought.
208- * @return the element present at the ith index.
213+ * @return the element present at the i-th index.
209214 * @throws IndexOutOfBoundsException if i is negative or greater than or equal to the
210215 * {@link #size()} of the array.
211216 */
212217 public JsonElement get (int i ) {
213218 return elements .get (i );
214219 }
215220
221+ private JsonElement getAsSingleElement () {
222+ int size = elements .size ();
223+ if (size == 1 ) {
224+ return elements .get (0 );
225+ }
226+ throw new IllegalStateException ("Array must have size 1, but has size " + size );
227+ }
228+
216229 /**
217- * convenience method to get this array as a {@link Number} if it contains a single element.
230+ * Convenience method to get this array as a {@link Number} if it contains a single element.
231+ * This method calls {@link JsonElement#getAsNumber()} on the element, therefore any
232+ * of the exceptions declared by that method can occur.
218233 *
219- * @return get this element as a number if it is single element array.
220- * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
221- * is not a valid Number.
222- * @throws IllegalStateException if the array has more than one element.
234+ * @return this element as a number if it is single element array.
235+ * @throws IllegalStateException if the array is empty or has more than one element.
223236 */
224237 @ Override
225238 public Number getAsNumber () {
226- if (elements .size () == 1 ) {
227- return elements .get (0 ).getAsNumber ();
228- }
229- throw new IllegalStateException ();
239+ return getAsSingleElement ().getAsNumber ();
230240 }
231241
232242 /**
233- * convenience method to get this array as a {@link String} if it contains a single element.
243+ * Convenience method to get this array as a {@link String} if it contains a single element.
244+ * This method calls {@link JsonElement#getAsString()} on the element, therefore any
245+ * of the exceptions declared by that method can occur.
234246 *
235- * @return get this element as a String if it is single element array.
236- * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
237- * is not a valid String.
238- * @throws IllegalStateException if the array has more than one element.
247+ * @return this element as a String if it is single element array.
248+ * @throws IllegalStateException if the array is empty or has more than one element.
239249 */
240250 @ Override
241251 public String getAsString () {
242- if (elements .size () == 1 ) {
243- return elements .get (0 ).getAsString ();
244- }
245- throw new IllegalStateException ();
252+ return getAsSingleElement ().getAsString ();
246253 }
247254
248255 /**
249- * convenience method to get this array as a double if it contains a single element.
256+ * Convenience method to get this array as a double if it contains a single element.
257+ * This method calls {@link JsonElement#getAsDouble()} on the element, therefore any
258+ * of the exceptions declared by that method can occur.
250259 *
251- * @return get this element as a double if it is single element array.
252- * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
253- * is not a valid double.
254- * @throws IllegalStateException if the array has more than one element.
260+ * @return this element as a double if it is single element array.
261+ * @throws IllegalStateException if the array is empty or has more than one element.
255262 */
256263 @ Override
257264 public double getAsDouble () {
258- if (elements .size () == 1 ) {
259- return elements .get (0 ).getAsDouble ();
260- }
261- throw new IllegalStateException ();
265+ return getAsSingleElement ().getAsDouble ();
262266 }
263267
264268 /**
265- * convenience method to get this array as a {@link BigDecimal} if it contains a single element.
269+ * Convenience method to get this array as a {@link BigDecimal} if it contains a single element.
270+ * This method calls {@link JsonElement#getAsBigDecimal()} on the element, therefore any
271+ * of the exceptions declared by that method can occur.
266272 *
267- * @return get this element as a {@link BigDecimal} if it is single element array.
268- * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive}.
269- * @throws NumberFormatException if the element at index 0 is not a valid {@link BigDecimal}.
270- * @throws IllegalStateException if the array has more than one element.
273+ * @return this element as a {@link BigDecimal} if it is single element array.
274+ * @throws IllegalStateException if the array is empty or has more than one element.
271275 * @since 1.2
272276 */
273277 @ Override
274278 public BigDecimal getAsBigDecimal () {
275- if (elements .size () == 1 ) {
276- return elements .get (0 ).getAsBigDecimal ();
277- }
278- throw new IllegalStateException ();
279+ return getAsSingleElement ().getAsBigDecimal ();
279280 }
280281
281282 /**
282- * convenience method to get this array as a {@link BigInteger} if it contains a single element.
283+ * Convenience method to get this array as a {@link BigInteger} if it contains a single element.
284+ * This method calls {@link JsonElement#getAsBigInteger()} on the element, therefore any
285+ * of the exceptions declared by that method can occur.
283286 *
284- * @return get this element as a {@link BigInteger} if it is single element array.
285- * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive}.
286- * @throws NumberFormatException if the element at index 0 is not a valid {@link BigInteger}.
287- * @throws IllegalStateException if the array has more than one element.
287+ * @return this element as a {@link BigInteger} if it is single element array.
288+ * @throws IllegalStateException if the array is empty or has more than one element.
288289 * @since 1.2
289290 */
290291 @ Override
291292 public BigInteger getAsBigInteger () {
292- if (elements .size () == 1 ) {
293- return elements .get (0 ).getAsBigInteger ();
294- }
295- throw new IllegalStateException ();
293+ return getAsSingleElement ().getAsBigInteger ();
296294 }
297295
298296 /**
299- * convenience method to get this array as a float if it contains a single element.
297+ * Convenience method to get this array as a float if it contains a single element.
298+ * This method calls {@link JsonElement#getAsFloat()} on the element, therefore any
299+ * of the exceptions declared by that method can occur.
300300 *
301- * @return get this element as a float if it is single element array.
302- * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
303- * is not a valid float.
304- * @throws IllegalStateException if the array has more than one element.
301+ * @return this element as a float if it is single element array.
302+ * @throws IllegalStateException if the array is empty or has more than one element.
305303 */
306304 @ Override
307305 public float getAsFloat () {
308- if (elements .size () == 1 ) {
309- return elements .get (0 ).getAsFloat ();
310- }
311- throw new IllegalStateException ();
306+ return getAsSingleElement ().getAsFloat ();
312307 }
313308
314309 /**
315- * convenience method to get this array as a long if it contains a single element.
310+ * Convenience method to get this array as a long if it contains a single element.
311+ * This method calls {@link JsonElement#getAsLong()} on the element, therefore any
312+ * of the exceptions declared by that method can occur.
316313 *
317- * @return get this element as a long if it is single element array.
318- * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
319- * is not a valid long.
320- * @throws IllegalStateException if the array has more than one element.
314+ * @return this element as a long if it is single element array.
315+ * @throws IllegalStateException if the array is empty or has more than one element.
321316 */
322317 @ Override
323318 public long getAsLong () {
324- if (elements .size () == 1 ) {
325- return elements .get (0 ).getAsLong ();
326- }
327- throw new IllegalStateException ();
319+ return getAsSingleElement ().getAsLong ();
328320 }
329321
330322 /**
331- * convenience method to get this array as an integer if it contains a single element.
323+ * Convenience method to get this array as an integer if it contains a single element.
324+ * This method calls {@link JsonElement#getAsInt()} on the element, therefore any
325+ * of the exceptions declared by that method can occur.
332326 *
333- * @return get this element as an integer if it is single element array.
334- * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
335- * is not a valid integer.
336- * @throws IllegalStateException if the array has more than one element.
327+ * @return this element as an integer if it is single element array.
328+ * @throws IllegalStateException if the array is empty or has more than one element.
337329 */
338330 @ Override
339331 public int getAsInt () {
340- if (elements .size () == 1 ) {
341- return elements .get (0 ).getAsInt ();
342- }
343- throw new IllegalStateException ();
332+ return getAsSingleElement ().getAsInt ();
344333 }
345334
335+ /**
336+ * Convenience method to get this array as a primitive byte if it contains a single element.
337+ * This method calls {@link JsonElement#getAsByte()} on the element, therefore any
338+ * of the exceptions declared by that method can occur.
339+ *
340+ * @return this element as a primitive byte if it is single element array.
341+ * @throws IllegalStateException if the array is empty or has more than one element.
342+ */
346343 @ Override
347344 public byte getAsByte () {
348- if (elements .size () == 1 ) {
349- return elements .get (0 ).getAsByte ();
350- }
351- throw new IllegalStateException ();
345+ return getAsSingleElement ().getAsByte ();
352346 }
353347
348+ /**
349+ * Convenience method to get this array as a character if it contains a single element.
350+ * This method calls {@link JsonElement#getAsCharacter()} on the element, therefore any
351+ * of the exceptions declared by that method can occur.
352+ *
353+ * @return this element as a primitive short if it is single element array.
354+ * @throws IllegalStateException if the array is empty or has more than one element.
355+ * @deprecated This method is misleading, as it does not get this element as a char but rather as
356+ * a string's first character.
357+ */
354358 @ Deprecated
355359 @ Override
356360 public char getAsCharacter () {
357- if (elements .size () == 1 ) {
358- JsonElement element = elements .get (0 );
359- return element .getAsCharacter ();
360- }
361- throw new IllegalStateException ();
361+ return getAsSingleElement ().getAsCharacter ();
362362 }
363363
364364 /**
365- * convenience method to get this array as a primitive short if it contains a single element.
365+ * Convenience method to get this array as a primitive short if it contains a single element.
366+ * This method calls {@link JsonElement#getAsShort()} on the element, therefore any
367+ * of the exceptions declared by that method can occur.
366368 *
367- * @return get this element as a primitive short if it is single element array.
368- * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
369- * is not a valid short.
370- * @throws IllegalStateException if the array has more than one element.
369+ * @return this element as a primitive short if it is single element array.
370+ * @throws IllegalStateException if the array is empty or has more than one element.
371371 */
372372 @ Override
373373 public short getAsShort () {
374- if (elements .size () == 1 ) {
375- return elements .get (0 ).getAsShort ();
376- }
377- throw new IllegalStateException ();
374+ return getAsSingleElement ().getAsShort ();
378375 }
379376
380377 /**
381- * convenience method to get this array as a boolean if it contains a single element.
378+ * Convenience method to get this array as a boolean if it contains a single element.
379+ * This method calls {@link JsonElement#getAsBoolean()} on the element, therefore any
380+ * of the exceptions declared by that method can occur.
382381 *
383- * @return get this element as a boolean if it is single element array.
384- * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
385- * is not a valid boolean.
386- * @throws IllegalStateException if the array has more than one element.
382+ * @return this element as a boolean if it is single element array.
383+ * @throws IllegalStateException if the array is empty or has more than one element.
387384 */
388385 @ Override
389386 public boolean getAsBoolean () {
390- if (elements .size () == 1 ) {
391- return elements .get (0 ).getAsBoolean ();
392- }
393- throw new IllegalStateException ();
387+ return getAsSingleElement ().getAsBoolean ();
394388 }
395389
396390 @ Override
0 commit comments