Class JsonArray
- java.lang.Object
-
- com.github.mavolin.maxon.jsonvalues.JsonArray
-
- All Implemented Interfaces:
JsonValue
,Iterable<JsonElement>
public class JsonArray extends Object implements JsonValue, Iterable<JsonElement>
AJsonArray
is a list-like representation of an JSON array. In contrast to arrays found in Java, a JSON array, is not limited to a list of objects of a specific type. This means, that the same JSON array may hold numbers and strings.
-
-
Constructor Summary
Constructors Constructor Description JsonArray()
Instantiates an emptyJsonArray
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonArray
add(int index, JsonElement jsonElement)
Adds the passedJsonElement
at the specified index.JsonArray
add(int index, JsonValue jsonValue)
Adds the passedJsonValue
at the specified index.JsonArray
add(int index, Boolean bool)
Adds the passedBoolean
at the specified index.JsonArray
add(int index, Character character)
Adds the passedCharacter
at the specified index.JsonArray
add(int index, Number num)
Adds the passedNumber
at the specified index.JsonArray
add(int index, String string)
Adds the passedString
at the specified index.JsonArray
add(JsonValue jsonValue)
Adds the passedJsonValue
to theJsonArray
.JsonArray
add(Boolean bool)
Adds the passedBoolean
to theJsonArray
.JsonArray
add(Character character)
Adds the passedCharacter
to theJsonArray
.JsonArray
add(Number num)
Adds the passedNumber
to theJsonArray
.JsonArray
add(String string)
Adds the passedString
to theJsonArray
.void
addAll(JsonArray jsonArray)
Adds all fields stored in the passedJsonArray
to itself.void
clear()
Removes all of the elements from thisJsonArray
(optional operation).boolean
equals(Object o)
Checks whether or not thisJsonArray
is equal to the providedObject
.BigDecimal
getAsBigDecimal(int index)
Gets thejava.math.BigDecimal
at the specified index.BigInteger
getAsBigInteger(int index)
Gets thejava.math.BigInteger
at the specified index.Boolean
getAsBoolean(int index)
Gets theBoolean
at the specified index.Byte
getAsByte(int index)
Gets theByte
at the specified index.Character
getAsCharacter(int index)
Gets theCharacter
at the specified index.Double
getAsDouble(int index)
Gets theDouble
at the specified index.Float
getAsFloat(int index)
Gets theFloat
at the specified index.Integer
getAsInteger(int index)
Gets theInteger
at the specified index.JsonArray
getAsJsonArray(int index)
Gets theJsonArray
at the specified index.JsonObject
getAsJsonObject(int index)
Gets theJsonObject
at the specified index.JsonValue
getAsJsonValue(int index)
Gets theJsonValue
at the specified index.Long
getAsLong(int index)
Gets theLong
at the specified index.Short
getAsShort(int index)
Gets theShort
at the specified index.String
getAsString(int index)
Gets theString
at the specified index.int
hashCode()
Generates a hash code that is unique to every object, that is not equal to one another, as defined byequals(Object)
, but is the same for everyJsonArray
that is equal.boolean
isEmpty()
Returnstrue
if thisJsonArray
contains no elements.Iterator<JsonElement>
iterator()
Returns an iterator over elements of typeT
.void
perform(int index, Function<JsonElement,JsonValue> function)
JsonElement
remove(int index)
Removes the element at the specified position in thisJsonArray
(optional operation).void
removeInRange(int startIndex, int endIndex)
Removes all elements in the range ofstartIndex
, inclusive, toendIndex
, exclusive.JsonElement
set(int index, JsonElement jsonElement)
Replaces the element at the specified index with the passedJsonElement
.JsonElement
set(int index, JsonValue jsonValue)
Replaces the element at the specified index with the passedJsonValue
.JsonElement
set(int index, Boolean bool)
Replaces the element at the specified index with the passedBoolean
.JsonElement
set(int index, Character character)
Replaces the element at the specified index with the passedCharacter
.JsonElement
set(int index, Number num)
Replaces the element at the specified index with the passedNumber
.JsonElement
set(int index, String string)
Replaces the element at the specified index with the passedString
.int
size()
Returns the number of elements in thisJsonArray
.Stream<JsonElement>
stream()
Returns a sequentialStream
of the elements of thisJsonArray
.String
toString()
Returns aString
representation of thisJsonArray
.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
add
public JsonArray add(Boolean bool)
Adds the passedBoolean
to theJsonArray
.- Parameters:
bool
- theBoolean
that is to be added- Returns:
- itself
-
add
public JsonArray add(Character character)
Adds the passedCharacter
to theJsonArray
.- Parameters:
character
- theCharacter
that is to be added- Returns:
- itself
-
add
public JsonArray add(Number num)
Adds the passedNumber
to theJsonArray
.- Parameters:
num
- theNumber
that is to be added- Returns:
- itself
-
add
public JsonArray add(String string)
Adds the passedString
to theJsonArray
.- Parameters:
string
- theString
that is to be added- Returns:
- itself
-
add
public JsonArray add(JsonValue jsonValue)
Adds the passedJsonValue
to theJsonArray
.- Parameters:
jsonValue
- theJsonValue
that is to be added- Returns:
- itself
-
add
public JsonArray add(int index, Boolean bool)
Adds the passedBoolean
at the specified index. This results in all other elements for whichelement.index >= index
would be true, to be shifted one index up.
-
add
public JsonArray add(int index, Character character)
Adds the passedCharacter
at the specified index. This results in all other elements for whichelement.index >= index
would be true, to be shifted one index up.
-
add
public JsonArray add(int index, Number num)
Adds the passedNumber
at the specified index. This results in all other elements for whichelement.index >= index
would be true, to be shifted one index up.
-
add
public JsonArray add(int index, String string)
Adds the passedString
at the specified index. This results in all other elements for whichelement.index >= index
would be true, to be shifted one index up.
-
add
public JsonArray add(int index, JsonValue jsonValue)
Adds the passedJsonValue
at the specified index. This results in all other elements for whichelement.index >= index
would be true, to be shifted one index up.
-
add
public JsonArray add(int index, JsonElement jsonElement)
Adds the passedJsonElement
at the specified index. This results in all other elements for whichelement.index >= index
would be true, to be shifted one index up.- Parameters:
index
- the index theJsonElement
is to be added tojsonElement
- theJsonElement
that is to be added- Returns:
- itself
-
addAll
public void addAll(JsonArray jsonArray)
Adds all fields stored in the passedJsonArray
to itself.- Parameters:
jsonArray
- theJsonArray
-
set
public JsonElement set(int index, Boolean bool)
Replaces the element at the specified index with the passedBoolean
. Returns the element that was previously saved at the specified index.- Parameters:
index
- the indexbool
- theBoolean
that is to replace the element at the specified index- Returns:
- a
JsonElement
representing the element that was previously stored at the specified index
-
set
public JsonElement set(int index, Character character)
Replaces the element at the specified index with the passedCharacter
. Returns the element that was previously saved at the specified index.- Parameters:
index
- the indexcharacter
- theCharacter
that is to replace the element at the specified index- Returns:
- a
JsonElement
representing the element that was previously stored at the specified index
-
set
public JsonElement set(int index, Number num)
Replaces the element at the specified index with the passedNumber
. Returns the element that was previously saved at the specified index.- Parameters:
index
- the indexnum
- theNumber
that is to replace the element at the specified index- Returns:
- a
JsonElement
representing the element that was previously stored at the specified index
-
set
public JsonElement set(int index, String string)
Replaces the element at the specified index with the passedString
. Returns the element that was previously saved at the specified index.- Parameters:
index
- the indexstring
- theString
that is to replace the element at the specified index- Returns:
- a
JsonElement
representing the element that was previously stored at the specified index
-
set
public JsonElement set(int index, JsonValue jsonValue)
Replaces the element at the specified index with the passedJsonValue
. Returns the element that was previously saved at the specified index.- Parameters:
index
- the indexjsonValue
- theJsonValue
that is to replace the element at the specified index- Returns:
- a
JsonElement
representing the element that was previously stored at the specified index
-
set
public JsonElement set(int index, JsonElement jsonElement)
Replaces the element at the specified index with the passedJsonElement
. Returns the element that was previously saved at the specified index.- Parameters:
index
- the indexjsonElement
- theJsonElement
that is to replace the element at the specified index- Returns:
- a
JsonElement
representing the element that was previously stored at the specified index
-
getAsBoolean
public Boolean getAsBoolean(int index)
Gets theBoolean
at the specified index.- Parameters:
index
- the index of the requested element- Returns:
- the
Boolean
- Throws:
IllegalTypeRequestedException
- if the element at the specified index is not an instance ofBoolean
-
getAsCharacter
public Character getAsCharacter(int index)
Gets theCharacter
at the specified index.- Parameters:
index
- the index of the requested element- Returns:
- the
Character
- Throws:
IllegalTypeRequestedException
- if the element at the specified index is not an instance ofCharacter
-
getAsByte
public Byte getAsByte(int index)
Gets theByte
at the specified index.- Parameters:
index
- the index of the requested element- Returns:
- the
Byte
- Throws:
IllegalTypeRequestedException
- if the element at the specified index is not an instance ofByte
-
getAsShort
public Short getAsShort(int index)
Gets theShort
at the specified index.- Parameters:
index
- the index of the requested element- Returns:
- the
Short
- Throws:
IllegalTypeRequestedException
- if the element at the specified index is not an instance ofShort
-
getAsInteger
public Integer getAsInteger(int index)
Gets theInteger
at the specified index.- Parameters:
index
- the index of the requested element- Returns:
- the
Integer
- Throws:
IllegalTypeRequestedException
- if the element at the specified index is not an instance ofInteger
-
getAsLong
public Long getAsLong(int index)
Gets theLong
at the specified index.- Parameters:
index
- the index of the requested element- Returns:
- the
Long
- Throws:
IllegalTypeRequestedException
- if the element at the specified index is not an instance ofLong
-
getAsBigInteger
public BigInteger getAsBigInteger(int index)
Gets thejava.math.BigInteger
at the specified index.- Parameters:
index
- the index of the requested element- Returns:
- the
java.math.BigInteger
- Throws:
IllegalTypeRequestedException
- if the element at the specified index is not an instance ofBigInteger
-
getAsFloat
public Float getAsFloat(int index)
Gets theFloat
at the specified index.- Parameters:
index
- the index of the requested element- Returns:
- the
Float
- Throws:
IllegalTypeRequestedException
- if the element at the specified index is not an instance ofFloat
-
getAsDouble
public Double getAsDouble(int index)
Gets theDouble
at the specified index.- Parameters:
index
- the index of the requested element- Returns:
- the
Double
- Throws:
IllegalTypeRequestedException
- if the element at the specified index is not an instance ofDouble
-
getAsBigDecimal
public BigDecimal getAsBigDecimal(int index)
Gets thejava.math.BigDecimal
at the specified index.- Parameters:
index
- the index of the requested element- Returns:
- the
java.math.BigDecimal
- Throws:
IllegalTypeRequestedException
- if the element at the specified index is not an instance ofjava.math.BigDecimal
-
getAsString
public String getAsString(int index)
Gets theString
at the specified index.- Parameters:
index
- the index of the requested element- Returns:
- the
String
- Throws:
IllegalTypeRequestedException
- if the element at the specified index is not an instance ofString
-
getAsJsonArray
public JsonArray getAsJsonArray(int index)
Gets theJsonArray
at the specified index.- Parameters:
index
- the index of the requested element- Returns:
- the
JsonArray
- Throws:
IllegalTypeRequestedException
- if the element at the specified index is not an instance ofJsonArray
-
getAsJsonObject
public JsonObject getAsJsonObject(int index)
Gets theJsonObject
at the specified index.- Parameters:
index
- the index of the requested element- Returns:
- the
JsonObject
- Throws:
IllegalTypeRequestedException
- if the element at the specified index is not an instance ofJsonObject
-
getAsJsonValue
public JsonValue getAsJsonValue(int index)
Gets theJsonValue
at the specified index.- Parameters:
index
- the index of the requested element- Returns:
- the
JsonValue
-
remove
public JsonElement remove(int index)
Removes the element at the specified position in thisJsonArray
(optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.- Parameters:
index
- the index of the element to be removed- Returns:
- the element previously at the specified position
- Throws:
UnsupportedOperationException
- if theremove
operation is not supported by this listIndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()
)
-
removeInRange
public void removeInRange(int startIndex, int endIndex)
Removes all elements in the range ofstartIndex
, inclusive, toendIndex
, exclusive.- Parameters:
startIndex
- the start indexendIndex
- the end index
-
perform
public void perform(int index, Function<JsonElement,JsonValue> function)
Performs theFunction
for the element belonging to the specified index and replaces the old element with the result of theFunction
.- Parameters:
index
- the index of the element for which theFunction
is to be performedfunction
- the function
-
size
public int size()
Returns the number of elements in thisJsonArray
. If this list contains more thanInteger.MAX_VALUE
elements, returnsInteger.MAX_VALUE
.- Returns:
- the number of elements in this
JsonArray
-
isEmpty
public boolean isEmpty()
Returnstrue
if thisJsonArray
contains no elements.- Returns:
true
if thisJsonArray
contains no elements
-
clear
public void clear()
Removes all of the elements from thisJsonArray
(optional operation). The list will be empty after this call returns.- Throws:
UnsupportedOperationException
- if theclear
operation is not supported by thisJsonArray
-
iterator
public Iterator<JsonElement> iterator()
Returns an iterator over elements of typeT
.- Specified by:
iterator
in interfaceIterable<JsonElement>
- Returns:
- an Iterator.
-
stream
public Stream<JsonElement> stream()
Returns a sequentialStream
of the elements of thisJsonArray
.- Returns:
- the
Stream
-
equals
public boolean equals(Object o)
Checks whether or not thisJsonArray
is equal to the providedObject
.
-
hashCode
public int hashCode()
Generates a hash code that is unique to every object, that is not equal to one another, as defined byequals(Object)
, but is the same for everyJsonArray
that is equal.
-
-