github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgRpc/kmgRpcJava/java/src/com/google/gson/JsonArray.java (about)

     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     * Replaces the element at the specified position in this array with the specified element.
    75     *   Element can be null.
    76     * @param index index of the element to replace
    77     * @param element element to be stored at the specified position
    78     * @return the element previously at the specified position
    79     * @throws IndexOutOfBoundsException if the specified index is outside the array bounds
    80     */
    81    public JsonElement set(int index, JsonElement element) {
    82      return elements.set(index, element);
    83    }
    84  
    85    /**
    86     * Removes the first occurrence of the specified element from this array, if it is present.
    87     * If the array does not contain the element, it is unchanged.
    88     * @param element element to be removed from this array, if present
    89     * @return true if this array contained the specified element, false otherwise
    90     * @since 2.3
    91     */
    92    public boolean remove(JsonElement element) {
    93      return elements.remove(element);
    94    }
    95  
    96    /**
    97     * Removes the element at the specified position in this array. Shifts any subsequent elements
    98     * to the left (subtracts one from their indices). Returns the element that was removed from
    99     * the array.
   100     * @param index index the index of the element to be removed
   101     * @return the element previously at the specified position
   102     * @throws IndexOutOfBoundsException if the specified index is outside the array bounds
   103     * @since 2.3
   104     */
   105    public JsonElement remove(int index) {
   106      return elements.remove(index);
   107    }
   108  
   109    /**
   110     * Returns true if this array contains the specified element.
   111     * @return true if this array contains the specified element.
   112     * @param element whose presence in this array is to be tested
   113     * @since 2.3
   114     */
   115    public boolean contains(JsonElement element) {
   116      return elements.contains(element);
   117    }
   118  
   119    /**
   120     * Returns the number of elements in the array.
   121     *
   122     * @return the number of elements in the array.
   123     */
   124    public int size() {
   125      return elements.size();
   126    }
   127  
   128    /**
   129     * Returns an iterator to navigate the elemetns of the array. Since the array is an ordered list,
   130     * the iterator navigates the elements in the order they were inserted.
   131     *
   132     * @return an iterator to navigate the elements of the array.
   133     */
   134    public Iterator<JsonElement> iterator() {
   135      return elements.iterator();
   136    }
   137  
   138    /**
   139     * Returns the ith element of the array.
   140     *
   141     * @param i the index of the element that is being sought.
   142     * @return the element present at the ith index.
   143     * @throws IndexOutOfBoundsException if i is negative or greater than or equal to the
   144     * {@link #size()} of the array.
   145     */
   146    public JsonElement get(int i) {
   147      return elements.get(i);
   148    }
   149  
   150    /**
   151     * convenience method to get this array as a {@link Number} if it contains a single element.
   152     *
   153     * @return get this element as a number if it is single element array.
   154     * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
   155     * is not a valid Number.
   156     * @throws IllegalStateException if the array has more than one element.
   157     */
   158    @Override
   159    public Number getAsNumber() {
   160      if (elements.size() == 1) {
   161        return elements.get(0).getAsNumber();
   162      }
   163      throw new IllegalStateException();
   164    }
   165  
   166    /**
   167     * convenience method to get this array as a {@link String} if it contains a single element.
   168     *
   169     * @return get this element as a String if it is single element array.
   170     * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
   171     * is not a valid String.
   172     * @throws IllegalStateException if the array has more than one element.
   173     */
   174    @Override
   175    public String getAsString() {
   176      if (elements.size() == 1) {
   177        return elements.get(0).getAsString();
   178      }
   179      throw new IllegalStateException();
   180    }
   181  
   182    /**
   183     * convenience method to get this array as a double if it contains a single element.
   184     *
   185     * @return get this element as a double if it is single element array.
   186     * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
   187     * is not a valid double.
   188     * @throws IllegalStateException if the array has more than one element.
   189     */
   190    @Override
   191    public double getAsDouble() {
   192      if (elements.size() == 1) {
   193        return elements.get(0).getAsDouble();
   194      }
   195      throw new IllegalStateException();
   196    }
   197  
   198    /**
   199     * convenience method to get this array as a {@link BigDecimal} if it contains a single element.
   200     *
   201     * @return get this element as a {@link BigDecimal} if it is single element array.
   202     * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive}.
   203     * @throws NumberFormatException if the element at index 0 is not a valid {@link BigDecimal}.
   204     * @throws IllegalStateException if the array has more than one element.
   205     * @since 1.2
   206     */
   207    @Override
   208    public BigDecimal getAsBigDecimal() {
   209      if (elements.size() == 1) {
   210        return elements.get(0).getAsBigDecimal();
   211      }
   212      throw new IllegalStateException();
   213    }
   214  
   215    /**
   216     * convenience method to get this array as a {@link BigInteger} if it contains a single element.
   217     *
   218     * @return get this element as a {@link BigInteger} if it is single element array.
   219     * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive}.
   220     * @throws NumberFormatException if the element at index 0 is not a valid {@link BigInteger}.
   221     * @throws IllegalStateException if the array has more than one element.
   222     * @since 1.2
   223     */
   224    @Override
   225    public BigInteger getAsBigInteger() {
   226      if (elements.size() == 1) {
   227        return elements.get(0).getAsBigInteger();
   228      }
   229      throw new IllegalStateException();
   230    }
   231  
   232    /**
   233     * convenience method to get this array as a float if it contains a single element.
   234     *
   235     * @return get this element as a float 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 float.
   238     * @throws IllegalStateException if the array has more than one element.
   239     */
   240    @Override
   241    public float getAsFloat() {
   242      if (elements.size() == 1) {
   243        return elements.get(0).getAsFloat();
   244      }
   245      throw new IllegalStateException();
   246    }
   247  
   248    /**
   249     * convenience method to get this array as a long if it contains a single element.
   250     *
   251     * @return get this element as a long 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 long.
   254     * @throws IllegalStateException if the array has more than one element.
   255     */
   256    @Override
   257    public long getAsLong() {
   258      if (elements.size() == 1) {
   259        return elements.get(0).getAsLong();
   260      }
   261      throw new IllegalStateException();
   262    }
   263  
   264    /**
   265     * convenience method to get this array as an integer if it contains a single element.
   266     *
   267     * @return get this element as an integer if it is single element array.
   268     * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
   269     * is not a valid integer.
   270     * @throws IllegalStateException if the array has more than one element.
   271     */
   272    @Override
   273    public int getAsInt() {
   274      if (elements.size() == 1) {
   275        return elements.get(0).getAsInt();
   276      }
   277      throw new IllegalStateException();
   278    }
   279  
   280    @Override
   281    public byte getAsByte() {
   282      if (elements.size() == 1) {
   283        return elements.get(0).getAsByte();
   284      }
   285      throw new IllegalStateException();
   286    }
   287  
   288    @Override
   289    public char getAsCharacter() {
   290      if (elements.size() == 1) {
   291        return elements.get(0).getAsCharacter();
   292      }
   293      throw new IllegalStateException();
   294    }
   295  
   296    /**
   297     * convenience method to get this array as a primitive short if it contains a single element.
   298     *
   299     * @return get this element as a primitive short if it is single element array.
   300     * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
   301     * is not a valid short.
   302     * @throws IllegalStateException if the array has more than one element.
   303     */
   304    @Override
   305    public short getAsShort() {
   306      if (elements.size() == 1) {
   307        return elements.get(0).getAsShort();
   308      }
   309      throw new IllegalStateException();
   310    }
   311  
   312    /**
   313     * convenience method to get this array as a boolean if it contains a single element.
   314     *
   315     * @return get this element as a boolean if it is single element array.
   316     * @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
   317     * is not a valid boolean.
   318     * @throws IllegalStateException if the array has more than one element.
   319     */
   320    @Override
   321    public boolean getAsBoolean() {
   322      if (elements.size() == 1) {
   323        return elements.get(0).getAsBoolean();
   324      }
   325      throw new IllegalStateException();
   326    }
   327  
   328    @Override
   329    public boolean equals(Object o) {
   330      return (o == this) || (o instanceof JsonArray && ((JsonArray) o).elements.equals(elements));
   331    }
   332  
   333    @Override
   334    public int hashCode() {
   335      return elements.hashCode();
   336    }
   337  }