Skip to content

Commit 0a281ba

Browse files
committed
Make JsonElement conversion methods more consistent and fix javadoc
1 parent 5e1005e commit 0a281ba

File tree

4 files changed

+248
-237
lines changed

4 files changed

+248
-237
lines changed

gson/src/main/java/com/google/gson/JsonArray.java

Lines changed: 97 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -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,183 @@ 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+
if (elements.size() == 1) {
223+
return elements.get(0);
224+
}
225+
throw new IllegalStateException();
226+
}
227+
216228
/**
217-
* convenience method to get this array as a {@link Number} if it contains a single element.
229+
* Convenience method to get this array as a {@link Number} if it contains a single element.
230+
* This method calls {@link JsonElement#getAsNumber()} on the element, therefore any
231+
* of the exceptions declared by that method can occur.
218232
*
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.
233+
* @return this element as a number if it is single element array.
234+
* @throws IllegalStateException if the array is empty or has more than one element.
223235
*/
224236
@Override
225237
public Number getAsNumber() {
226-
if (elements.size() == 1) {
227-
return elements.get(0).getAsNumber();
228-
}
229-
throw new IllegalStateException();
238+
return getAsSingleElement().getAsNumber();
230239
}
231240

232241
/**
233-
* convenience method to get this array as a {@link String} if it contains a single element.
242+
* Convenience method to get this array as a {@link String} if it contains a single element.
243+
* This method calls {@link JsonElement#getAsString()} on the element, therefore any
244+
* of the exceptions declared by that method can occur.
234245
*
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.
246+
* @return this element as a String if it is single element array.
247+
* @throws IllegalStateException if the array is empty or has more than one element.
239248
*/
240249
@Override
241250
public String getAsString() {
242-
if (elements.size() == 1) {
243-
return elements.get(0).getAsString();
244-
}
245-
throw new IllegalStateException();
251+
return getAsSingleElement().getAsString();
246252
}
247253

248254
/**
249-
* convenience method to get this array as a double if it contains a single element.
255+
* Convenience method to get this array as a double if it contains a single element.
256+
* This method calls {@link JsonElement#getAsDouble()} on the element, therefore any
257+
* of the exceptions declared by that method can occur.
250258
*
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.
259+
* @return this element as a double if it is single element array.
260+
* @throws IllegalStateException if the array is empty or has more than one element.
255261
*/
256262
@Override
257263
public double getAsDouble() {
258-
if (elements.size() == 1) {
259-
return elements.get(0).getAsDouble();
260-
}
261-
throw new IllegalStateException();
264+
return getAsSingleElement().getAsDouble();
262265
}
263266

264267
/**
265-
* convenience method to get this array as a {@link BigDecimal} if it contains a single element.
268+
* Convenience method to get this array as a {@link BigDecimal} if it contains a single element.
269+
* This method calls {@link JsonElement#getAsBigDecimal()} on the element, therefore any
270+
* of the exceptions declared by that method can occur.
266271
*
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.
272+
* @return this element as a {@link BigDecimal} if it is single element array.
273+
* @throws IllegalStateException if the array is empty or has more than one element.
271274
* @since 1.2
272275
*/
273276
@Override
274277
public BigDecimal getAsBigDecimal() {
275-
if (elements.size() == 1) {
276-
return elements.get(0).getAsBigDecimal();
277-
}
278-
throw new IllegalStateException();
278+
return getAsSingleElement().getAsBigDecimal();
279279
}
280280

281281
/**
282-
* convenience method to get this array as a {@link BigInteger} if it contains a single element.
282+
* Convenience method to get this array as a {@link BigInteger} if it contains a single element.
283+
* This method calls {@link JsonElement#getAsBigInteger()} on the element, therefore any
284+
* of the exceptions declared by that method can occur.
283285
*
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.
286+
* @return this element as a {@link BigInteger} if it is single element array.
287+
* @throws IllegalStateException if the array is empty or has more than one element.
288288
* @since 1.2
289289
*/
290290
@Override
291291
public BigInteger getAsBigInteger() {
292-
if (elements.size() == 1) {
293-
return elements.get(0).getAsBigInteger();
294-
}
295-
throw new IllegalStateException();
292+
return getAsSingleElement().getAsBigInteger();
296293
}
297294

298295
/**
299-
* convenience method to get this array as a float if it contains a single element.
296+
* Convenience method to get this array as a float if it contains a single element.
297+
* This method calls {@link JsonElement#getAsFloat()} on the element, therefore any
298+
* of the exceptions declared by that method can occur.
300299
*
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.
300+
* @return this element as a float if it is single element array.
301+
* @throws IllegalStateException if the array is empty or has more than one element.
305302
*/
306303
@Override
307304
public float getAsFloat() {
308-
if (elements.size() == 1) {
309-
return elements.get(0).getAsFloat();
310-
}
311-
throw new IllegalStateException();
305+
return getAsSingleElement().getAsFloat();
312306
}
313307

314308
/**
315-
* convenience method to get this array as a long if it contains a single element.
309+
* Convenience method to get this array as a long if it contains a single element.
310+
* This method calls {@link JsonElement#getAsLong()} on the element, therefore any
311+
* of the exceptions declared by that method can occur.
316312
*
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.
313+
* @return this element as a long if it is single element array.
314+
* @throws IllegalStateException if the array is empty or has more than one element.
321315
*/
322316
@Override
323317
public long getAsLong() {
324-
if (elements.size() == 1) {
325-
return elements.get(0).getAsLong();
326-
}
327-
throw new IllegalStateException();
318+
return getAsSingleElement().getAsLong();
328319
}
329320

330321
/**
331-
* convenience method to get this array as an integer if it contains a single element.
322+
* Convenience method to get this array as an integer if it contains a single element.
323+
* This method calls {@link JsonElement#getAsInt()} on the element, therefore any
324+
* of the exceptions declared by that method can occur.
332325
*
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.
326+
* @return this element as an integer if it is single element array.
327+
* @throws IllegalStateException if the array is empty or has more than one element.
337328
*/
338329
@Override
339330
public int getAsInt() {
340-
if (elements.size() == 1) {
341-
return elements.get(0).getAsInt();
342-
}
343-
throw new IllegalStateException();
331+
return getAsSingleElement().getAsInt();
344332
}
345333

334+
/**
335+
* Convenience method to get this array as a primitive byte if it contains a single element.
336+
* This method calls {@link JsonElement#getAsByte()} on the element, therefore any
337+
* of the exceptions declared by that method can occur.
338+
*
339+
* @return this element as a primitive byte if it is single element array.
340+
* @throws IllegalStateException if the array is empty or has more than one element.
341+
*/
346342
@Override
347343
public byte getAsByte() {
348-
if (elements.size() == 1) {
349-
return elements.get(0).getAsByte();
350-
}
351-
throw new IllegalStateException();
344+
return getAsSingleElement().getAsByte();
352345
}
353346

347+
/**
348+
* Convenience method to get this array as a character if it contains a single element.
349+
* This method calls {@link JsonElement#getAsCharacter()} on the element, therefore any
350+
* of the exceptions declared by that method can occur.
351+
*
352+
* @return this element as a primitive short if it is single element array.
353+
* @throws IllegalStateException if the array is empty or has more than one element.
354+
* @deprecated This method is misleading, as it does not get this element as a char but rather as
355+
* a string's first character.
356+
*/
354357
@Deprecated
355358
@Override
356359
public char getAsCharacter() {
357-
if (elements.size() == 1) {
358-
JsonElement element = elements.get(0);
359-
return element.getAsCharacter();
360-
}
361-
throw new IllegalStateException();
360+
return getAsSingleElement().getAsCharacter();
362361
}
363362

364363
/**
365-
* convenience method to get this array as a primitive short if it contains a single element.
364+
* Convenience method to get this array as a primitive short if it contains a single element.
365+
* This method calls {@link JsonElement#getAsShort()} on the element, therefore any
366+
* of the exceptions declared by that method can occur.
366367
*
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.
368+
* @return this element as a primitive short if it is single element array.
369+
* @throws IllegalStateException if the array is empty or has more than one element.
371370
*/
372371
@Override
373372
public short getAsShort() {
374-
if (elements.size() == 1) {
375-
return elements.get(0).getAsShort();
376-
}
377-
throw new IllegalStateException();
373+
return getAsSingleElement().getAsShort();
378374
}
379375

380376
/**
381-
* convenience method to get this array as a boolean if it contains a single element.
377+
* Convenience method to get this array as a boolean if it contains a single element.
378+
* This method calls {@link JsonElement#getAsBoolean()} on the element, therefore any
379+
* of the exceptions declared by that method can occur.
382380
*
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.
381+
* @return this element as a boolean if it is single element array.
382+
* @throws IllegalStateException if the array is empty or has more than one element.
387383
*/
388384
@Override
389385
public boolean getAsBoolean() {
390-
if (elements.size() == 1) {
391-
return elements.get(0).getAsBoolean();
392-
}
393-
throw new IllegalStateException();
386+
return getAsSingleElement().getAsBoolean();
394387
}
395388

396389
@Override

0 commit comments

Comments
 (0)