]>
iEval git - unical.git/blob - JsonArray.java
885dc20a8f219c27c63afc98f1f5aef288b57ff2
2 * Copyright (C) 2008 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.google
.gson
;
19 import java
.math
.BigDecimal
;
20 import java
.math
.BigInteger
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Iterator
;
23 import java
.util
.List
;
26 * A class representing an array type in Json. An array is a list of {@link JsonElement}s each of
27 * which can be of a different type. This is an ordered list, meaning that the order in which
28 * elements are added is preserved.
30 * @author Inderjeet Singh
33 public final class JsonArray
extends JsonElement
implements Iterable
<JsonElement
> {
34 private final List
<JsonElement
> elements
;
37 * Creates an empty JsonArray.
40 elements
= new ArrayList
<JsonElement
>();
44 JsonArray
deepCopy() {
45 JsonArray result
= new JsonArray();
46 for (JsonElement element
: elements
) {
47 result
.add(element
.deepCopy());
53 * Adds the specified element to self.
55 * @param element the element that needs to be added to the array.
57 public void add(JsonElement element
) {
58 if (element
== null) {
59 element
= JsonNull
.INSTANCE
;
61 elements
.add(element
);
65 * Adds all the elements of the specified array to self.
67 * @param array the array whose elements need to be added to the array.
69 public void addAll(JsonArray array
) {
70 elements
.addAll(array
.elements
);
74 * Returns the number of elements in the array.
76 * @return the number of elements in the array.
79 return elements
.size();
83 * Returns an iterator to navigate the elemetns of the array. Since the array is an ordered list,
84 * the iterator navigates the elements in the order they were inserted.
86 * @return an iterator to navigate the elements of the array.
88 public Iterator
<JsonElement
> iterator() {
89 return elements
.iterator();
93 * Returns the ith element of the array.
95 * @param i the index of the element that is being sought.
96 * @return the element present at the ith index.
97 * @throws IndexOutOfBoundsException if i is negative or greater than or equal to the
98 * {@link #size()} of the array.
100 public JsonElement
get(int i
) {
101 return elements
.get(i
);
105 * convenience method to get this array as a {@link Number} if it contains a single element.
107 * @return get this element as a number if it is single element array.
108 * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
109 * is not a valid Number.
110 * @throws IllegalStateException if the array has more than one element.
113 public Number
getAsNumber() {
114 if (elements
.size() == 1) {
115 return elements
.get(0).getAsNumber();
117 throw new IllegalStateException();
121 * convenience method to get this array as a {@link String} if it contains a single element.
123 * @return get this element as a String if it is single element array.
124 * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
125 * is not a valid String.
126 * @throws IllegalStateException if the array has more than one element.
129 public String
getAsString() {
130 if (elements
.size() == 1) {
131 return elements
.get(0).getAsString();
133 throw new IllegalStateException();
137 * convenience method to get this array as a double if it contains a single element.
139 * @return get this element as a double if it is single element array.
140 * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
141 * is not a valid double.
142 * @throws IllegalStateException if the array has more than one element.
145 public double getAsDouble() {
146 if (elements
.size() == 1) {
147 return elements
.get(0).getAsDouble();
149 throw new IllegalStateException();
153 * convenience method to get this array as a {@link BigDecimal} if it contains a single element.
155 * @return get this element as a {@link BigDecimal} if it is single element array.
156 * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive}.
157 * @throws NumberFormatException if the element at index 0 is not a valid {@link BigDecimal}.
158 * @throws IllegalStateException if the array has more than one element.
162 public BigDecimal
getAsBigDecimal() {
163 if (elements
.size() == 1) {
164 return elements
.get(0).getAsBigDecimal();
166 throw new IllegalStateException();
170 * convenience method to get this array as a {@link BigInteger} if it contains a single element.
172 * @return get this element as a {@link BigInteger} if it is single element array.
173 * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive}.
174 * @throws NumberFormatException if the element at index 0 is not a valid {@link BigInteger}.
175 * @throws IllegalStateException if the array has more than one element.
179 public BigInteger
getAsBigInteger() {
180 if (elements
.size() == 1) {
181 return elements
.get(0).getAsBigInteger();
183 throw new IllegalStateException();
187 * convenience method to get this array as a float if it contains a single element.
189 * @return get this element as a float if it is single element array.
190 * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
191 * is not a valid float.
192 * @throws IllegalStateException if the array has more than one element.
195 public float getAsFloat() {
196 if (elements
.size() == 1) {
197 return elements
.get(0).getAsFloat();
199 throw new IllegalStateException();
203 * convenience method to get this array as a long if it contains a single element.
205 * @return get this element as a long if it is single element array.
206 * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
207 * is not a valid long.
208 * @throws IllegalStateException if the array has more than one element.
211 public long getAsLong() {
212 if (elements
.size() == 1) {
213 return elements
.get(0).getAsLong();
215 throw new IllegalStateException();
219 * convenience method to get this array as an integer if it contains a single element.
221 * @return get this element as an integer if it is single element array.
222 * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
223 * is not a valid integer.
224 * @throws IllegalStateException if the array has more than one element.
227 public int getAsInt() {
228 if (elements
.size() == 1) {
229 return elements
.get(0).getAsInt();
231 throw new IllegalStateException();
235 public byte getAsByte() {
236 if (elements
.size() == 1) {
237 return elements
.get(0).getAsByte();
239 throw new IllegalStateException();
243 public char getAsCharacter() {
244 if (elements
.size() == 1) {
245 return elements
.get(0).getAsCharacter();
247 throw new IllegalStateException();
251 * convenience method to get this array as a primitive short if it contains a single element.
253 * @return get this element as a primitive short if it is single element array.
254 * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
255 * is not a valid short.
256 * @throws IllegalStateException if the array has more than one element.
259 public short getAsShort() {
260 if (elements
.size() == 1) {
261 return elements
.get(0).getAsShort();
263 throw new IllegalStateException();
267 * convenience method to get this array as a boolean if it contains a single element.
269 * @return get this element as a boolean if it is single element array.
270 * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
271 * is not a valid boolean.
272 * @throws IllegalStateException if the array has more than one element.
275 public boolean getAsBoolean() {
276 if (elements
.size() == 1) {
277 return elements
.get(0).getAsBoolean();
279 throw new IllegalStateException();
283 public boolean equals(Object o
) {
284 return (o
== this) || (o
instanceof JsonArray
&& ((JsonArray
) o
).elements
.equals(elements
));
288 public int hashCode() {
289 return elements
.hashCode();
This page took 0.054178 seconds and 3 git commands to generate.