java.lang.Object
jsonvalues.JsArray
Represents a json array, which is an ordered list of elements.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final JsArray
static final JsOptics.JsArrayLenses
lenses defined for a Json arraystatic final JsOptics.JsArrayOptionals
optionals defined for a Json arrayprism between the sum type JsValue and JsArray -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds one or more elements, starting from the first, to the back of this array.Adds all the elements of the given array, starting from the head, to the back of this array.boolean
containsValue
(JsValue el) Returns true if this json contains the given element in the first level.delete
(int index) Removes the element in this json located at the given path, if it exists, returning the same this instance otherwisestatic JsArray
empty()
boolean
boolean
equals
(JsArray array, JsArray.TYPE ARRAY_AS) Returns true if this array is equal to the given as a parameter.filterKeys
(BiPredicate<? super JsPath, ? super JsValue> filter) Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.filterKeys
(Predicate<? super String> filter) Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.filterObjs
(BiPredicate<? super JsPath, ? super JsObj> filter) Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.filterObjs
(Predicate<? super JsObj> filter) Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.filterValues
(BiPredicate<? super JsPath, ? super JsPrimitive> filter) Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.filterValues
(Predicate<? super JsPrimitive> filter) Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.get
(int i) returns the element located at the specified index or JsNothing if it doesn't exist.Returns the element located at the given path orJsNothing
if it doesn't exist.getArray
(int index) Returns the array located at the given index or null if it doesn't exist or it's not a json array.Returns the array located at the given index or the default value provided if it doesn't exist or it's not a json array.getBigDec
(int index) Returns the number located at the given index as a big decimal or null if it doesn't exist or it's not a decimal number.getBigDec
(int index, Supplier<BigDecimal> orElse) Returns the number located at the given index as a big decimal or the default value provided if it doesn't exist or it's not a decimal number.getBigInt
(int index) Returns the number located at the given index as a big integer or null if it doesn't exist or it's not an integral number.getBigInt
(int index, Supplier<BigInteger> orElse) Returns the number located at the given index as a big integer or the default value provided if it doesn't exist or it's not an integral number.byte[]
getBinary
(int index) Returns the array of bytes located at the given index or null if it doesn't exist or it's not an array of bytes.byte[]
Returns the array of bytes located at the given index or the default value provided if it doesn't exist or it's not an array of bytes.getBool
(int index) Returns the boolean located at the given index or null if it doesn't exist.Returns the boolean located at the given index or the default value provided if it doesn't exist.getDouble
(int index) Returns the number located at the given index as a double or null if it doesn't exist or it's not a decimal number.Returns the number located at the given index as a double or the default value provided if it doesn't exist or it's not a decimal number.getInstant
(int index) Returns the instant located at the given index or null if it doesn't exist or it's not an instant.getInstant
(int index, Supplier<Instant> orElse) Returns the instant located at the given index or the default value provided if it doesn't exist or it's not an instant.getInt
(int index) Returns the integral number located at the given index as an integer or null if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an integer.Returns the integral number located at the given index as an integer or the default value provided if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an integer.getLong
(int index) Returns the long number located at the given index as an long or null if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an long.Returns the long number located at the given index as an long or the default value provided if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an long.getObj
(int index) Returns the object located at the given index or null if it doesn't exist or it's not a json object.Returns the object located at the given index or the default value provided if it doesn't exist or it's not a json object.getStr
(int index) Returns the string located at the given index or null if it doesn't exist.Returns the string located at the given index or the default value provided if it doesn't exist.int
hashCode()
equals method is inherited, so it's implemented.head()
Returns the first element of this array.init()
Returns all the elements of this array except the last one.intersection
(JsArray that, JsArray.TYPE ARRAY_AS) this.union(that, SET)
returnsthis
plus those elements fromthat
that don't exist inthis
.boolean
isArray()
Returns true if this JsValue is a JsArrayboolean
isEmpty()
return true if there's no element in this jsoniterator()
last()
Returns the last element of this array.mapKeys
(BiFunction<? super JsPath, ? super JsValue, String> fn) Maps all the keys of this json.Maps all the keys of this json.mapObjs
(BiFunction<? super JsPath, ? super JsObj, ? extends JsValue> fn) Maps all the jsons of this json.Maps all the jsons of this json.mapValues
(BiFunction<? super JsPath, ? super JsPrimitive, ? extends JsValue> fn) Maps all the values of this json.mapValues
(Function<? super JsPrimitive, ? extends JsValue> fn) Maps all the values of this json.static JsArray
of
(boolean bool, boolean... others) Returns an immutable array from one or more booleans.static JsArray
of
(double number, double... others) Returns an immutable array from one or more doubles.static JsArray
of
(int number, int... others) Returns an immutable array from one or more integers.static JsArray
of
(long number, long... others) Returns an immutable array from one or more longs.static JsArray
Returns an immutable array from one or more strings.static JsArray
of
(BigInteger number, BigInteger... others) Returns an immutable array from one or more big integers.static JsArray
static JsArray
Returns an immutable two-element array.static JsArray
Returns an immutable three-element array.static JsArray
Returns an immutable four-element array.static JsArray
Returns an immutable five-element array.static JsArray
Returns an immutable array.static JsArray
ofIterable
(Iterable<? extends JsValue> iterable) returns an immutable json array from an iterable of json elementsstatic JsArray
Tries to parse the string into an immutable json array.Adds one or more elements, starting from the last, to the front of this array.prependAll
(JsArray array) Adds all the elements of the array, starting from the last, to the front of this array.<R> Optional<R>
reduce
(BinaryOperator<R> op, BiFunction<? super JsPath, ? super JsPrimitive, R> map, BiPredicate<? super JsPath, ? super JsPrimitive> predicate) Performs a reduction on the values of this json that satisfy the predicate.<R> Optional<R>
reduce
(BinaryOperator<R> op, Function<? super JsPrimitive, R> map, Predicate<? super JsPrimitive> predicate) Performs a reduction on the values of this json that satisfy the predicate.Inserts the element at the path in this json, replacing any existing element and filling withJsNull
empty indexes in arrays when necessary.Inserts the element at the path in this json, replacing any existing element and filling with padElement empty indexes in arrays when necessary.int
size()
Returns the number of elements in the first level of this jsonstream()
Returns a stream over all the pairs of elements in this json object.tail()
Returns a json array consisting of all elements of this array except the first one.toString()
// Single-check idiom Item 83 from effective javaunion
(JsArray that, JsArray.TYPE ARRAY_AS) returnsthis
plus those elements fromthat
which position is>= this.size()
, and, at the positions where a container of the same type exists in boththis
andthat
, the result is their union.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 jsonvalues.Json
containsPath, equals, getArray, getArray, getBigDec, getBigDec, getBigInt, getBigInt, getBinary, getBinary, getBool, getBool, getDouble, getDouble, getInstant, getInstant, getInt, getInt, getLong, getLong, getObj, getObj, getStr, getStr, ifEmptyElse, isNotEmpty, serialize, serialize, toJsPrimitive, toPrettyString, toPrettyString
Methods inherited from interface jsonvalues.JsValue
ifNothing, ifNull, isArray, isBigDec, isBigDec, isBigInt, isBigInt, isBinary, isBool, isDecimal, isDouble, isDouble, isFalse, isInstant, isInstant, isInt, isInt, isIntegral, isJson, isJson, isLong, isLong, isNothing, isNotNothing, isNotNull, isNotNumber, isNull, isNumber, isObj, isObj, isPrimitive, isSameType, isStr, isStr, isTrue, toJsArray, toJsBigDec, toJsBigInt, toJsBinary, toJsBool, toJsDouble, toJsInstant, toJsInt, toJsLong, toJsNumber, toJsObj, toJson, toJsStr
-
Field Details
-
lens
lenses defined for a Json array -
optional
optionals defined for a Json array -
EMPTY
-
prism
prism between the sum type JsValue and JsArray
-
-
Constructor Details
-
JsArray
public JsArray()
-
-
Method Details
-
empty
-
of
public static JsArray of(JsValue e, JsValue e1, JsValue e2, JsValue e3, JsValue e4, JsValue... rest) Returns an immutable array.- Parameters:
e
- a JsValuee1
- a JsValuee2
- a JsValuee3
- a JsValuee4
- a JsValuerest
- more optional JsValue- Returns:
- an immutable JsArray
-
of
Returns an immutable five-element array.- Parameters:
e
- a JsValuee1
- a JsValuee2
- a JsValuee3
- a JsValuee4
- a JsValue- Returns:
- an immutable five-element JsArray
-
of
Returns an immutable four-element array.- Parameters:
e
- a JsValuee1
- a JsValuee2
- a JsValuee3
- a JsValue- Returns:
- an immutable four-element JsArray
-
of
Returns an immutable three-element array.- Parameters:
e
- a JsValuee1
- a JsValuee2
- a JsValue- Returns:
- an immutable three-element JsArray
-
of
Returns an immutable two-element array.- Parameters:
e
- a JsValuee1
- a JsValue- Returns:
- an immutable two-element JsArray
-
of
-
of
Returns an immutable array from one or more strings.- Parameters:
str
- a stringothers
- more optional strings- Returns:
- an immutable JsArray
-
of
Returns an immutable array from one or more integers.- Parameters:
number
- an integerothers
- more optional integers- Returns:
- an immutable JsArray
-
of
Returns an immutable array from one or more booleans.- Parameters:
bool
- an booleanothers
- more optional booleans- Returns:
- an immutable JsArray
-
of
Returns an immutable array from one or more longs.- Parameters:
number
- a longothers
- more optional longs- Returns:
- an immutable JsArray
-
of
Returns an immutable array from one or more big integers.- Parameters:
number
- a big integerothers
- more optional big integers- Returns:
- an immutable JsArray
-
of
Returns an immutable array from one or more doubles.- Parameters:
number
- a doubleothers
- more optional doubles- Returns:
- an immutable JsArray
-
ofIterable
returns an immutable json array from an iterable of json elements- Parameters:
iterable
- the iterable of json elements- Returns:
- an immutable json array
-
parse
Tries to parse the string into an immutable json array.- Parameters:
str
- the string to be parsed- Returns:
- a JsArray
- Throws:
JsParserException
- if the string doesnt represent a json array
-
append
Adds one or more elements, starting from the first, to the back of this array.- Parameters:
e
- the JsValue to be added to the back.others
- more optional JsValue to be added to the back- Returns:
- a new JsArray
-
appendAll
Adds all the elements of the given array, starting from the head, to the back of this array.- Parameters:
array
- the JsArray of elements to be added to the back- Returns:
- a new JsArray
-
equals
Returns true if this array is equal to the given as a parameter. In the case of ARRAY_AS=LIST, this method is equivalent to JsArray.equals(Object).- Parameters:
array
- the given arrayARRAY_AS
- option to define if arrays are considered SETS, LISTS OR MULTISET- Returns:
- true if both arrays are equals according to ARRAY_AS parameter
-
getInt
Returns the integral number located at the given index as an integer or null if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an integer.- Parameters:
index
- the index- Returns:
- the integral number located at the given index or null
-
getInt
Returns the integral number located at the given index as an integer or the default value provided if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an integer.- Parameters:
index
- the indexorElse
- the default value- Returns:
- the integral number located at the given index or null
-
getLong
Returns the long number located at the given index as an long or null if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an long.- Parameters:
index
- the index- Returns:
- the long number located at the given index or null
-
getLong
Returns the long number located at the given index as an long or the default value provided if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an long.- Parameters:
index
- the indexorElse
- the default value- Returns:
- the long number located at the given index or the default value provided
-
getStr
Returns the string located at the given index or null if it doesn't exist.- Parameters:
index
- the index- Returns:
- the string located at the given index or null
-
getStr
Returns the string located at the given index or the default value provided if it doesn't exist.- Parameters:
index
- the indexorElse
- the default value- Returns:
- the string located at the given index or the default value
-
getInstant
Returns the instant located at the given index or null if it doesn't exist or it's not an instant. If the element is an instant formatted as a string, it's returned as an instant as well.- Parameters:
index
- the given index- Returns:
- an instant
-
getInstant
Returns the instant located at the given index or the default value provided if it doesn't exist or it's not an instant. If the element is an instant formatted as a string, it's returned as an instant as well.- Parameters:
index
- the given indexorElse
- the default value- Returns:
- an instant
-
getBinary
public byte[] getBinary(int index) Returns the array of bytes located at the given index or null if it doesn't exist or it's not an array of bytes. If the element is a string in base64, it's returned as an array of bytes as well.- Parameters:
index
- the given index- Returns:
- an array of bytes
-
getBinary
Returns the array of bytes located at the given index or the default value provided if it doesn't exist or it's not an array of bytes. If the element is a string in base64, it's returned as an array of bytes as well.- Parameters:
index
- the given indexorElse
- the default value- Returns:
- an array of bytes
-
getBool
Returns the boolean located at the given index or null if it doesn't exist.- Parameters:
index
- the index- Returns:
- the boolean located at the given index or null
-
getBool
Returns the boolean located at the given index or the default value provided if it doesn't exist.- Parameters:
index
- the indexorElse
- the default value- Returns:
- the boolean located at the given index or the default value provided
-
getDouble
Returns the number located at the given index as a double or null if it doesn't exist or it's not a decimal number. If the number is a BigDecimal, the conversion is identical to the specified inBigDecimal.doubleValue()
and in some cases it can lose information about the precision of the BigDecimal- Parameters:
index
- the index- Returns:
- the double number located at the given index or null
-
getDouble
Returns the number located at the given index as a double or the default value provided if it doesn't exist or it's not a decimal number. If the number is a BigDecimal, the conversion is identical to the specified inBigDecimal.doubleValue()
and in some cases it can lose information about the precision of the BigDecimal- Parameters:
index
- the indexorElse
- the default value- Returns:
- the double number located at the given index or null
-
getBigDec
Returns the number located at the given index as a big decimal or null if it doesn't exist or it's not a decimal number.- Parameters:
index
- the index- Returns:
- the decimal number located at the given index or null
-
getBigDec
Returns the number located at the given index as a big decimal or the default value provided if it doesn't exist or it's not a decimal number.- Parameters:
index
- the indexorElse
- the default value- Returns:
- the decimal number located at the given index or the default value provided
-
getBigInt
Returns the number located at the given index as a big integer or null if it doesn't exist or it's not an integral number.- Parameters:
index
- the index- Returns:
- the integral number located at the given index or null
-
getBigInt
Returns the number located at the given index as a big integer or the default value provided if it doesn't exist or it's not an integral number.- Parameters:
index
- the indexorElse
- the default value provided- Returns:
- the integral number located at the given index or null
-
getObj
Returns the object located at the given index or null if it doesn't exist or it's not a json object.- Parameters:
index
- the index- Returns:
- the object located at the given index or null
-
getObj
Returns the object located at the given index or the default value provided if it doesn't exist or it's not a json object.- Parameters:
index
- the indexorElse
- the default value- Returns:
- the object located at the given index or the default value provided
-
getArray
Returns the array located at the given index or null if it doesn't exist or it's not a json array.- Parameters:
index
- the index- Returns:
- the array located at the given index or null
-
getArray
Returns the array located at the given index or the default value provided if it doesn't exist or it's not a json array.- Parameters:
index
- the indexorElse
- the default value- Returns:
- the array located at the given index or the default value provided
-
containsValue
Description copied from interface:Json
Returns true if this json contains the given element in the first level.- Specified by:
containsValue
in interfaceJson<JsArray>
- Parameters:
el
- the give element JsValue whose presence in this JsArray is to be tested- Returns:
- true if this JsArray contains the JsValue
-
get
Description copied from interface:Json
Returns the element located at the given path orJsNothing
if it doesn't exist. -
filterValues
Description copied from interface:Json
Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterValues
in interfaceJson<JsArray>
- Parameters:
filter
- the predicate which takes as the input every JsPair of this json- Returns:
- same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
-
filterValues
Description copied from interface:Json
Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterValues
in interfaceJson<JsArray>
- Parameters:
filter
- the predicate which takes as the input every JsPair of this json- Returns:
- same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
-
filterKeys
Description copied from interface:Json
Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterKeys
in interfaceJson<JsArray>
- Parameters:
filter
- the predicate which takes as the input every JsPair of this json- Returns:
- same this instance if all the keys satisfy the predicate or a new filtered json of the same type T
-
filterKeys
Description copied from interface:Json
Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterKeys
in interfaceJson<JsArray>
- Parameters:
filter
- the predicate which takes as the input every JsPair of this json- Returns:
- same this instance if all the keys satisfy the predicate or a new filtered json of the same type T
-
filterObjs
Description copied from interface:Json
Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterObjs
in interfaceJson<JsArray>
- Parameters:
filter
- the predicate which takes as the input every JsPair of this json- Returns:
- same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
-
filterObjs
Description copied from interface:Json
Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterObjs
in interfaceJson<JsArray>
- Parameters:
filter
- the predicate which takes as the input every JsPair of this json- Returns:
- same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
-
isEmpty
public boolean isEmpty()Description copied from interface:Json
return true if there's no element in this json -
mapValues
Description copied from interface:Json
Maps all the values of this json. -
mapValues
Description copied from interface:Json
Maps all the values of this json. -
mapKeys
Description copied from interface:Json
Maps all the keys of this json. -
mapKeys
Description copied from interface:Json
Maps all the keys of this json. -
mapObjs
Description copied from interface:Json
Maps all the jsons of this json. -
mapObjs
Description copied from interface:Json
Maps all the jsons of this json. -
set
Description copied from interface:Json
Inserts the element at the path in this json, replacing any existing element and filling withJsNull
empty indexes in arrays when necessary.The same instance is returned when the head of the path is a key and this is an array or the head of the path is an index and this is an object or the element is
JsNothing
-
set
-
set
-
set
Description copied from interface:Json
Inserts the element at the path in this json, replacing any existing element and filling with padElement empty indexes in arrays when necessary.The same instance is returned when the head of the path is a key and this is an array or the head of the path is an index and this is an object or the element is
JsNothing
- Specified by:
set
in interfaceJson<JsArray>
- Parameters:
path
- the JsPath object where the element will be inserted atvalue
- the JsValue that will be insertedpadElement
- the JsValue that will be inserted in arrays when padding is necessary- Returns:
- the same instance or a new json of the same type T
-
reduce
public <R> Optional<R> reduce(BinaryOperator<R> op, BiFunction<? super JsPath, ? super JsPrimitive, R> map, BiPredicate<? super JsPath, ? super JsPrimitive> predicate) Description copied from interface:Json
Performs a reduction on the values of this json that satisfy the predicate. The reduction is performed mapping each value with the mapping function and then applying the operator- Specified by:
reduce
in interfaceJson<JsArray>
- Type Parameters:
R
- the type of the operands of the operator- Parameters:
op
- the operator upon two objects of type Rmap
- the mapping function which produces an object of type R from a JsValuepredicate
- the predicate that determines what JsValue will be mapped and reduced- Returns:
- an
Optional
describing the result of the reduction
-
reduce
public <R> Optional<R> reduce(BinaryOperator<R> op, Function<? super JsPrimitive, R> map, Predicate<? super JsPrimitive> predicate) Description copied from interface:Json
Performs a reduction on the values of this json that satisfy the predicate. The reduction is performed mapping each value with the mapping function and then applying the operator- Specified by:
reduce
in interfaceJson<JsArray>
- Type Parameters:
R
- the type of the operands of the operator- Parameters:
op
- the operator upon two objects of type Rmap
- the mapping function which produces an object of type R from a JsValuepredicate
- the predicate that determines what JsValue will be mapped and reduced- Returns:
- an
Optional
describing the result of the reduction
-
delete
Description copied from interface:Json
Removes the element in this json located at the given path, if it exists, returning the same this instance otherwise -
size
public int size()Description copied from interface:Json
Returns the number of elements in the first level of this json -
stream
Description copied from interface:Json
Returns a stream over all the pairs of elements in this json object. -
get
returns the element located at the specified index or JsNothing if it doesn't exist. It never throws an exception, it's a total function.- Parameters:
i
- the index- Returns:
- a JsValue
-
hashCode
public int hashCode()equals method is inherited, so it's implemented. The purpose of this method is to cache the hashcode once calculated. the object is immutable and it won't change Single-check idiom Item 83 from Effective Java -
equals
-
toString
// Single-check idiom Item 83 from effective java -
head
Returns the first element of this array.- Returns:
- the first JsValue of this JsArray
- Throws:
UserError
- if this JsArray is empty
-
tail
Returns a json array consisting of all elements of this array except the first one.- Returns:
- a JsArray consisting of all the elements of this JsArray except the head
- Throws:
UserError
- if this JsArray is empty.
-
init
Returns all the elements of this array except the last one.- Returns:
- JsArray with all the JsValue except the last one
- Throws:
UserError
- if this JsArray is empty
-
intersection
this.union(that, SET)
returnsthis
plus those elements fromthat
that don't exist inthis
.this.union(that, MULTISET)
returnsthis
plus those elements fromthat
appended to the back.this.union(that, LIST)
returnsthis
plus those elements fromthat
which position is>= this.size()
. For those elements that are containers of the same type and are located at the same position, the result is their intersection. So this operation is kind of a recursive intersection- Specified by:
intersection
in interfaceJson<JsArray>
- Parameters:
that
- the other array- Returns:
- a JsArray of the same type as the inputs
-
isArray
public boolean isArray()Description copied from interface:JsValue
Returns true if this JsValue is a JsArray -
iterator
-
last
Returns the last element of this array.- Returns:
- the last JsValue of this JsArray
- Throws:
UserError
- if this JsArray is empty
-
prepend
Adds one or more elements, starting from the last, to the front of this array.- Parameters:
e
- the JsValue to be added to the front.others
- more optional JsValue to be added to the front- Returns:
- a new JsArray
-
prependAll
Adds all the elements of the array, starting from the last, to the front of this array.- Parameters:
array
- the JsArray of elements to be added to the front- Returns:
- a new JsArray
-
delete
-
union
returnsthis
plus those elements fromthat
which position is>= this.size()
, and, at the positions where a container of the same type exists in boththis
andthat
, the result is their union. This operations doesn't make any sense if arrays are not considered lists, because there is no notion of order.
-