Package net.dv8tion.jda.api.utils.data
Class DataArray
- java.lang.Object
-
- net.dv8tion.jda.api.utils.data.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
IndexOutOfBoundsExceptionif 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 DataArrayadd(java.lang.Object value)Appends the provided value to the end of the array.DataArrayaddAll(java.util.Collection<?> values)Appends the provided values to the end of the array.DataArrayaddAll(DataArray array)Appends the provided values to the end of the array.static DataArrayempty()Creates a new empty DataArray, ready to be populated with values.static DataArrayfromCollection(java.util.Collection<?> col)Creates a new DataArray and populates it with the contents of the provided collection.static DataArrayfromETF(byte[] data)Parses usingExTermDecoder.static DataArrayfromJson(java.io.InputStream json)Parses a JSON Array into a DataArray instance.static DataArrayfromJson(java.io.Reader json)Parses a JSON Array into a DataArray instance.static DataArrayfromJson(java.lang.String json)Parses a JSON Array into a DataArray instance.DataArraygetArray(int index)Resolves the value at the specified index to a DataArraybooleangetBoolean(int index)Resolves the value at the specified index to a boolean.booleangetBoolean(int index, boolean defaultValue)Resolves the value at the specified index to a boolean.intgetInt(int index)Resolves the value at the specified index to an int.intgetInt(int index, int defaultValue)Resolves the value at the specified index to an int.longgetLong(int index)Resolves the value at the specified index to a long.longgetLong(int index, long defaultValue)Resolves the value at the specified index to a long.DataObjectgetObject(int index)Resolves the value at the specified index to a DataObjectjava.lang.StringgetString(int index)Resolves the value at the specified index to a String.java.lang.StringgetString(int index, java.lang.String defaultValue)Resolves the value at the specified index to a String.intgetUnsignedInt(int index)Resolves the value at the specified index to an unsigned int.intgetUnsignedInt(int index, int defaultValue)Resolves the value at the specified index to an unsigned int.longgetUnsignedLong(int index)Resolves the value at the specified index to an unsigned long.longgetUnsignedLong(int index, long defaultValue)Resolves the value at the specified index to an unsigned long.DataArrayinsert(int index, java.lang.Object value)Inserts the specified value at the provided index.booleanisEmpty()Whether this array is emptybooleanisNull(int index)Whether the value at the specified index is null.booleanisType(int index, DataType type)Whether the value at the specified index is of the specified type.java.util.Iterator<java.lang.Object>iterator()intlength()The length of the array.DataArrayremove(int index)Removes the value at the specified index.DataArrayremove(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)DataArraytoDataArray()SerializedDataArrayfor 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 aList.java.lang.StringtoPrettyString()java.lang.StringtoString()
-
-
-
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- TheCollection- 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 usingExTermDecoder. 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 nullParsingException- 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 checktype- 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 resolvedefaultValue- 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 resolvedefaultValue- 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 resolvedefaultValue- 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 resolvedefaultValue- 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 resolvedefaultValue- 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 resolvedefaultValue- 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 indexvalue- 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:
toStringin classjava.lang.Object
-
toPrettyString
@Nonnull public java.lang.String toPrettyString()
-
toList
@Nonnull public java.util.List<java.lang.Object> toList()
Converts this DataArray to aList.- Returns:
- The resulting list
-
iterator
@Nonnull public java.util.Iterator<java.lang.Object> iterator()
- Specified by:
iteratorin interfacejava.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)
-
toDataArray
@Nonnull public DataArray toDataArray()
Description copied from interface:SerializableArraySerializedDataArrayfor this object.- Specified by:
toDataArrayin interfaceSerializableArray- Returns:
DataArray
-
-