Package jsonvalues
Class JsArray
- java.lang.Object
-
- jsonvalues.JsArray
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JsArray.TYPE
Type of arrays: SET, MULTISET or LIST.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsArray
append(JsPath path, JsValue elem)
Appends one element to the array located at the given path in this json.JsArray
append(JsValue e, JsValue... others)
Adds one or more elements, starting from the first, to the back of this array.JsArray
appendAll(JsArray array)
Adds all the elements of the given array, starting from the head, to the back of this array.JsArray
appendAll(JsPath path, JsArray elems)
Appends all the elements of the array, starting from the head, to the array located at the given path in this json.protected Trampoline<JsArray>
appendAllBackTrampoline(JsArray arr1, JsArray arr2)
protected Trampoline<JsArray>
appendAllFrontTrampoline(JsArray arr1, JsArray arr2)
boolean
containsValue(JsValue el)
Returns true if this json contains the given element in the first level.static JsArray
empty()
boolean
equals(@Nullable Object that)
boolean
equals(JsArray array, JsArray.TYPE ARRAY_AS)
Returns true if this array is equal to the given as a parameter.JsArray
filterAllKeys(Predicate<? super JsPair> filter)
Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.JsArray
filterAllObjs(BiPredicate<? super JsPath,? super JsObj> filter)
Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.JsArray
filterAllValues(Predicate<? super JsPair> filter)
Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.JsArray
filterKeys(Predicate<? super JsPair> filter)
Filters the keys in the first level of this json, removing those that don't ifPredicateElse the predicate.JsArray
filterObjs(BiPredicate<? super JsPath,? super JsObj> filter)
Filters the pair of jsons in the first level of this json, removing those that don't ifPredicateElse the predicate.JsArray
filterValues(Predicate<? super JsPair> filter)
Filters the pairs of elements in the first level of this json, removing those that don't ifPredicateElse the predicate.JsValue
get(int i)
JsValue
get(Position pos)
Returns the element located at the key or index specified by the given position orJsNothing
if it doesn't exist.int
hashCode()
equals method is inherited, so it's implemented.JsValue
head()
Returns the first element of this array.int
id()
<T> Trampoline<T>
ifEmptyElse(Trampoline<T> empty, BiFunction<JsValue,JsArray,Trampoline<T>> fn)
JsArray
init()
Returns all the elements of this array except the last one.JsArray
intersection(JsArray that, JsArray.TYPE ARRAY_AS)
this.intersection(that, SET)
returns an array with the elements that exist in boththis
andthat
.JsArray
intersectionAll(JsArray that)
this.intersectionAll(that)
behaves asthis.intersection(that, LIST)
, but for those elements that are containers of the same type and are located at the same position, the result is their intersection.boolean
isArray()
boolean
isEmpty()
return true if there's no element in this jsonIterator<JsValue>
iterator()
JsValue
last()
Returns the last element of this array.JsArray
mapAllKeys(Function<? super JsPair,String> fn)
Maps all the keys of this json.JsArray
mapAllKeys(Function<? super JsPair,String> fn, Predicate<? super JsPair> predicate)
Maps all the keys of this json that satisfies a given predicate.JsArray
mapAllObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn)
Maps all the jsons of this json.JsArray
mapAllObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn, BiPredicate<? super JsPath,? super JsObj> predicate)
Maps all the jsons of this json that satisfies a given predicate.JsArray
mapAllValues(Function<? super JsPair,? extends JsValue> fn)
Maps all the values of this json.JsArray
mapAllValues(Function<? super JsPair,? extends JsValue> fn, Predicate<? super JsPair> predicate)
Maps all the values of this json that satisfies a given predicate.JsArray
mapKeys(Function<? super JsPair,String> fn)
Maps the keys in the first level of this json.JsArray
mapKeys(Function<? super JsPair,String> fn, Predicate<? super JsPair> predicate)
Maps the keys in the first level of this json that satisfies a given predicate.JsArray
mapObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn)
Maps the jsons in the first level of this json.JsArray
mapObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn, BiPredicate<? super JsPath,? super JsObj> predicate)
Maps the jsons in the first level of this json that satisfies a given predicate.JsArray
mapValues(Function<? super JsPair,? extends JsValue> fn)
Maps the values in the first level of this json.JsArray
mapValues(Function<? super JsPair,? extends JsValue> fn, Predicate<? super JsPair> predicate)
Maps the values in the first level of this json that satisfies a given predicate.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
of(String str, String... others)
Returns an immutable array from one or more strings.static JsArray
of(JsPair pair, JsPair... others)
Returns an immutable array from one or more pairs.static JsArray
of(JsValue e)
static JsArray
of(JsValue e, JsValue e1)
Returns an immutable two-element array.static JsArray
of(JsValue e, JsValue e1, JsValue e2)
Returns an immutable three-element array.static JsArray
of(JsValue e, JsValue e1, JsValue e2, JsValue e3)
Returns an immutable four-element array.static JsArray
of(JsValue e, JsValue e1, JsValue e2, JsValue e3, JsValue e4)
Returns an immutable five-element array.static JsArray
of(JsValue e, JsValue e1, JsValue e2, JsValue e3, JsValue e4, JsValue... rest)
Returns an immutable array.static JsArray
ofIterable(Iterable<JsValue> iterable)
returns an immutable json array from an iterable of json elementsstatic JsArray
parse(String str)
Tries to parse the string into an immutable json array.static JsArray
parse(String str, ParseBuilder builder)
Tries to parse the string into an immutable json, performing the specified transformations while the parsing.JsArray
prepend(JsPath path, JsValue elem)
prepends one element to the array located at the path in this json.JsArray
prepend(JsValue e, JsValue... others)
Adds one or more elements, starting from the last, to the front of this array.JsArray
prependAll(JsArray array)
Adds all the elements of the array, starting from the last, to the front of this array.JsArray
prependAll(JsPath path, JsArray elems)
prepends all the elements of the array, starting from the head, to the array located at the path in this json.JsArray
put(JsPath path, Function<? super JsValue,? extends JsValue> fn)
Inserts the element returned by the function at the given path in this json, replacing any existing element and filling withJsNull
empty indexes in arrays when necessary.<R> Optional<R>
reduce(BinaryOperator<R> op, Function<? super JsPair,R> map, Predicate<? super JsPair> predicate)
Performs a reduction on the values that satisfy the predicate in the first level of this json.<R> Optional<R>
reduceAll(BinaryOperator<R> op, Function<? super JsPair,R> map, Predicate<? super JsPair> predicate)
Performs a reduction on the values of this json that satisfy the predicate.JsArray
remove(JsPath path)
Removes the element in this json located at the given path, if it exists, returning the same this instance otherwiseint
size()
Returns the number of elements in the first level of this jsonStream<JsPair>
stream()
Returns a stream over the pairs of elements in the first level of this json object.Stream<JsPair>
streamAll()
Returns a stream over all the pairs of elements in this json object.JsArray
tail()
Returns a json array consisting of all elements of this array except the first one.String
toString()
// Single-check idiom Item 83 from effective javaJsArray
union(JsArray that, JsArray.TYPE ARRAY_AS)
this.union(that, SET)
returnsthis
plus those elements fromthat
that don't exist inthis
.JsArray
unionAll(JsArray that)
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
append, append, append, append, append, append, appendAllIfPresent, appendIfPresent, appendIfPresent, appendIfPresent, appendIfPresent, appendIfPresent, appendIfPresent, containsPath, equals, get, getArray, getBigDecimal, getBigInt, getBool, getDouble, getInt, getLong, getObj, getStr, ifEmptyElse, isNotEmpty, map, prepend, prepend, prepend, prepend, prepend, prepend, prependAllIfPresent, prependIfPresent, prependIfPresent, prependIfPresent, prependIfPresent, prependIfPresent, prependIfPresent, put, put, put, put, put, put, put, putIf, putIfAbsent, putIfAbsent, putIfAbsent, putIfAbsent, putIfPresent, putIfPresent, putIfPresent, putIfPresent, serialize, serialize, size, sizeAll, sizeAll, times, timesAll, toPrettyString
-
Methods inherited from interface jsonvalues.JsValue
isArray, isBigDec, isBigDec, isBigInt, isBigInt, isBool, isDecimal, isDouble, isDouble, isFalse, isInstant, isInstant, isInt, isInt, isIntegral, isJson, isJson, isLocalDate, isLocalDate, isLocalDateTime, isLocalDateTime, isLong, isLong, isNothing, isNotJson, isNotNothing, isNotNull, isNotNumber, isNull, isNumber, isObj, isObj, isSameType, isStr, isStr, isTrue, toJsArray, toJsBigDec, toJsBigInt, toJsBool, toJsDouble, toJsInt, toJsLong, toJsObj, toJson, toJsStr
-
-
-
-
Field Detail
-
EMPTY
public static JsArray EMPTY
-
ID
public static final int ID
- See Also:
- Constant Field Values
-
-
Method Detail
-
append
public final JsArray append(JsValue e, JsValue... others)
Adds one or more elements, starting from the first, to the back of this array.- Parameters:
e
- the JsElem to be added to the back.others
- more optional JsElem to be added to the back- Returns:
- a new JsArray
-
append
public final JsArray append(JsPath path, JsValue elem)
Description copied from interface:Json
Appends one element to the array located at the given path in this json. If the array doesn't exist, a new one is created, replacing any existing element in the path and filling empty indexes in arrays withJsNull
when necessary. The same this instance is returned when it's an array and the head of the path is a key or when it's an object and the head of the path is an index.
-
appendAll
public final JsArray appendAll(JsArray array)
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
-
appendAll
public final JsArray appendAll(JsPath path, JsArray elems)
Description copied from interface:Json
Appends all the elements of the array, starting from the head, to the array located at the given path in this json. If the array doesn't exist, a new one is created, replacing any existing element in the path and filling empty indexes in arrays withJsNull
when necessary. The same this instance is returned when it's an array and the head of the path is a key or when it's an object and the head of the path is an index.
-
appendAllBackTrampoline
protected Trampoline<JsArray> appendAllBackTrampoline(JsArray arr1, JsArray arr2)
-
appendAllFrontTrampoline
protected Trampoline<JsArray> appendAllFrontTrampoline(JsArray arr1, JsArray arr2)
-
containsValue
public final boolean containsValue(JsValue el)
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 JsElem whose presence in this JsArray is to be tested- Returns:
- true if this JsArray contains the JsElem
-
empty
public static JsArray empty()
-
equals
public boolean equals(JsArray array, JsArray.TYPE ARRAY_AS)
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
-
filterAllValues
public final JsArray filterAllValues(Predicate<? super JsPair> filter)
Description copied from interface:Json
Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterAllValues
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
- See Also:
how to filter the pairs of values of only the first level
-
filterAllKeys
public final JsArray filterAllKeys(Predicate<? super JsPair> filter)
Description copied from interface:Json
Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterAllKeys
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
- See Also:
how to filter the keys of only the first level
-
filterAllObjs
public final JsArray filterAllObjs(BiPredicate<? super JsPath,? super JsObj> filter)
Description copied from interface:Json
Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.- Specified by:
filterAllObjs
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
- See Also:
how to filter the pair of jsons of only the first level
-
filterValues
public final JsArray filterValues(Predicate<? super JsPair> filter)
Description copied from interface:Json
Filters the pairs of elements in the first level 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 in the first level of this json- Returns:
- same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
- See Also:
how to filter the pair of elements of the whole json and not only the first level
-
filterKeys
public final JsArray filterKeys(Predicate<? super JsPair> filter)
Description copied from interface:Json
Filters the keys in the first level 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 in the first level of this json- Returns:
- same this instance if all the keys satisfy the predicate or a new filtered json of the same type T
- See Also:
how to filter the keys of the whole json and not only the first level
-
filterObjs
public final JsArray filterObjs(BiPredicate<? super JsPath,? super JsObj> filter)
Description copied from interface:Json
Filters the pair of jsons in the first level 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 in the first level of this json- Returns:
- same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
- See Also:
how to filter the pair of jsons of the whole json and not only the first level
-
get
public final JsValue get(Position pos)
Description copied from interface:Json
Returns the element located at the key or index specified by the given position orJsNothing
if it doesn't exist.
-
get
public JsValue get(int i)
-
hashCode
public final 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
-
head
public final JsValue head()
Returns the first element of this array.- Returns:
- the first JsElem of this JsArray
- Throws:
UserError
- if this JsArray is empty
-
ifEmptyElse
public <T> Trampoline<T> ifEmptyElse(Trampoline<T> empty, BiFunction<JsValue,JsArray,Trampoline<T>> fn)
-
init
public final JsArray init()
Returns all the elements of this array except the last one.- Returns:
- JsArray with all the JsElem except the last one
- Throws:
UserError
- if this JsArray is empty
-
intersection
public final JsArray intersection(JsArray that, JsArray.TYPE ARRAY_AS)
this.intersection(that, SET)
returns an array with the elements that exist in boththis
andthat
.this.intersection(that, MULTISET)
returns an array with the elements that exist in boththis
andthat
, being duplicates allowed.this.intersection(that, LIST)
returns an array with the elements that exist in boththis
andthat
and are located at the same position.- Parameters:
that
- the other arrayARRAY_AS
- option to define if arrays are considered SETS, LISTS OR MULTISET- Returns:
- a new JsArray of the same type as the inputs (mutable or immutable)
-
intersectionAll
public JsArray intersectionAll(JsArray that)
this.intersectionAll(that)
behaves asthis.intersection(that, LIST)
, but 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- Parameters:
that
- the other array- Returns:
- a JsArray of the same type as the inputs (mutable or immutable)
-
isArray
public boolean isArray()
-
isEmpty
public final boolean isEmpty()
Description copied from interface:Json
return true if there's no element in this json
-
last
public final JsValue last()
Returns the last element of this array.- Returns:
- the last JsElem of this JsArray
- Throws:
UserError
- if this JsArray is empty
-
mapAllValues
public JsArray mapAllValues(Function<? super JsPair,? extends JsValue> fn)
Description copied from interface:Json
Maps all the values of this json.- Specified by:
mapAllValues
in interfaceJson<JsArray>
- Parameters:
fn
- the mapping function- Returns:
- a new mapped json of the same type T
- See Also:
to map jsons
,to map keys of json objects
,to map only the first level
-
mapAllValues
public JsArray mapAllValues(Function<? super JsPair,? extends JsValue> fn, Predicate<? super JsPair> predicate)
Description copied from interface:Json
Maps all the values of this json that satisfies a given predicate.- Specified by:
mapAllValues
in interfaceJson<JsArray>
- Parameters:
fn
- the mapping functionpredicate
- the given predicate that determines what JsValues will be mapped- Returns:
- same this instance or a new mapped json of the same type TT
- See Also:
to map jsons
,to map keys of json objects
,to map only the first level
-
mapAllKeys
public final JsArray mapAllKeys(Function<? super JsPair,String> fn)
Description copied from interface:Json
Maps all the keys of this json.- Specified by:
mapAllKeys
in interfaceJson<JsArray>
- Parameters:
fn
- the mapping function- Returns:
- a new mapped json of the same type T
- See Also:
to map values
,to map jsons
,to map only the first level
-
mapAllKeys
public final JsArray mapAllKeys(Function<? super JsPair,String> fn, Predicate<? super JsPair> predicate)
Description copied from interface:Json
Maps all the keys of this json that satisfies a given predicate.- Specified by:
mapAllKeys
in interfaceJson<JsArray>
- Parameters:
fn
- the mapping functionpredicate
- the given predicate that determines what keys will be mapped- Returns:
- same this instance or a new mapped json of the same type T
- See Also:
to map values
,to map jsons
,to map only the first level
-
mapAllObjs
public final JsArray mapAllObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn, BiPredicate<? super JsPath,? super JsObj> predicate)
Description copied from interface:Json
Maps all the jsons of this json that satisfies a given predicate.- Specified by:
mapAllObjs
in interfaceJson<JsArray>
- Parameters:
fn
- the mapping functionpredicate
- the given predicate that determines what Jsons will be mapped- Returns:
- same this instance or a new mapped json of the same type T
- See Also:
to map values
,to map keys of json objects
,to map only the first level
-
mapAllObjs
public final JsArray mapAllObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn)
Description copied from interface:Json
Maps all the jsons of this json.- Specified by:
mapAllObjs
in interfaceJson<JsArray>
- Parameters:
fn
- the mapping function- Returns:
- a new mapped json of the same type T
- See Also:
to map values
,to map keys of json objects
,to map only the first level
-
mapValues
public final JsArray mapValues(Function<? super JsPair,? extends JsValue> fn)
Description copied from interface:Json
Maps the values in the first level of this json.- Specified by:
mapValues
in interfaceJson<JsArray>
- Parameters:
fn
- the mapping function- Returns:
- a new mapped json of the same type T
- See Also:
to map jsons
,to map keys of json objects
,to map all the values and not only the first level
-
mapValues
public JsArray mapValues(Function<? super JsPair,? extends JsValue> fn, Predicate<? super JsPair> predicate)
Description copied from interface:Json
Maps the values in the first level of this json that satisfies a given predicate.- Specified by:
mapValues
in interfaceJson<JsArray>
- Parameters:
fn
- the mapping functionpredicate
- the given predicate that determines what JsValues will be mapped- Returns:
- same this instance or a new mapped json of the same type T
- See Also:
to map jsons
,to map keys of json objects
,to map all the values and not only the first level
-
mapKeys
public final JsArray mapKeys(Function<? super JsPair,String> fn)
Description copied from interface:Json
Maps the keys in the first level of this json.- Specified by:
mapKeys
in interfaceJson<JsArray>
- Parameters:
fn
- the mapping function- Returns:
- a new mapped json of the same type T
- See Also:
to map values
,to map jsons
,to map all the keys and not only the first level
-
mapKeys
public final JsArray mapKeys(Function<? super JsPair,String> fn, Predicate<? super JsPair> predicate)
Description copied from interface:Json
Maps the keys in the first level of this json that satisfies a given predicate.- Specified by:
mapKeys
in interfaceJson<JsArray>
- Parameters:
fn
- the mapping functionpredicate
- the given predicate that determines what keys will be mapped- Returns:
- same this instance or a new mapped json of the same type T
- See Also:
to map jsons
,to map values
,to map all the keys and not only the first level
-
mapObjs
public final JsArray mapObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn, BiPredicate<? super JsPath,? super JsObj> predicate)
Description copied from interface:Json
Maps the jsons in the first level of this json that satisfies a given predicate.- Specified by:
mapObjs
in interfaceJson<JsArray>
- Parameters:
fn
- the mapping functionpredicate
- the given predicate that determines what Jsons will be mapped- Returns:
- same this instance or a new mapped json of the same type T
- See Also:
to map values
,to map keys of json objects
,to map all the jsons and not only the first level
-
mapObjs
public final JsArray mapObjs(BiFunction<? super JsPath,? super JsObj,JsObj> fn)
Description copied from interface:Json
Maps the jsons in the first level of this json.- Specified by:
mapObjs
in interfaceJson<JsArray>
- Parameters:
fn
- the mapping function- Returns:
- a new mapped json of the same type T
- See Also:
to map values
,to map keys of json objects
,to map all the jsons and not only the first level
-
of
public static JsArray of(JsPair pair, JsPair... others)
Returns an immutable array from one or more pairs.- Parameters:
pair
- a pairothers
- more optional pairs- Returns:
- an immutable JsArray
- Throws:
UserError
- if an elem of a pair is mutable
-
of
public static JsArray of(JsValue e, JsValue e1)
Returns an immutable two-element array.- Parameters:
e
- a JsEleme1
- a JsElem- Returns:
- an immutable two-element JsArray
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsArray of(JsValue e, JsValue e1, JsValue e2)
Returns an immutable three-element array.- Parameters:
e
- a JsEleme1
- a JsEleme2
- a JsElem- Returns:
- an immutable three-element JsArray
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsArray of(JsValue e, JsValue e1, JsValue e2, JsValue e3)
Returns an immutable four-element array.- Parameters:
e
- a JsEleme1
- a JsEleme2
- a JsEleme3
- a JsElem- Returns:
- an immutable four-element JsArray
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsArray of(JsValue e, JsValue e1, JsValue e2, JsValue e3, JsValue e4)
Returns an immutable five-element array.- Parameters:
e
- a JsEleme1
- a JsEleme2
- a JsEleme3
- a JsEleme4
- a JsElem- Returns:
- an immutable five-element JsArray
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsArray of(JsValue e, JsValue e1, JsValue e2, JsValue e3, JsValue e4, JsValue... rest)
Returns an immutable array.- Parameters:
e
- a JsEleme1
- a JsEleme2
- a JsEleme3
- a JsEleme4
- a JsElemrest
- more optional JsElem- Returns:
- an immutable JsArray
- Throws:
UserError
- if an elem is a mutable Json
-
of
public static JsArray of(String str, String... others)
Returns an immutable array from one or more strings.- Parameters:
str
- a stringothers
- more optional strings- Returns:
- an immutable JsArray
-
of
public static JsArray of(int number, int... others)
Returns an immutable array from one or more integers.- Parameters:
number
- an integerothers
- more optional integers- Returns:
- an immutable JsArray
-
of
public static JsArray of(boolean bool, boolean... others)
Returns an immutable array from one or more booleans.- Parameters:
bool
- an booleanothers
- more optional booleans- Returns:
- an immutable JsArray
-
of
public static JsArray of(long number, long... others)
Returns an immutable array from one or more longs.- Parameters:
number
- a longothers
- more optional longs- Returns:
- an immutable JsArray
-
of
public static JsArray of(double number, double... others)
Returns an immutable array from one or more doubles.- Parameters:
number
- a doubleothers
- more optional doubles- Returns:
- an immutable JsArray
-
ofIterable
public static JsArray ofIterable(Iterable<JsValue> iterable)
returns an immutable json array from an iterable of json elements- Parameters:
iterable
- the iterable of json elements- Returns:
- an immutable json array
-
parse
public static JsArray parse(String str) throws MalformedJson
Tries to parse the string into an immutable json array.- Parameters:
str
- the string to be parsed- Returns:
- a JsArray
- Throws:
MalformedJson
- if the string doesnt represent a json array
-
parse
public static JsArray parse(String str, ParseBuilder builder) throws MalformedJson
Description copied from interface:Json
Tries to parse the string into an immutable json, performing the specified transformations while the parsing.- Parameters:
str
- the string that will be parsedbuilder
- a builder with the transformations that will be applied during the parsing- Returns:
- an immutable json
- Throws:
MalformedJson
- if it's not a valid Json
-
prepend
public final JsArray prepend(JsValue e, JsValue... others)
Adds one or more elements, starting from the last, to the front of this array.- Parameters:
e
- the JsElem to be added to the front.others
- more optional JsElem to be added to the front- Returns:
- a new JsArray
-
prepend
public final JsArray prepend(JsPath path, JsValue elem)
Description copied from interface:Json
prepends one element to the array located at the path in this json. If the array at the path doesn't exist, a new one is created, replacing any existing element in the path and filling empty indexes in arrays withJsNull
when necessary. The same this instance is returned when it's an array and the head of the path is a key or when it's an object and the head of the path is an index.
-
prependAll
public final JsArray prependAll(JsArray array)
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
-
prependAll
public final JsArray prependAll(JsPath path, JsArray elems)
Description copied from interface:Json
prepends all the elements of the array, starting from the head, to the array located at the path in this json. If the array at the path doesn't exist, a new one is created, replacing any existing element in the path and filling empty indexes in arrays withJsNull
when necessary. The same this instance is returned when it's an array and the head of the path is a key or when it's an object and the head of the path is an index.- Specified by:
prependAll
in interfaceJson<JsArray>
- Parameters:
path
- the JsPath pointing to the array in which all the elements will be prependedelems
- the JsArray of elements to be prepended to the existing or created array- Returns:
- same this instance or a new json of the same type T
-
put
public final JsArray put(JsPath path, Function<? super JsValue,? extends JsValue> fn)
Description copied from interface:Json
Inserts the element returned by the function at the given path in this json, replacing any existing element and filling withJsNull
empty indexes in arrays when necessary. The same instance is returned when the path is empty, or 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. In both cases the function is not invoked. The same instance is returned as well when the element returned by the function isJsNothing
-
reduce
public final <R> Optional<R> reduce(BinaryOperator<R> op, Function<? super JsPair,R> map, Predicate<? super JsPair> predicate)
Description copied from interface:Json
Performs a reduction on the values that satisfy the predicate in the first level of this json. 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 of of the reduction - See Also:
to apply the reduction in all the Json and not only in the first level
-
reduceAll
public final <R> Optional<R> reduceAll(BinaryOperator<R> op, Function<? super JsPair,R> map, Predicate<? super JsPair> 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:
reduceAll
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 - See Also:
to apply the reduction only in the first level
-
remove
public final JsArray remove(JsPath path)
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 final int size()
Description copied from interface:Json
Returns the number of elements in the first level of this json
-
stream
public Stream<JsPair> stream()
Description copied from interface:Json
Returns a stream over the pairs of elements in the first level of this json object.
-
streamAll
public Stream<JsPair> streamAll()
Description copied from interface:Json
Returns a stream over all the pairs of elements in this json object.
-
tail
public final JsArray 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.
-
toString
public final String toString()
// Single-check idiom Item 83 from effective java
-
union
public final JsArray union(JsArray that, JsArray.TYPE ARRAY_AS)
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()
.- Parameters:
that
- the other arrayARRAY_AS
- option to define if arrays are considered SETS, LISTS OR MULTISET- Returns:
- a new json array of the same type as the inputs (mutable or immutable)
-
unionAll
public final JsArray unionAll(JsArray that)
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.- Parameters:
that
- the other array- Returns:
- a new JsArray of the same type as the inputs (mutable or immutable)
-
-