Package de.marhali.json5
Class Json5Array
- java.lang.Object
-
- de.marhali.json5.Json5Element
-
- de.marhali.json5.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 ofJson5Elements 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 supportnullvalues. Ifnullis provided as element argument to any of the methods, it is converted to aJson5Null.Json5Arrayonly implements theIterableinterface but not theListinterface. AListview of it can be obtained withasList().See the
Json5documentation for details on how to convertJson5Arrayand generally anyJson5Elementfrom and to Json5.
-
-
Field Summary
-
Fields inherited from class de.marhali.json5.Json5Element
comment
-
-
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 voidadd(Json5Element element)Adds the specified element to self.voidadd(java.lang.Boolean bool)Adds the specified boolean to self.voidadd(java.lang.Character character)Adds the specified character to self.voidadd(java.lang.Number number)Adds the specified number to self.voidadd(java.lang.Number number, int radix)voidadd(java.lang.String string)Adds the specified string to self.voidadd(java.time.Instant instant)Adds the specifiedInstantto self.voidaddAll(Json5Array array)Adds all the elements of the specified array to self.java.util.List<Json5Element>asList()Returns a mutableListview of thisJson5Array.booleancontains(Json5Element element)Returns true if this array contains the specified element.Json5ArraydeepCopy()Creates a deep copy of this element and all its children.booleanequals(java.lang.Object o)Json5Elementget(int i)Returns the i-th element of the array.java.math.BigDecimalgetAsBigDecimal()Convenience method to get this array as aBigDecimalif it contains a single element.java.math.BigIntegergetAsBigInteger()Convenience method to get this array as aBigIntegerif it contains a single element.java.lang.StringgetAsBinaryString()Convenience method to get this array as a binary number string if it contains a single element.booleangetAsBoolean()Convenience method to get this array as a boolean if it contains a single element.bytegetAsByte()Convenience method to get this array as a primitive byte if it contains a single element.doublegetAsDouble()Convenience method to get this array as a double if it contains a single element.floatgetAsFloat()Convenience method to get this array as a float if it contains a single element.java.lang.StringgetAsHexString()Convenience method to get this array as a hex number string if it contains a single element.java.time.InstantgetAsInstant()Convenience method to get this array as a Insta if it contains a single element.intgetAsInt()Convenience method to get this array as an integer if it contains a single element.Json5NullgetAsJson5Null()Convenience method to get this element as aJson5Null.longgetAsLong()Convenience method to get this array as a long if it contains a single element.java.lang.NumbergetAsNumber()Convenience method to get this array as aNumberif it contains a single element.java.lang.StringgetAsOctalString()Convenience method to get this array as a octal number string if it contains a single element.RadixNumbergetAsRadixNumber()Convenience method to get this array as aRadixNumberif it contains a single element.shortgetAsShort()Convenience method to get this array as a primitive short if it contains a single element.java.lang.StringgetAsString()Convenience method to get this array as aStringif it contains a single element.inthashCode()booleanisEmpty()Returns true if the array is empty.java.util.Iterator<Json5Element>iterator()Returns an iterator to navigate the elements of the array.Json5Elementremove(int index)Removes the element at the specified position in this array.booleanremove(Json5Element element)Removes the first occurrence of the specified element from this array, if it is present.Json5Elementset(int index, Json5Element element)Replaces the element at the specified position in this array with the specified element.intsize()Returns the number of elements in the array.-
Methods inherited from class de.marhali.json5.Json5Element
getAsJson5Array, getAsJson5Object, getAsJson5Primitive, getComment, hasComment, isJson5Array, isJson5Null, isJson5Object, isJson5Primitive, setComment, toString, toString
-
-
-
-
Method Detail
-
deepCopy
public Json5Array deepCopy()
Creates a deep copy of this element and all its children.- Specified by:
deepCopyin classJson5Element- Returns:
- Deep copy
-
add
public void add(java.time.Instant instant)
Adds the specifiedInstantto self.- Parameters:
instant- theInstantthat 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 replaceelement- 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:
iteratorin interfacejava.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- ifiis negative or greater than or equal to thesize()of the array.
-
getAsJson5Null
public Json5Null getAsJson5Null()
Description copied from class:Json5ElementConvenience method to get this element as aJson5Null. If this element is of some other type, anIllegalStateExceptionwill result. Hence it is best to use this method after ensuring that this element is of the desired type by callingJson5Element.isJson5Null()first.- Overrides:
getAsJson5Nullin classJson5Element- Returns:
- this element as a
Json5Null.
-
getAsNumber
public java.lang.Number getAsNumber()
Convenience method to get this array as aNumberif it contains a single element. This method callsJson5Element.getAsNumber()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsNumberin classJson5Element- 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 aRadixNumberif it contains a single element. This method callsJson5Element.getAsRadixNumber()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsRadixNumberin classJson5Element- 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 callsJson5Element.getAsBinaryString()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsBinaryStringin classJson5Element- 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 callsJson5Element.getAsOctalString()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsOctalStringin classJson5Element- 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 callsJson5Element.getAsHexString()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsHexStringin classJson5Element- 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 aStringif it contains a single element. This method callsJson5Element.getAsString()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsStringin classJson5Element- 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 callsJson5Element.getAsDouble()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsDoublein classJson5Element- 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 aBigDecimalif it contains a single element. This method callsJson5Element.getAsBigDecimal()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsBigDecimalin classJson5Element- Returns:
- this element as a
BigDecimalif 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 aBigIntegerif it contains a single element. This method callsJson5Element.getAsBigInteger()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsBigIntegerin classJson5Element- Returns:
- this element as a
BigIntegerif 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 callsJson5Element.getAsFloat()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsFloatin classJson5Element- 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 callsJson5Element.getAsLong()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsLongin classJson5Element- 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 callsJson5Element.getAsInt()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsIntin classJson5Element- 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 callsJson5Element.getAsByte()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsBytein classJson5Element- 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 callsJson5Element.getAsShort()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsShortin classJson5Element- 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 callsJson5Element.getAsBoolean()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsInstantin classJson5Element- 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 callsJson5Element.getAsBoolean()on the element, therefore any of the exceptions declared by that method can occur.- Overrides:
getAsBooleanin classJson5Element- 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 mutableListview of thisJson5Array. Changes to theListare visible in thisJson5Arrayand the other way around.The
Listdoes not permitnullelements. UnlikeJson5Array'snullhandling, aNullPointerExceptionis thrown when trying to addnull. UseJson5Nullfor Json5 null values.- Returns:
- mutable
Listview
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classJson5Element
-
hashCode
public int hashCode()
- Overrides:
hashCodein classJson5Element
-
-