Class JSArray

java.lang.Object
io.inversion.utils.JSNode
io.inversion.utils.JSArray
All Implemented Interfaces:
Iterable, Map<String,Object>

public class JSArray extends JSNode implements Iterable
Yet another JavaScript/JSON list object representation with a few superpowers.

JSArray extends JSNode and JSNode implements Map so it is impossible for JSArray to implement List but that is practically what is happening.

This builds on JSNode but instead of string based keys, the keys are integer index positions in an underlying List.

See Also:
  • Field Details

    • objects

      protected List objects
      The objects stored in this JSArray.

      This replaces the use of the JSNode.properties map from the superclass.

  • Constructor Details

    • JSArray

      public JSArray(Object... objects)
  • Method Details

    • isArray

      public boolean isArray()
      Description copied from class: JSNode
      Easy alternative to 'instanceof' to differentiate JSNode from JSArray (which subclasses JSNode).
      Overrides:
      isArray in class JSNode
      Returns:
      true
      See Also:
    • put

      public Object put(String index, Object value)
      Override of JSNode.put(String, Object) to parse index as an Integer and store the result in objects instead of JSNode.properties

      Put/set/add are basically synonyms.

      Specified by:
      put in interface Map<String,Object>
      Overrides:
      put in class JSNode
      Returns:
      the prior value at index
      See Also:
    • put

      public Object put(int index, Object value)
      Overloading of put(String, Object) for completion, really just calls set(int, Object)

      Put/set/add are basically synonyms.

      Parameters:
      index - the zero based property index to store
      value - a JSNode, JSArray, or compatible Java type
      Returns:
      the prior value at index
      See Also:
    • set

      public Object set(int index, Object value) throws ArrayIndexOutOfBoundsException
      Sets objects[index] to value expanding the size of objects as much as is required.

      Put/set/add are basically synonyms.

      Parameters:
      index - the zero based property index to store
      value - a JSNode, JSArray, or compatible Java type
      Returns:
      the prior value at index if it exists
      Throws:
      ArrayIndexOutOfBoundsException - only if index is @lt; 0
    • add

      public void add(int index, Object value)
      Overloading of set(int, Object)
      Parameters:
      index - the zero based property index to store
      value - a JSNode, JSArray, or compatible Java type
    • add

      public void add(Object value)
      Adds object to the end of objects
      Parameters:
      value - a JSNode, JSArray, or compatible Java type
    • addAll

      public void addAll(JSArray array)
      Adds all elements from array to the end of objects
      Parameters:
      array - an array of elements to add
    • remove

      public Object remove(Object index)
      Override of JSNode.remove(Object) to parse index as an Integer and remove the column from objects instead of removing the key from JSNode.properties
      Specified by:
      remove in interface Map<String,Object>
      Overrides:
      remove in class JSNode
      Parameters:
      index - a value that should parse to an integer
      Returns:
      the prior value at index if it exits
      See Also:
    • remove

      public Object remove(int index)
      Removes column index if it exists.

      Will not throw ArrayIndexOutOfBoundsException

      Parameters:
      index - the element/column to remove from objects
      Returns:
      the prior value at index if it exits
    • get

      public Object get(Object index)
      Override of JSNode.get(Object) to parse index as an Integer and pull from objects instead of JSNode.properties
      Specified by:
      get in interface Map<String,Object>
      Overrides:
      get in class JSNode
      Parameters:
      index - the zero based property index to get
    • get

      public Object get(int index)
      Gets the object at index and will not throw ArrayIndexOutOfBoundsException
      Parameters:
      index - the zero based property index to get
      Returns:
      the object at index if it exists else, null
    • getNode

      public JSNode getNode(int index) throws ClassCastException
      Convenience overloading of get(int)
      Parameters:
      index - the zero based property index to get
      Returns:
      the value at index cast to a JSNode if exists else null
      Throws:
      ClassCastException - if the object found is not a JSNode
      See Also:
    • getArray

      public JSArray getArray(int index)
      Convenience overloading of get(Object)
      Parameters:
      index - the zero based property index to get
      Returns:
      the value at index cast to a JSArray if exists else null
      Throws:
      ClassCastException - if the object found is not a JSArray
      See Also:
    • getString

      public String getString(int index)
      Convenience overloading of get(Object)
      Parameters:
      index - the zero based property index to get
      Returns:
      the value of property name stringified if it exists else null
      See Also:
    • getInt

      public int getInt(int index)
      Convenience overloading of get(Object)
      Parameters:
      index - the zero based property index to get
      Returns:
      the value at index stringified and parsed as an int if it exists else -1
      See Also:
    • getDouble

      public double getDouble(int index)
      Convenience overloading of get(Object)
      Parameters:
      index - the zero based property index to get
      Returns:
      the value at index stringified and parsed as a double if it exists else -1
      See Also:
    • getBoolean

      public boolean getBoolean(int index)
      Convenience overloading of get(int)
      Parameters:
      index - the zero based property index to get
      Returns:
      the value at index stringified and parsed as a boolean if it exists else false
      See Also:
    • keySet

      public Set<String> keySet()
      Specified by:
      keySet in interface Map<String,Object>
      Overrides:
      keySet in class JSNode
    • asList

      public List asList()
      Description copied from class: JSNode
      Returns this object as the only element in a list.

      JSArray overrides this method to return all of its elements in a list.

      This method is designed to make it super easy to iterate over all property values or array elements without having to cast or consider differences between JSNode and JSArray.

      For example:

       JSNode node = response.getJson();//don't know if this is a JSNode or JSArray
       for(Object value : node.asList())
       {
          //do something;
       }
       
      Overrides:
      asList in class JSNode
      Returns:
      A List with this node as the only value.
      See Also:
    • asArray

      public JSArray asArray()
      Description copied from class: JSNode
      Similar to #asList() but instead of returning a List, it returns a this JSNode as the only item in a JSArray.

      JSArray overrides this method to simply return 'this'.

      Overrides:
      asArray in class JSNode
      Returns:
      simply returns 'this';
      See Also:
    • asMap

      public Map asMap()
      Overrides:
      asMap in class JSNode
      Returns:
      a map of array index as strings to their value
    • contains

      public boolean contains(Object object)
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<String,Object>
      Overrides:
      isEmpty in class JSNode
      Returns:
      true if size() == 0
    • clear

      public void clear()
      Description copied from class: JSNode
      Removes all properties
      Specified by:
      clear in interface Map<String,Object>
      Overrides:
      clear in class JSNode
    • size

      public int size()
      Specified by:
      size in interface Map<String,Object>
      Overrides:
      size in class JSNode
      Returns:
      the number of properties on this node.
    • length

      public int length()
    • iterator

      public Iterator iterator()
      Specified by:
      iterator in interface Iterable
    • values

      public Collection values()
      Specified by:
      values in interface Map<String,Object>
      Overrides:
      values in class JSNode
      Returns:
      a collection of property values