Class JSONArray<T>

java.lang.Object
com.invirgance.convirgance.json.JSONArray<T>
Type Parameters:
T - Optional typing to apply to the array
All Implemented Interfaces:
Iterable<T>, Collection<T>, List<T>, SequencedCollection<T>

public class JSONArray<T> extends Object implements List<T>
A List implementation that supports JSON array data
Author:
jbanes
  • Constructor Details

    • JSONArray

      public JSONArray()
      Creates a new empty JSONArray.
    • JSONArray

      public JSONArray(T... value)
      Create a JSONArray from the provided values
      Parameters:
      value - one or more values to store in the JSONArray
    • JSONArray

      public JSONArray(Collection list)
      Creates a JSONArray from a List with a shallow copy of its elements.
      Parameters:
      list - The source List to copy elements from.
    • JSONArray

      public JSONArray(String json)
      Creates a JSONArray by parsing the values from a JSON key holding an array.
      Parameters:
      json - The JSON string to parse, must represent a valid JSON array.
      Throws:
      ConvirganceException - If the JSON string is invalid or cannot be parsed.
  • Method Details

    • size

      public int size()
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in interface List<T>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<T>
      Specified by:
      isEmpty in interface List<T>
    • contains

      public boolean contains(Object object)
      Specified by:
      contains in interface Collection<T>
      Specified by:
      contains in interface List<T>
    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface List<T>
    • toArray

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<T>
      Specified by:
      toArray in interface List<T>
    • toArray

      public <T> T[] toArray(T[] array)
      Specified by:
      toArray in interface Collection<T>
      Specified by:
      toArray in interface List<T>
    • add

      public boolean add(T object)
      Specified by:
      add in interface Collection<T>
      Specified by:
      add in interface List<T>
    • remove

      public boolean remove(Object object)
      Specified by:
      remove in interface Collection<T>
      Specified by:
      remove in interface List<T>
    • containsAll

      public boolean containsAll(Collection<?> collection)
      Specified by:
      containsAll in interface Collection<T>
      Specified by:
      containsAll in interface List<T>
    • addAll

      public boolean addAll(Collection<? extends T> collection)
      Specified by:
      addAll in interface Collection<T>
      Specified by:
      addAll in interface List<T>
    • removeAll

      public boolean removeAll(Collection<?> collection)
      Specified by:
      removeAll in interface Collection<T>
      Specified by:
      removeAll in interface List<T>
    • retainAll

      public boolean retainAll(Collection<?> collection)
      Specified by:
      retainAll in interface Collection<T>
      Specified by:
      retainAll in interface List<T>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<T>
      Specified by:
      clear in interface List<T>
    • addAll

      public boolean addAll(int index, Collection<? extends T> c)
      Specified by:
      addAll in interface List<T>
    • get

      public T get(int index)
      Specified by:
      get in interface List<T>
    • getInt

      public int getInt(int index) throws ConvirganceException
      Gets the value associated with the specified index as a Int. Returns value if its already an Int. Otherwise we use Integer.parseInt() and the toString() of value.
      Parameters:
      index - The array index.
      Returns:
      The index's value parsed to Int.
      Throws:
      ConvirganceException - When the index's value cannot be converted to a Int.
    • getInt

      public int getInt(int index, int defaultValue) throws ConvirganceException
      Gets the value associated with the specified index as a Int returning the provided default if the value is null. If value is null or the key doesn't exist defaultValue is returned. Otherwise if value is of type Int, it will be returned unchanged.
      Parameters:
      index - The array index.
      defaultValue - The default Int value to return if the index's value is null.
      Returns:
      The index's value parsed to Int, or defaultValue when value is null.
      Throws:
      ConvirganceException - When the index's value cannot be converted to a Int.
    • getLong

      public long getLong(int index) throws ConvirganceException
      Gets the value associated with the specified index as a Long. Returns value if its already an Long. Otherwise we use Long.parseLong() and the toString() of value.
      Parameters:
      index - The array index.
      Returns:
      The index's value parsed to Long.
      Throws:
      ConvirganceException - When the index's value cannot be converted to a Long.
    • getLong

      public long getLong(int index, long defaultValue) throws ConvirganceException
      Gets the value associated with the specified index as a Long returning the provided default if the value is null. If value is null or the index doesn't exist defaultValue is returned. Otherwise if value is of type Long, it will be returned unchanged.
      Parameters:
      index - The array index.
      defaultValue - The default Long value to return if the index's value is null.
      Returns:
      The index's value parsed to Long, or defaultValue when value is null.
      Throws:
      ConvirganceException - When the index's value cannot be converted to a Long.
    • getDouble

      public double getDouble(int index) throws ConvirganceException
      Gets the value associated with the specified key as a Double. If the index's value is a Double its returned. If the index's value is a String we pass the toString() of value to Double.parseDouble()
      Parameters:
      index - The array index.
      Returns:
      The index's value or a String coerced into a Double.
      Throws:
      ConvirganceException - When the index's value is null, or its type cannot be coerced to a Double.
    • getDouble

      public double getDouble(int index, double defaultValue) throws ConvirganceException
      Gets the value associated with the specified key as a Double returning the provided default if the value or key is null. If the index's value is a Double its returned. If the index's value is a String we pass the toString() of value to Double.parseDouble() Otherwise if the index or index's value was null, defaultValue is returned.
      Parameters:
      index - The array index.
      defaultValue - The default Double value to return if the key's value is null.
      Returns:
      The index's value parsed to Double, or defaultValue if value or key is null.
      Throws:
      ConvirganceException - When the index's value cannot be converted to a Double.
    • getBoolean

      public boolean getBoolean(int index)
      Gets the Boolean value for the data at given index. Supported types: - Boolean - String that is then parsed using Boolean.parseBoolean
      Parameters:
      index - The array index.
      Returns:
      the indexes value as a Boolean.
      Throws:
      ConvirganceException - When the indexes value cannot be converted into a Boolean. Or when the index's value is null.
    • getBoolean

      public boolean getBoolean(int index, boolean defaultValue)
      Gets the Boolean value for the data at given index. Returns the provided default if the index's value is null. Supported types: - Boolean - String that is then parsed using Boolean.parseBoolean
      Parameters:
      index - The array index.
      defaultValue - The default Boolean value to return.
      Returns:
      the indexes value as a Boolean.
      Throws:
      ConvirganceException - When the indexes value cannot be converted into a Boolean.
    • getJSONArray

      public JSONArray getJSONArray(int index)
      Gets the JSONArray at the specified index.
      Parameters:
      index - The index.
      Returns:
      The JSONArray at the given index, or null if the value is null.
      Throws:
      ConvirganceException - If the value exists but is not a JSONArray.
    • getJSONArray

      public JSONArray getJSONArray(int index, JSONArray defaultValue)
      Gets the JSONArray at the given index. Returns the provided default if the index's value is null.
      Parameters:
      index - The index.
      defaultValue - A JSONArray to return when index's value is null.
      Returns:
      The provided default when index's value is null, otherwise returns the index's value.
      Throws:
      ConvirganceException - If the value exists but is not a JSONArray.
    • getJSONObject

      public JSONObject getJSONObject(int index)
      Gets the JSONObject at the given index.
      Parameters:
      index - Position in this array to retrieve the value from.
      Returns:
      The JSONObject at the given index, or null if the value is null.
      Throws:
      ConvirganceException - When the index's value exists but is not a JSONObject.
    • getJSONObject

      public JSONObject getJSONObject(int index, JSONObject defaultValue)
      Gets the JSONObject at the specified index, returning a default value if null.
      Parameters:
      index - Position in this array to retrieve the value from.
      defaultValue - The value to return if the array element is null.
      Returns:
      The JSONObject at the given index, or defaultValue if the value is null.
      Throws:
      ConvirganceException - If the value exists but is not a JSONObject.
    • getString

      public String getString(int index)
      Gets the string value for the specified index. If the index has no value null is returned.
      Parameters:
      index - The index.
      Returns:
      null if the index value is null, otherwise the values toString() representation.
    • getString

      public String getString(int index, String defaultValue)
      Gets the string representation of the value at the specified index.
      Parameters:
      index - Position in this array to retrieve the value from.
      defaultValue - The string to return if the array element is null.
      Returns:
      The value's string representation, or defaultValue if the value is null.
    • set

      public T set(int index, T element)
      Specified by:
      set in interface List<T>
    • add

      public void add(int index, T element)
      Specified by:
      add in interface List<T>
    • remove

      public T remove(int index)
      Specified by:
      remove in interface List<T>
    • indexOf

      public int indexOf(Object o)
      Specified by:
      indexOf in interface List<T>
    • lastIndexOf

      public int lastIndexOf(Object o)
      Specified by:
      lastIndexOf in interface List<T>
    • listIterator

      public ListIterator<T> listIterator()
      Specified by:
      listIterator in interface List<T>
    • listIterator

      public ListIterator<T> listIterator(int index)
      Specified by:
      listIterator in interface List<T>
    • subList

      public List<T> subList(int fromIndex, int toIndex)
      Specified by:
      subList in interface List<T>
    • toString

      public String toString()
      Returns the string representation of the array.
      Overrides:
      toString in class Object
      Returns:
      A string.
      Throws:
      ConvirganceException - If an error occurs while 'writing' the array to a string.
    • toString

      public String toString(int indent)
      Converts this JSONArray to a formatted JSON string with specified indentation
      Parameters:
      indent - the number of spaces to indent when formatting the JSON
      Returns:
      a formatted JSON string representation of this array.
      Throws:
      ConvirganceException - if an error occurs while serializing the JSON
    • equals

      public boolean equals(Object obj)
      Checks if the provided object is also a JSONArray and has the same contents in the same order
      Specified by:
      equals in interface Collection<T>
      Specified by:
      equals in interface List<T>
      Overrides:
      equals in class Object
      Parameters:
      obj - the comparison array.
      Returns:
      true if the arrays contain exactly identical items, false otherwise
    • hashCode

      public int hashCode()
      Creates stable hash code based on the values in the array
      Specified by:
      hashCode in interface Collection<T>
      Specified by:
      hashCode in interface List<T>
      Overrides:
      hashCode in class Object
      Returns:
      a hashed representation of this object