Class JsonArray

java.lang.Object
net.luis.utils.io.data.json.JsonArray
All Implemented Interfaces:
Iterable<JsonElement>, JsonElement

public class JsonArray extends Object implements JsonElement, Iterable<JsonElement>
Represents a json array.
A json array is an ordered collection of values.
The values can be of any type, including json null.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final List<JsonElement>
    The internal linked list of json elements.
    The elements are stored in the order they were added.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty json array.
    JsonArray(@NotNull List<? extends JsonElement> elements)
    Constructs a json array with the given list of json elements.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(boolean value)
    Adds the given boolean to this json array.
    The boolean will be converted to a json primitive element.
    void
    add(byte value)
    Adds the given byte to this json array.
    The byte will be converted to a json primitive element.
    void
    add(double value)
    Adds the given double to this json array.
    The double will be converted to a json primitive element.
    void
    add(float value)
    Adds the given float to this json array.
    The float will be converted to a json primitive element.
    void
    add(int value)
    Adds the given int to this json array.
    The int will be converted to a json primitive element.
    void
    add(long value)
    Adds the given long to this json array.
    The long will be converted to a json primitive element.
    void
    add(short value)
    Adds the given short to this json array.
    The short will be converted to a json primitive element.
    void
    add(@Nullable Number value)
    Adds the given number to this json array.
    The number will be converted to a json primitive element.
    If the number is null, it will be replaced with a json null element.
    void
    add(@Nullable String value)
    Adds the given string to this json array.
    The string will be converted to a json primitive element.
    If the string is null, it will be replaced with a json null element.
    void
    add(@Nullable JsonElement json)
    Adds the given json element to this json array.
    If the json element is null, it will be replaced with a json null element.
    void
    addAll(@NotNull List<? extends JsonElement> elements)
    Adds all json elements from the given list to this json array.
    void
    addAll(@NotNull JsonArray array)
    Adds all json elements from the given json array to this json array.
    void
    addAll(JsonElement @NotNull ... elements)
    Adds all json elements from the given array to this json array.
    void
    Removes all elements from this json array.
    boolean
    contains(@Nullable JsonElement json)
    Checks if this json array contains the given json element.
    @NotNull @Unmodifiable Collection<JsonElement>
    Returns an unmodifiable collection of the json elements in this json array.
    boolean
     
    @NotNull JsonElement
    get(int index)
    Gets the json element at the given index from this json array.
    boolean
    getAsBoolean(int index)
    Gets the json element at the given index from this json array as a boolean.
    The element will be converted to a json primitive and then to a boolean.
    byte
    getAsByte(int index)
    Gets the json element at the given index from this json array as a byte.
    The element will be converted to a json primitive and then to a byte.
    double
    getAsDouble(int index)
    Gets the json element at the given index from this json array as a double.
    The element will be converted to a json primitive and then to a double.
    float
    getAsFloat(int index)
    Gets the json element at the given index from this json array as a float.
    The element will be converted to a json primitive and then to a float.
    int
    getAsInteger(int index)
    Gets the json element at the given index from this json array as an int.
    The element will be converted to a json primitive and then to an int.
    @NotNull JsonArray
    getAsJsonArray(int index)
    Gets the json element at the given index from this json array as a json array.
    @NotNull JsonObject
    getAsJsonObject(int index)
    Gets the json element at the given index from this json array as a json object.
    @NotNull JsonPrimitive
    getAsJsonPrimitive(int index)
    Gets the json element at the given index from this json array as a json primitive.
    long
    getAsLong(int index)
    Gets the json element at the given index from this json array as a long.
    The element will be converted to a json primitive and then to a long.
    @NotNull Number
    getAsNumber(int index)
    Gets the json element at the given index from this json array as a number.
    The element will be converted to a json primitive and then to a number.
    short
    getAsShort(int index)
    Gets the json element at the given index from this json array as a short.
    The element will be converted to a json primitive and then to a short.
    @NotNull String
    getAsString(int index)
    Gets the json element at the given index from this json array as a string.
    The element will be converted to a json primitive and then to a string.
    @NotNull @Unmodifiable List<JsonElement>
    Returns an unmodifiable list of the json elements in this json array.
    int
     
    boolean
    Checks if this json array is empty.
    Returns an iterator over the elements in this json array.
    @NotNull JsonElement
    remove(int index)
    Removes the element at the given index from this json array.
    boolean
    remove(@Nullable JsonElement json)
    Removes the given json element from this json array.
    @NotNull JsonElement
    set(int index, boolean value)
    Sets the element at the given index to the given boolean.
    The boolean will be converted to a json primitive element.
    @NotNull JsonElement
    set(int index, byte value)
    Sets the element at the given index to the given byte.
    The byte will be converted to a json primitive element.
    @NotNull JsonElement
    set(int index, double value)
    Sets the element at the given index to the given double.
    The double will be converted to a json primitive element.
    @NotNull JsonElement
    set(int index, float value)
    Sets the element at the given index to the given float.
    The float will be converted to a json primitive element.
    @NotNull JsonElement
    set(int index, int value)
    Sets the element at the given index to the given int.
    The int will be converted to a json primitive element.
    @NotNull JsonElement
    set(int index, long value)
    Sets the element at the given index to the given long.
    The long will be converted to a json primitive element.
    @NotNull JsonElement
    set(int index, short value)
    Sets the element at the given index to the given short.
    The short will be converted to a json primitive element.
    @NotNull JsonElement
    set(int index, @Nullable Number value)
    Sets the element at the given index to the given number.
    The number will be converted to a json primitive element.
    If the number is null, it will be replaced with a json null element.
    @NotNull JsonElement
    set(int index, @Nullable String value)
    Sets the element at the given index to the given string.
    The string will be converted to a json primitive element.
    If the string is null, it will be replaced with a json null element.
    @NotNull JsonElement
    set(int index, @Nullable JsonElement json)
    Sets the element at the given index to the given json element.
    If the json element is null, it will be replaced with a json null element.
    int
    Returns the number of elements in this json array.
     
    @NotNull String
    toString(@NotNull JsonConfig config)
    Returns a string representation of this json element based on the given json config.
    The json config specifies how the json element should be formatted.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator

    Methods inherited from interface net.luis.utils.io.data.json.JsonElement

    getAsJsonArray, getAsJsonObject, getAsJsonPrimitive, isJsonArray, isJsonNull, isJsonObject, isJsonPrimitive
  • Field Details

    • elements

      private final List<JsonElement> elements
      The internal linked list of json elements.
      The elements are stored in the order they were added.
  • Constructor Details

    • JsonArray

      public JsonArray()
      Constructs an empty json array.
    • JsonArray

      public JsonArray(@NotNull @NotNull List<? extends JsonElement> elements)
      Constructs a json array with the given list of json elements.
      Parameters:
      elements - The list of json elements to add
      Throws:
      NullPointerException - If the list of json elements is null
  • Method Details

    • size

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

      public boolean isEmpty()
      Checks if this json array is empty.
      Returns:
      True if this json array is empty, false otherwise
    • contains

      public boolean contains(@Nullable @Nullable JsonElement json)
      Checks if this json array contains the given json element.
      Parameters:
      json - The json element to check for
      Returns:
      True if this json array contains the given json element, false otherwise
    • iterator

      @NotNull public @NotNull Iterator<JsonElement> iterator()
      Returns an iterator over the elements in this json array.
      Specified by:
      iterator in interface Iterable<JsonElement>
      Returns:
      The iterator over the elements
    • elements

      @NotNull public @NotNull @Unmodifiable Collection<JsonElement> elements()
      Returns an unmodifiable collection of the json elements in this json array.
      Returns:
      The collection of json elements
    • getElements

      @NotNull public @NotNull @Unmodifiable List<JsonElement> getElements()
      Returns an unmodifiable list of the json elements in this json array.
      Returns:
      The list of json elements
    • set

      @NotNull public @NotNull JsonElement set(int index, @Nullable @Nullable JsonElement json)
      Sets the element at the given index to the given json element.
      If the json element is null, it will be replaced with a json null element.
      Parameters:
      index - The index of the element to set
      json - The json element to set
      Returns:
      The previous element at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
    • set

      @NotNull public @NotNull JsonElement set(int index, @Nullable @Nullable String value)
      Sets the element at the given index to the given string.
      The string will be converted to a json primitive element.
      If the string is null, it will be replaced with a json null element.
      Parameters:
      index - The index of the element to set
      value - The string to set
      Returns:
      The previous element at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      See Also:
    • set

      @NotNull public @NotNull JsonElement set(int index, boolean value)
      Sets the element at the given index to the given boolean.
      The boolean will be converted to a json primitive element.
      Parameters:
      index - The index of the element to set
      value - The boolean to set
      Returns:
      The previous element at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      See Also:
    • set

      @NotNull public @NotNull JsonElement set(int index, @Nullable @Nullable Number value)
      Sets the element at the given index to the given number.
      The number will be converted to a json primitive element.
      If the number is null, it will be replaced with a json null element.
      Parameters:
      index - The index of the element to set
      value - The number to set
      Returns:
      The previous element at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      See Also:
    • set

      @NotNull public @NotNull JsonElement set(int index, byte value)
      Sets the element at the given index to the given byte.
      The byte will be converted to a json primitive element.
      Parameters:
      index - The index of the element to set
      value - The byte to set
      Returns:
      The previous element at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      See Also:
    • set

      @NotNull public @NotNull JsonElement set(int index, short value)
      Sets the element at the given index to the given short.
      The short will be converted to a json primitive element.
      Parameters:
      index - The index of the element to set
      value - The short to set
      Returns:
      The previous element at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      See Also:
    • set

      @NotNull public @NotNull JsonElement set(int index, int value)
      Sets the element at the given index to the given int.
      The int will be converted to a json primitive element.
      Parameters:
      index - The index of the element to set
      value - The int to set
      Returns:
      The previous element at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      See Also:
    • set

      @NotNull public @NotNull JsonElement set(int index, long value)
      Sets the element at the given index to the given long.
      The long will be converted to a json primitive element.
      Parameters:
      index - The index of the element to set
      value - The long to set
      Returns:
      The previous element at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      See Also:
    • set

      @NotNull public @NotNull JsonElement set(int index, float value)
      Sets the element at the given index to the given float.
      The float will be converted to a json primitive element.
      Parameters:
      index - The index of the element to set
      value - The float to set
      Returns:
      The previous element at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      See Also:
    • set

      @NotNull public @NotNull JsonElement set(int index, double value)
      Sets the element at the given index to the given double.
      The double will be converted to a json primitive element.
      Parameters:
      index - The index of the element to set
      value - The double to set
      Returns:
      The previous element at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      See Also:
    • add

      public void add(@Nullable @Nullable JsonElement json)
      Adds the given json element to this json array.
      If the json element is null, it will be replaced with a json null element.
      Parameters:
      json - The json element to add
    • add

      public void add(@Nullable @Nullable String value)
      Adds the given string to this json array.
      The string will be converted to a json primitive element.
      If the string is null, it will be replaced with a json null element.
      Parameters:
      value - The string to add
    • add

      public void add(boolean value)
      Adds the given boolean to this json array.
      The boolean will be converted to a json primitive element.
      Parameters:
      value - The boolean to add
    • add

      public void add(@Nullable @Nullable Number value)
      Adds the given number to this json array.
      The number will be converted to a json primitive element.
      If the number is null, it will be replaced with a json null element.
      Parameters:
      value - The number to add
    • add

      public void add(byte value)
      Adds the given byte to this json array.
      The byte will be converted to a json primitive element.
      Parameters:
      value - The byte to add
    • add

      public void add(short value)
      Adds the given short to this json array.
      The short will be converted to a json primitive element.
      Parameters:
      value - The short to add
    • add

      public void add(int value)
      Adds the given int to this json array.
      The int will be converted to a json primitive element.
      Parameters:
      value - The int to add
    • add

      public void add(long value)
      Adds the given long to this json array.
      The long will be converted to a json primitive element.
      Parameters:
      value - The long to add
    • add

      public void add(float value)
      Adds the given float to this json array.
      The float will be converted to a json primitive element.
      Parameters:
      value - The float to add
    • add

      public void add(double value)
      Adds the given double to this json array.
      The double will be converted to a json primitive element.
      Parameters:
      value - The double to add
    • addAll

      public void addAll(@NotNull @NotNull JsonArray array)
      Adds all json elements from the given json array to this json array.
      Parameters:
      array - The json array to add
      Throws:
      NullPointerException - If the json array is null
    • addAll

      public void addAll(JsonElement @NotNull ... elements)
      Adds all json elements from the given array to this json array.
      Parameters:
      elements - The array of json elements to add
    • addAll

      public void addAll(@NotNull @NotNull List<? extends JsonElement> elements)
      Adds all json elements from the given list to this json array.
      Parameters:
      elements - The list of json elements to add
      Throws:
      NullPointerException - If the list of json elements is null
    • remove

      @NotNull public @NotNull JsonElement remove(int index)
      Removes the element at the given index from this json array.
      Parameters:
      index - The index of the element to remove
      Returns:
      The removed element
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
    • remove

      public boolean remove(@Nullable @Nullable JsonElement json)
      Removes the given json element from this json array.
      Parameters:
      json - The json element to remove
      Returns:
      True if the json element was removed, false otherwise
    • clear

      public void clear()
      Removes all elements from this json array.
    • get

      @NotNull public @NotNull JsonElement get(int index)
      Gets the json element at the given index from this json array.
      Parameters:
      index - The index of the element to get
      Returns:
      The json element at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
    • getAsJsonObject

      @NotNull public @NotNull JsonObject getAsJsonObject(int index)
      Gets the json element at the given index from this json array as a json object.
      Parameters:
      index - The index of the element to get
      Returns:
      The json object at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      JsonTypeException - If the json element at the given index is not a json object
      See Also:
    • getAsJsonArray

      @NotNull public @NotNull JsonArray getAsJsonArray(int index)
      Gets the json element at the given index from this json array as a json array.
      Parameters:
      index - The index of the element to get
      Returns:
      The json array at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      JsonTypeException - If the json element at the given index is not a json array
      See Also:
    • getAsJsonPrimitive

      @NotNull public @NotNull JsonPrimitive getAsJsonPrimitive(int index)
      Gets the json element at the given index from this json array as a json primitive.
      Parameters:
      index - The index of the element to get
      Returns:
      The json primitive at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      JsonTypeException - If the json element at the given index is not a json primitive
      See Also:
    • getAsString

      @NotNull public @NotNull String getAsString(int index)
      Gets the json element at the given index from this json array as a string.
      The element will be converted to a json primitive and then to a string.
      Parameters:
      index - The index of the element to get
      Returns:
      The string at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      JsonTypeException - If the json element at the given index is not a json primitive
      See Also:
    • getAsBoolean

      public boolean getAsBoolean(int index)
      Gets the json element at the given index from this json array as a boolean.
      The element will be converted to a json primitive and then to a boolean.
      Parameters:
      index - The index of the element to get
      Returns:
      The boolean at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      JsonTypeException - If the json element at the given index is not a json primitive
      See Also:
    • getAsNumber

      @NotNull public @NotNull Number getAsNumber(int index)
      Gets the json element at the given index from this json array as a number.
      The element will be converted to a json primitive and then to a number.
      Parameters:
      index - The index of the element to get
      Returns:
      The number at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      JsonTypeException - If the json element at the given index is not a json primitive
      See Also:
    • getAsByte

      public byte getAsByte(int index)
      Gets the json element at the given index from this json array as a byte.
      The element will be converted to a json primitive and then to a byte.
      Parameters:
      index - The index of the element to get
      Returns:
      The byte at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      JsonTypeException - If the json element at the given index is not a json primitive
      See Also:
    • getAsShort

      public short getAsShort(int index)
      Gets the json element at the given index from this json array as a short.
      The element will be converted to a json primitive and then to a short.
      Parameters:
      index - The index of the element to get
      Returns:
      The short at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      JsonTypeException - If the json element at the given index is not a json primitive
      See Also:
    • getAsInteger

      public int getAsInteger(int index)
      Gets the json element at the given index from this json array as an int.
      The element will be converted to a json primitive and then to an int.
      Parameters:
      index - The index of the element to get
      Returns:
      The int at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      JsonTypeException - If the json element at the given index is not a json primitive
      See Also:
    • getAsLong

      public long getAsLong(int index)
      Gets the json element at the given index from this json array as a long.
      The element will be converted to a json primitive and then to a long.
      Parameters:
      index - The index of the element to get
      Returns:
      The long at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      JsonTypeException - If the json element at the given index is not a json primitive
      See Also:
    • getAsFloat

      public float getAsFloat(int index)
      Gets the json element at the given index from this json array as a float.
      The element will be converted to a json primitive and then to a float.
      Parameters:
      index - The index of the element to get
      Returns:
      The float at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      JsonTypeException - If the json element at the given index is not a json primitive
      See Also:
    • getAsDouble

      public double getAsDouble(int index)
      Gets the json element at the given index from this json array as a double.
      The element will be converted to a json primitive and then to a double.
      Parameters:
      index - The index of the element to get
      Returns:
      The double at the given index
      Throws:
      JsonArrayIndexOutOfBoundsException - If the index is negative or greater than the size of this json array
      JsonTypeException - If the json element at the given index is not a json primitive
      See Also:
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      @NotNull public @NotNull String toString(@NotNull @NotNull JsonConfig config)
      Description copied from interface: JsonElement
      Returns a string representation of this json element based on the given json config.
      The json config specifies how the json element should be formatted.
      Specified by:
      toString in interface JsonElement
      Parameters:
      config - The json config to use
      Returns:
      The string representation of this json element