Class DataArray

  • All Implemented Interfaces:
    java.lang.Iterable<java.lang.Object>, SerializableArray

    public class DataArray
    extends java.lang.Object
    implements java.lang.Iterable<java.lang.Object>, SerializableArray
    Represents a list of values used in communication with the Discord API.

    Throws IndexOutOfBoundsException if provided with index out of bounds.

    This class is not Thread-Safe

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      DataArray add​(java.lang.Object value)
      Appends the provided value to the end of the array.
      DataArray addAll​(java.util.Collection<?> values)
      Appends the provided values to the end of the array.
      DataArray addAll​(DataArray array)
      Appends the provided values to the end of the array.
      static DataArray empty()
      Creates a new empty DataArray, ready to be populated with values.
      static DataArray fromCollection​(java.util.Collection<?> col)
      Creates a new DataArray and populates it with the contents of the provided collection.
      static DataArray fromETF​(byte[] data)
      Parses using ExTermDecoder.
      static DataArray fromJson​(java.io.InputStream json)
      Parses a JSON Array into a DataArray instance.
      static DataArray fromJson​(java.io.Reader json)
      Parses a JSON Array into a DataArray instance.
      static DataArray fromJson​(java.lang.String json)
      Parses a JSON Array into a DataArray instance.
      DataArray getArray​(int index)
      Resolves the value at the specified index to a DataArray
      boolean getBoolean​(int index)
      Resolves the value at the specified index to a boolean.
      boolean getBoolean​(int index, boolean defaultValue)
      Resolves the value at the specified index to a boolean.
      int getInt​(int index)
      Resolves the value at the specified index to an int.
      int getInt​(int index, int defaultValue)
      Resolves the value at the specified index to an int.
      long getLong​(int index)
      Resolves the value at the specified index to a long.
      long getLong​(int index, long defaultValue)
      Resolves the value at the specified index to a long.
      DataObject getObject​(int index)
      Resolves the value at the specified index to a DataObject
      java.lang.String getString​(int index)
      Resolves the value at the specified index to a String.
      java.lang.String getString​(int index, java.lang.String defaultValue)
      Resolves the value at the specified index to a String.
      int getUnsignedInt​(int index)
      Resolves the value at the specified index to an unsigned int.
      int getUnsignedInt​(int index, int defaultValue)
      Resolves the value at the specified index to an unsigned int.
      long getUnsignedLong​(int index)
      Resolves the value at the specified index to an unsigned long.
      long getUnsignedLong​(int index, long defaultValue)
      Resolves the value at the specified index to an unsigned long.
      DataArray insert​(int index, java.lang.Object value)
      Inserts the specified value at the provided index.
      boolean isEmpty()
      Whether this array is empty
      boolean isNull​(int index)
      Whether the value at the specified index is null.
      boolean isType​(int index, DataType type)
      Whether the value at the specified index is of the specified type.
      java.util.Iterator<java.lang.Object> iterator()  
      int length()
      The length of the array.
      DataArray remove​(int index)
      Removes the value at the specified index.
      DataArray remove​(java.lang.Object value)
      Removes the specified value.
      <T> java.util.stream.Stream<T> stream​(java.util.function.BiFunction<? super DataArray,​java.lang.Integer,​? extends T> mapper)  
      DataArray toDataArray()
      Serialized DataArray for this object.
      byte[] toETF()
      Serializes this object as ETF LIST term.
      byte[] toJson()
      Serializes this object as JSON.
      java.util.List<java.lang.Object> toList()
      Converts this DataArray to a List.
      java.lang.String toPrettyString()  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Method Detail

      • empty

        @Nonnull
        public static DataArray empty()
        Creates a new empty DataArray, ready to be populated with values.
        Returns:
        An empty DataArray instance
        See Also:
        add(Object)
      • fromCollection

        @Nonnull
        public static DataArray fromCollection​(@Nonnull
                                               java.util.Collection<?> col)
        Creates a new DataArray and populates it with the contents of the provided collection.
        Parameters:
        col - The Collection
        Returns:
        A new DataArray populated with the contents of the collection
      • fromJson

        @Nonnull
        public static DataArray fromJson​(@Nonnull
                                         java.lang.String json)
        Parses a JSON Array into a DataArray instance.
        Parameters:
        json - The correctly formatted JSON Array
        Returns:
        A new DataArray instance for the provided array
        Throws:
        ParsingException - If the provided JSON is incorrectly formatted
      • fromJson

        @Nonnull
        public static DataArray fromJson​(@Nonnull
                                         java.io.InputStream json)
        Parses a JSON Array into a DataArray instance.
        Parameters:
        json - The correctly formatted JSON Array
        Returns:
        A new DataArray instance for the provided array
        Throws:
        ParsingException - If the provided JSON is incorrectly formatted or an I/O error occurred
      • fromJson

        @Nonnull
        public static DataArray fromJson​(@Nonnull
                                         java.io.Reader json)
        Parses a JSON Array into a DataArray instance.
        Parameters:
        json - The correctly formatted JSON Array
        Returns:
        A new DataArray instance for the provided array
        Throws:
        ParsingException - If the provided JSON is incorrectly formatted or an I/O error occurred
      • fromETF

        @Nonnull
        public static DataArray fromETF​(@Nonnull
                                        byte[] data)
        Parses using ExTermDecoder. The provided data must start with the correct version header (131).
        Parameters:
        data - The data to decode
        Returns:
        A DataArray instance for the provided payload
        Throws:
        java.lang.IllegalArgumentException - If the provided data is null
        ParsingException - If the provided ETF payload is incorrectly formatted or an I/O error occurred
        Since:
        4.2.1
      • isNull

        public boolean isNull​(int index)
        Whether the value at the specified index is null.
        Parameters:
        index - The index to check
        Returns:
        True, if the value at the index is null
      • isType

        public boolean isType​(int index,
                              @Nonnull
                              DataType type)
        Whether the value at the specified index is of the specified type.
        Parameters:
        index - The index to check
        type - The type to check
        Returns:
        True, if the type check is successful
        See Also:
        DataType.isType(Object)
      • length

        public int length()
        The length of the array.
        Returns:
        The length of the array
      • isEmpty

        public boolean isEmpty()
        Whether this array is empty
        Returns:
        True, if this array is empty
      • getObject

        @Nonnull
        public DataObject getObject​(int index)
        Resolves the value at the specified index to a DataObject
        Parameters:
        index - The index to resolve
        Returns:
        The resolved DataObject
        Throws:
        ParsingException - If the value is of the wrong type or missing
      • getArray

        @Nonnull
        public DataArray getArray​(int index)
        Resolves the value at the specified index to a DataArray
        Parameters:
        index - The index to resolve
        Returns:
        The resolved DataArray
        Throws:
        ParsingException - If the value is of the wrong type or null
      • getString

        @Nonnull
        public java.lang.String getString​(int index)
        Resolves the value at the specified index to a String.
        Parameters:
        index - The index to resolve
        Returns:
        The resolved String
        Throws:
        ParsingException - If the value is of the wrong type or null
      • getString

        @Contract("_, !null -> !null")
        public java.lang.String getString​(int index,
                                          @Nullable
                                          java.lang.String defaultValue)
        Resolves the value at the specified index to a String.
        Parameters:
        index - The index to resolve
        defaultValue - Alternative value to use when the value associated with the index is null
        Returns:
        The resolved String
        Throws:
        ParsingException - If the value is of the wrong type
      • getBoolean

        public boolean getBoolean​(int index)
        Resolves the value at the specified index to a boolean.
        Parameters:
        index - The index to resolve
        Returns:
        True, if the value is present and set to true. Otherwise false.
        Throws:
        ParsingException - If the value is of the wrong type
      • getBoolean

        public boolean getBoolean​(int index,
                                  boolean defaultValue)
        Resolves the value at the specified index to a boolean.
        Parameters:
        index - The index to resolve
        defaultValue - Alternative value to use when the value associated with the index is null
        Returns:
        True, if the value is present and set to true. False, if it is set to false. Otherwise defaultValue.
        Throws:
        ParsingException - If the value is of the wrong type
      • getInt

        public int getInt​(int index)
        Resolves the value at the specified index to an int.
        Parameters:
        index - The index to resolve
        Returns:
        The resolved int value
        Throws:
        ParsingException - If the value is of the wrong type
      • getInt

        public int getInt​(int index,
                          int defaultValue)
        Resolves the value at the specified index to an int.
        Parameters:
        index - The index to resolve
        defaultValue - Alternative value to use when the value associated with the index is null
        Returns:
        The resolved int value
        Throws:
        ParsingException - If the value is of the wrong type
      • getUnsignedInt

        public int getUnsignedInt​(int index)
        Resolves the value at the specified index to an unsigned int.
        Parameters:
        index - The index to resolve
        Returns:
        The resolved unsigned int value
        Throws:
        ParsingException - If the value is of the wrong type
      • getUnsignedInt

        public int getUnsignedInt​(int index,
                                  int defaultValue)
        Resolves the value at the specified index to an unsigned int.
        Parameters:
        index - The index to resolve
        defaultValue - Alternative value to use when the value associated with the index is null
        Returns:
        The resolved unsigned int value
        Throws:
        ParsingException - If the value is of the wrong type
      • getLong

        public long getLong​(int index)
        Resolves the value at the specified index to a long.
        Parameters:
        index - The index to resolve
        Returns:
        The resolved long value
        Throws:
        ParsingException - If the value is of the wrong type
      • getLong

        public long getLong​(int index,
                            long defaultValue)
        Resolves the value at the specified index to a long.
        Parameters:
        index - The index to resolve
        defaultValue - Alternative value to use when the value associated with the index is null
        Returns:
        The resolved long value
        Throws:
        ParsingException - If the value is of the wrong type
      • getUnsignedLong

        public long getUnsignedLong​(int index)
        Resolves the value at the specified index to an unsigned long.
        Parameters:
        index - The index to resolve
        Returns:
        The resolved unsigned long value
        Throws:
        ParsingException - If the value is of the wrong type
      • getUnsignedLong

        public long getUnsignedLong​(int index,
                                    long defaultValue)
        Resolves the value at the specified index to an unsigned long.
        Parameters:
        index - The index to resolve
        defaultValue - Alternative value to use when the value associated with the index is null
        Returns:
        The resolved unsigned long value
        Throws:
        ParsingException - If the value is of the wrong type
      • add

        @Nonnull
        public DataArray add​(@Nullable
                             java.lang.Object value)
        Appends the provided value to the end of the array.
        Parameters:
        value - The value to append
        Returns:
        A DataArray with the value inserted at the end
      • addAll

        @Nonnull
        public DataArray addAll​(@Nonnull
                                java.util.Collection<?> values)
        Appends the provided values to the end of the array.
        Parameters:
        values - The values to append
        Returns:
        A DataArray with the values inserted at the end
      • addAll

        @Nonnull
        public DataArray addAll​(@Nonnull
                                DataArray array)
        Appends the provided values to the end of the array.
        Parameters:
        array - The values to append
        Returns:
        A DataArray with the values inserted at the end
      • insert

        @Nonnull
        public DataArray insert​(int index,
                                @Nullable
                                java.lang.Object value)
        Inserts the specified value at the provided index.
        Parameters:
        index - The target index
        value - The value to insert
        Returns:
        A DataArray with the value inserted at the specified index
      • remove

        @Nonnull
        public DataArray remove​(int index)
        Removes the value at the specified index.
        Parameters:
        index - The target index to remove
        Returns:
        A DataArray with the value removed
      • remove

        @Nonnull
        public DataArray remove​(@Nullable
                                java.lang.Object value)
        Removes the specified value.
        Parameters:
        value - The value to remove
        Returns:
        A DataArray with the value removed
      • toJson

        @Nonnull
        public byte[] toJson()
        Serializes this object as JSON.
        Returns:
        byte array containing the JSON representation of this object
      • toETF

        @Nonnull
        public byte[] toETF()
        Serializes this object as ETF LIST term.
        Returns:
        byte array containing the encoded ETF term
        Since:
        4.2.1
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toPrettyString

        @Nonnull
        public java.lang.String toPrettyString()
      • toList

        @Nonnull
        public java.util.List<java.lang.Object> toList()
        Converts this DataArray to a List.
        Returns:
        The resulting list
      • iterator

        @Nonnull
        public java.util.Iterator<java.lang.Object> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<java.lang.Object>
      • stream

        @Nonnull
        public <T> java.util.stream.Stream<T> stream​(java.util.function.BiFunction<? super DataArray,​java.lang.Integer,​? extends T> mapper)