Class Json5Array

  • All Implemented Interfaces:
    java.lang.Iterable<Json5Element>

    public final class Json5Array
    extends Json5Element
    implements java.lang.Iterable<Json5Element>
    A class representing an array type in Json5. An array is a list of Json5Elements each of which can be of a different type. This is an ordered list, meaning that the order in which elements are added is preserved. This class does not support null values. If null is provided as element argument to any of the methods, it is converted to a Json5Null.

    Json5Array only implements the Iterable interface but not the List interface. A List view of it can be obtained with asList().

    See the Json5 documentation for details on how to convert Json5Array and generally any Json5Element from and to Json5.

    • Constructor Summary

      Constructors 
      Constructor Description
      Json5Array()
      Creates an empty Json5Array.
      Json5Array​(int capacity)
      Creates an empty Json5Array with the desired initial capacity.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(Json5Element element)
      Adds the specified element to self.
      void add​(java.lang.Boolean bool)
      Adds the specified boolean to self.
      void add​(java.lang.Character character)
      Adds the specified character to self.
      void add​(java.lang.Number number)
      Adds the specified number to self.
      void add​(java.lang.Number number, int radix)  
      void add​(java.lang.String string)
      Adds the specified string to self.
      void add​(java.time.Instant instant)
      Adds the specified Instant to self.
      void addAll​(Json5Array array)
      Adds all the elements of the specified array to self.
      java.util.List<Json5Element> asList()
      Returns a mutable List view of this Json5Array.
      boolean contains​(Json5Element element)
      Returns true if this array contains the specified element.
      Json5Array deepCopy()
      Creates a deep copy of this element and all its children.
      boolean equals​(java.lang.Object o)  
      Json5Element get​(int i)
      Returns the i-th element of the array.
      java.math.BigDecimal getAsBigDecimal()
      Convenience method to get this array as a BigDecimal if it contains a single element.
      java.math.BigInteger getAsBigInteger()
      Convenience method to get this array as a BigInteger if it contains a single element.
      java.lang.String getAsBinaryString()
      Convenience method to get this array as a binary number string if it contains a single element.
      boolean getAsBoolean()
      Convenience method to get this array as a boolean if it contains a single element.
      byte getAsByte()
      Convenience method to get this array as a primitive byte if it contains a single element.
      double getAsDouble()
      Convenience method to get this array as a double if it contains a single element.
      float getAsFloat()
      Convenience method to get this array as a float if it contains a single element.
      java.lang.String getAsHexString()
      Convenience method to get this array as a hex number string if it contains a single element.
      java.time.Instant getAsInstant()
      Convenience method to get this array as a Insta if it contains a single element.
      int getAsInt()
      Convenience method to get this array as an integer if it contains a single element.
      Json5Null getAsJson5Null()
      Convenience method to get this element as a Json5Null.
      long getAsLong()
      Convenience method to get this array as a long if it contains a single element.
      java.lang.Number getAsNumber()
      Convenience method to get this array as a Number if it contains a single element.
      java.lang.String getAsOctalString()
      Convenience method to get this array as a octal number string if it contains a single element.
      RadixNumber getAsRadixNumber()
      Convenience method to get this array as a RadixNumber if it contains a single element.
      short getAsShort()
      Convenience method to get this array as a primitive short if it contains a single element.
      java.lang.String getAsString()
      Convenience method to get this array as a String if it contains a single element.
      int hashCode()  
      boolean isEmpty()
      Returns true if the array is empty.
      java.util.Iterator<Json5Element> iterator()
      Returns an iterator to navigate the elements of the array.
      Json5Element remove​(int index)
      Removes the element at the specified position in this array.
      boolean remove​(Json5Element element)
      Removes the first occurrence of the specified element from this array, if it is present.
      Json5Element set​(int index, Json5Element element)
      Replaces the element at the specified position in this array with the specified element.
      int size()
      Returns the number of elements in the array.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • Json5Array

        public Json5Array()
        Creates an empty Json5Array.
      • Json5Array

        public Json5Array​(int capacity)
        Creates an empty Json5Array with the desired initial capacity.
        Parameters:
        capacity - initial capacity.
        Throws:
        java.lang.IllegalArgumentException - if the capacity is negative
    • Method Detail

      • deepCopy

        public Json5Array deepCopy()
        Creates a deep copy of this element and all its children.
        Specified by:
        deepCopy in class Json5Element
        Returns:
        Deep copy
      • add

        public void add​(java.time.Instant instant)
        Adds the specified Instant to self.
        Parameters:
        instant - the Instant that needs to be added to the array.
      • add

        public void add​(java.lang.Boolean bool)
        Adds the specified boolean to self.
        Parameters:
        bool - the boolean that needs to be added to the array.
      • add

        public void add​(java.lang.Character character)
        Adds the specified character to self.
        Parameters:
        character - the character that needs to be added to the array.
      • add

        public void add​(java.lang.Number number)
        Adds the specified number to self.
        Parameters:
        number - the number that needs to be added to the array.
      • add

        public void add​(java.lang.Number number,
                        int radix)
      • add

        public void add​(java.lang.String string)
        Adds the specified string to self.
        Parameters:
        string - the string that needs to be added to the array.
      • add

        public void add​(Json5Element element)
        Adds the specified element to self.
        Parameters:
        element - the element that needs to be added to the array.
      • addAll

        public void addAll​(Json5Array array)
        Adds all the elements of the specified array to self.
        Parameters:
        array - the array whose elements need to be added to the array.
      • set

        public Json5Element set​(int index,
                                Json5Element element)
        Replaces the element at the specified position in this array with the specified element.
        Parameters:
        index - index of the element to replace
        element - element to be stored at the specified position
        Returns:
        the element previously at the specified position
        Throws:
        java.lang.IndexOutOfBoundsException - if the specified index is outside the array bounds
      • remove

        public boolean remove​(Json5Element element)
        Removes the first occurrence of the specified element from this array, if it is present. If the array does not contain the element, it is unchanged.
        Parameters:
        element - element to be removed from this array, if present
        Returns:
        true if this array contained the specified element, false otherwise
      • remove

        public Json5Element remove​(int index)
        Removes the element at the specified position in this array. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the array.
        Parameters:
        index - index the index of the element to be removed
        Returns:
        the element previously at the specified position
        Throws:
        java.lang.IndexOutOfBoundsException - if the specified index is outside the array bounds
      • contains

        public boolean contains​(Json5Element element)
        Returns true if this array contains the specified element.
        Parameters:
        element - whose presence in this array is to be tested
        Returns:
        true if this array contains the specified element.
      • size

        public int size()
        Returns the number of elements in the array.
        Returns:
        the number of elements in the array.
      • isEmpty

        public boolean isEmpty()
        Returns true if the array is empty.
        Returns:
        true if the array is empty.
      • iterator

        public java.util.Iterator<Json5Element> iterator()
        Returns an iterator to navigate the elements of the array. Since the array is an ordered list, the iterator navigates the elements in the order they were inserted.
        Specified by:
        iterator in interface java.lang.Iterable<Json5Element>
        Returns:
        an iterator to navigate the elements of the array.
      • get

        public Json5Element get​(int i)
        Returns the i-th element of the array.
        Parameters:
        i - the index of the element that is being sought.
        Returns:
        the element present at the i-th index.
        Throws:
        java.lang.IndexOutOfBoundsException - if i is negative or greater than or equal to the size() of the array.
      • getAsNumber

        public java.lang.Number getAsNumber()
        Convenience method to get this array as a Number if it contains a single element. This method calls Json5Element.getAsNumber() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsNumber in class Json5Element
        Returns:
        this element as a number if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsRadixNumber

        public RadixNumber getAsRadixNumber()
        Convenience method to get this array as a RadixNumber if it contains a single element. This method calls Json5Element.getAsRadixNumber() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsRadixNumber in class Json5Element
        Returns:
        this element as a radix number if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsBinaryString

        public java.lang.String getAsBinaryString()
        Convenience method to get this array as a binary number string if it contains a single element. This method calls Json5Element.getAsBinaryString() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsBinaryString in class Json5Element
        Returns:
        this element as a binary number string if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsOctalString

        public java.lang.String getAsOctalString()
        Convenience method to get this array as a octal number string if it contains a single element. This method calls Json5Element.getAsOctalString() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsOctalString in class Json5Element
        Returns:
        this element as a octal number string if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsHexString

        public java.lang.String getAsHexString()
        Convenience method to get this array as a hex number string if it contains a single element. This method calls Json5Element.getAsHexString() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsHexString in class Json5Element
        Returns:
        this element as a hex number string if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsString

        public java.lang.String getAsString()
        Convenience method to get this array as a String if it contains a single element. This method calls Json5Element.getAsString() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsString in class Json5Element
        Returns:
        this element as a String if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsDouble

        public double getAsDouble()
        Convenience method to get this array as a double if it contains a single element. This method calls Json5Element.getAsDouble() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsDouble in class Json5Element
        Returns:
        this element as a double if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsBigDecimal

        public java.math.BigDecimal getAsBigDecimal()
        Convenience method to get this array as a BigDecimal if it contains a single element. This method calls Json5Element.getAsBigDecimal() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsBigDecimal in class Json5Element
        Returns:
        this element as a BigDecimal if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsBigInteger

        public java.math.BigInteger getAsBigInteger()
        Convenience method to get this array as a BigInteger if it contains a single element. This method calls Json5Element.getAsBigInteger() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsBigInteger in class Json5Element
        Returns:
        this element as a BigInteger if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsFloat

        public float getAsFloat()
        Convenience method to get this array as a float if it contains a single element. This method calls Json5Element.getAsFloat() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsFloat in class Json5Element
        Returns:
        this element as a float if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsLong

        public long getAsLong()
        Convenience method to get this array as a long if it contains a single element. This method calls Json5Element.getAsLong() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsLong in class Json5Element
        Returns:
        this element as a long if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsInt

        public int getAsInt()
        Convenience method to get this array as an integer if it contains a single element. This method calls Json5Element.getAsInt() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsInt in class Json5Element
        Returns:
        this element as an integer if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsByte

        public byte getAsByte()
        Convenience method to get this array as a primitive byte if it contains a single element. This method calls Json5Element.getAsByte() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsByte in class Json5Element
        Returns:
        this element as a primitive byte if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsShort

        public short getAsShort()
        Convenience method to get this array as a primitive short if it contains a single element. This method calls Json5Element.getAsShort() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsShort in class Json5Element
        Returns:
        this element as a primitive short if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsInstant

        public java.time.Instant getAsInstant()
        Convenience method to get this array as a Insta if it contains a single element. This method calls Json5Element.getAsBoolean() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsInstant in class Json5Element
        Returns:
        this element as a boolean if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • getAsBoolean

        public boolean getAsBoolean()
        Convenience method to get this array as a boolean if it contains a single element. This method calls Json5Element.getAsBoolean() on the element, therefore any of the exceptions declared by that method can occur.
        Overrides:
        getAsBoolean in class Json5Element
        Returns:
        this element as a boolean if it is single element array.
        Throws:
        java.lang.IllegalStateException - if the array is empty or has more than one element.
      • asList

        public java.util.List<Json5Element> asList()
        Returns a mutable List view of this Json5Array. Changes to the List are visible in this Json5Array and the other way around.

        The List does not permit null elements. Unlike Json5Array's null handling, a NullPointerException is thrown when trying to add null. Use Json5Null for Json5 null values.

        Returns:
        mutable List view
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class Json5Element