| 1 | /* |
| 2 | * Copyright (C) 2008 Google Inc. |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package com.google.gson; |
| 18 | |
| 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; |
| 24 | |
| 25 | /** |
| 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. |
| 29 | * |
| 30 | * @author Inderjeet Singh |
| 31 | * @author Joel Leitch |
| 32 | */ |
| 33 | public final class JsonArray extends JsonElement implements Iterable<JsonElement> { |
| 34 | private final List<JsonElement> elements; |
| 35 | |
| 36 | /** |
| 37 | * Creates an empty JsonArray. |
| 38 | */ |
| 39 | public JsonArray() { |
| 40 | elements = new ArrayList<JsonElement>(); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | JsonArray deepCopy() { |
| 45 | JsonArray result = new JsonArray(); |
| 46 | for (JsonElement element : elements) { |
| 47 | result.add(element.deepCopy()); |
| 48 | } |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Adds the specified element to self. |
| 54 | * |
| 55 | * @param element the element that needs to be added to the array. |
| 56 | */ |
| 57 | public void add(JsonElement element) { |
| 58 | if (element == null) { |
| 59 | element = JsonNull.INSTANCE; |
| 60 | } |
| 61 | elements.add(element); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Adds all the elements of the specified array to self. |
| 66 | * |
| 67 | * @param array the array whose elements need to be added to the array. |
| 68 | */ |
| 69 | public void addAll(JsonArray array) { |
| 70 | elements.addAll(array.elements); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the number of elements in the array. |
| 75 | * |
| 76 | * @return the number of elements in the array. |
| 77 | */ |
| 78 | public int size() { |
| 79 | return elements.size(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 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. |
| 85 | * |
| 86 | * @return an iterator to navigate the elements of the array. |
| 87 | */ |
| 88 | public Iterator<JsonElement> iterator() { |
| 89 | return elements.iterator(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns the ith element of the array. |
| 94 | * |
| 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. |
| 99 | */ |
| 100 | public JsonElement get(int i) { |
| 101 | return elements.get(i); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * convenience method to get this array as a {@link Number} if it contains a single element. |
| 106 | * |
| 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. |
| 111 | */ |
| 112 | @Override |
| 113 | public Number getAsNumber() { |
| 114 | if (elements.size() == 1) { |
| 115 | return elements.get(0).getAsNumber(); |
| 116 | } |
| 117 | throw new IllegalStateException(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * convenience method to get this array as a {@link String} if it contains a single element. |
| 122 | * |
| 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. |
| 127 | */ |
| 128 | @Override |
| 129 | public String getAsString() { |
| 130 | if (elements.size() == 1) { |
| 131 | return elements.get(0).getAsString(); |
| 132 | } |
| 133 | throw new IllegalStateException(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * convenience method to get this array as a double if it contains a single element. |
| 138 | * |
| 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. |
| 143 | */ |
| 144 | @Override |
| 145 | public double getAsDouble() { |
| 146 | if (elements.size() == 1) { |
| 147 | return elements.get(0).getAsDouble(); |
| 148 | } |
| 149 | throw new IllegalStateException(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * convenience method to get this array as a {@link BigDecimal} if it contains a single element. |
| 154 | * |
| 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. |
| 159 | * @since 1.2 |
| 160 | */ |
| 161 | @Override |
| 162 | public BigDecimal getAsBigDecimal() { |
| 163 | if (elements.size() == 1) { |
| 164 | return elements.get(0).getAsBigDecimal(); |
| 165 | } |
| 166 | throw new IllegalStateException(); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * convenience method to get this array as a {@link BigInteger} if it contains a single element. |
| 171 | * |
| 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. |
| 176 | * @since 1.2 |
| 177 | */ |
| 178 | @Override |
| 179 | public BigInteger getAsBigInteger() { |
| 180 | if (elements.size() == 1) { |
| 181 | return elements.get(0).getAsBigInteger(); |
| 182 | } |
| 183 | throw new IllegalStateException(); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * convenience method to get this array as a float if it contains a single element. |
| 188 | * |
| 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. |
| 193 | */ |
| 194 | @Override |
| 195 | public float getAsFloat() { |
| 196 | if (elements.size() == 1) { |
| 197 | return elements.get(0).getAsFloat(); |
| 198 | } |
| 199 | throw new IllegalStateException(); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * convenience method to get this array as a long if it contains a single element. |
| 204 | * |
| 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. |
| 209 | */ |
| 210 | @Override |
| 211 | public long getAsLong() { |
| 212 | if (elements.size() == 1) { |
| 213 | return elements.get(0).getAsLong(); |
| 214 | } |
| 215 | throw new IllegalStateException(); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * convenience method to get this array as an integer if it contains a single element. |
| 220 | * |
| 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. |
| 225 | */ |
| 226 | @Override |
| 227 | public int getAsInt() { |
| 228 | if (elements.size() == 1) { |
| 229 | return elements.get(0).getAsInt(); |
| 230 | } |
| 231 | throw new IllegalStateException(); |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public byte getAsByte() { |
| 236 | if (elements.size() == 1) { |
| 237 | return elements.get(0).getAsByte(); |
| 238 | } |
| 239 | throw new IllegalStateException(); |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public char getAsCharacter() { |
| 244 | if (elements.size() == 1) { |
| 245 | return elements.get(0).getAsCharacter(); |
| 246 | } |
| 247 | throw new IllegalStateException(); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * convenience method to get this array as a primitive short if it contains a single element. |
| 252 | * |
| 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. |
| 257 | */ |
| 258 | @Override |
| 259 | public short getAsShort() { |
| 260 | if (elements.size() == 1) { |
| 261 | return elements.get(0).getAsShort(); |
| 262 | } |
| 263 | throw new IllegalStateException(); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * convenience method to get this array as a boolean if it contains a single element. |
| 268 | * |
| 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. |
| 273 | */ |
| 274 | @Override |
| 275 | public boolean getAsBoolean() { |
| 276 | if (elements.size() == 1) { |
| 277 | return elements.get(0).getAsBoolean(); |
| 278 | } |
| 279 | throw new IllegalStateException(); |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public boolean equals(Object o) { |
| 284 | return (o == this) || (o instanceof JsonArray && ((JsonArray) o).elements.equals(elements)); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public int hashCode() { |
| 289 | return elements.hashCode(); |
| 290 | } |
| 291 | } |