Package jsonvalues
Class JsPair
- java.lang.Object
-
- jsonvalues.JsPair
-
public final class JsPair extends Object
Immutable pair which represents a JsElem of a Json and its JsPath location: (path, element).
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
consumeIf(Predicate<JsPair> predicate, Consumer<JsPair> consumer)
Consumes this pair if it's evaluated to true o a given predicateboolean
equals(@Nullable Object that)
Returns true if that is a pair and both represents the same element at the same location.int
hashCode()
Returns the hashcode of this pair.<R> R
ifElse(Predicate<? super JsPair> predicate, Function<? super JsPair,R> ifTrue, Function<? super JsPair,R> ifFalse)
Declarative way of implementing an if-else.<T> T
ifJsonElse(BiFunction<JsPath,JsObj,T> ifJsOb, BiFunction<JsPath,JsArray,T> ifJsArr, BiFunction<JsPath,JsElem,T> ifNotJson)
Declarative way of implementing an if(obj)return T; else if(array) return T; else return T; where T is computed by the given functions<T> T
ifJsonElse(BiFunction<JsPath,Json<?>,T> ifJson, BiFunction<JsPath,JsElem,T> ifNotJson)
Declarative way of implementing an if(json)return T; else return T; where T is computed by the given functionsJsPair
mapElem(UnaryOperator<JsElem> map)
Returns a new pair with the same path and a new element result of applying the mapping functionJsPair
mapIfInt(IntUnaryOperator operator)
Declarative way of implementingif(pair.elem.isInt()) return Pair.of(pair.path, pair.elem.asJsInt().map(operator)) else return pair
JsPair
mapIfStr(UnaryOperator<String> fn)
Declarative way of implementingif(pair.elem.isStr()) return Pair.of(pair.path, pair.elem.asJsStr().map(mapFn)) else return pair
JsPair
mapPath(UnaryOperator<JsPath> map)
Returns a new pair with the same element and a new path result of applying the mapping functionstatic JsPair
of(JsPath path, boolean b)
Returns a json pair from the path and the boolean.static JsPair
of(JsPath path, double d)
Returns a json pair from the path and the double.static JsPair
of(JsPath path, int i)
Returns a json pair from the path and the integer.static JsPair
of(JsPath path, long l)
Returns a json pair from the path and the long.static JsPair
of(JsPath path, String s)
Returns a json pair from the path and the string.static JsPair
of(JsPath path, BigDecimal bd)
Returns a json pair from the path and the big decimal.static JsPair
of(JsPath path, BigInteger bi)
Returns a json pair from the path and the big integer.static JsPair
of(JsPath path, JsElem elem)
Returns a json pair from the path and the json element.String
toString()
-
-
-
Method Detail
-
mapIfInt
public JsPair mapIfInt(IntUnaryOperator operator)
Declarative way of implementingif(pair.elem.isInt()) return Pair.of(pair.path, pair.elem.asJsInt().map(operator)) else return pair
- Parameters:
operator
- the function to be applied to map the integer- Returns:
- the same this instance if the JsElem is not a JsInt or a new pair
-
mapIfStr
public JsPair mapIfStr(UnaryOperator<String> fn)
Declarative way of implementingif(pair.elem.isStr()) return Pair.of(pair.path, pair.elem.asJsStr().map(mapFn)) else return pair
- Parameters:
fn
- the function to be applied to map the string of the JsStr- Returns:
- the same this instance if the JsElem is not a JsStr or a new pair
-
of
public static JsPair of(JsPath path, JsElem elem)
Returns a json pair from the path and the json element.- Parameters:
path
- the JsPath objectelem
- the JsElem- Returns:
- an immutable JsPair
-
of
public static JsPair of(JsPath path, int i)
Returns a json pair from the path and the integer.- Parameters:
path
- the JsPathi
- the integer- Returns:
- an immutable JsPair
-
of
public static JsPair of(JsPath path, double d)
Returns a json pair from the path and the double.- Parameters:
path
- the JsPathd
- the double- Returns:
- an immutable JsPair
-
of
public static JsPair of(JsPath path, long l)
Returns a json pair from the path and the long.- Parameters:
path
- the JsPathl
- the long- Returns:
- an immutable JsPair
-
of
public static JsPair of(JsPath path, boolean b)
Returns a json pair from the path and the boolean.- Parameters:
path
- the JsPathb
- the boolean- Returns:
- an immutable JsPair
-
of
public static JsPair of(JsPath path, String s)
Returns a json pair from the path and the string.- Parameters:
path
- the JsPaths
- the string- Returns:
- an immutable JsPair
-
of
public static JsPair of(JsPath path, BigDecimal bd)
Returns a json pair from the path and the big decimal.- Parameters:
path
- the JsPathbd
- the big decimal- Returns:
- an immutable JsPair
-
of
public static JsPair of(JsPath path, BigInteger bi)
Returns a json pair from the path and the big integer.- Parameters:
path
- the JsPathbi
- the big integer- Returns:
- an immutable JsPair
-
toString
public String toString()
-
equals
public boolean equals(@Nullable Object that)
Returns true if that is a pair and both represents the same element at the same location.
-
hashCode
public int hashCode()
Returns the hashcode of this pair.
-
mapElem
public JsPair mapElem(UnaryOperator<JsElem> map)
Returns a new pair with the same path and a new element result of applying the mapping function- Parameters:
map
- the mapping function which maps the JsElem- Returns:
- a new JsPair
-
mapPath
public JsPair mapPath(UnaryOperator<JsPath> map)
Returns a new pair with the same element and a new path result of applying the mapping function- Parameters:
map
- the mapping function which maps the JsPath- Returns:
- a new JsPair
-
ifJsonElse
public <T> T ifJsonElse(BiFunction<JsPath,Json<?>,T> ifJson, BiFunction<JsPath,JsElem,T> ifNotJson)
Declarative way of implementing an if(json)return T; else return T; where T is computed by the given functions- Type Parameters:
T
- type of the result- Parameters:
ifJson
- function that returns a T and is invoked if the element of this pair is a jsonifNotJson
- function that returns a T and is invoked if the element of this pair is not a json- Returns:
- object of type T
-
ifJsonElse
public <T> T ifJsonElse(BiFunction<JsPath,JsObj,T> ifJsOb, BiFunction<JsPath,JsArray,T> ifJsArr, BiFunction<JsPath,JsElem,T> ifNotJson)
Declarative way of implementing an if(obj)return T; else if(array) return T; else return T; where T is computed by the given functions- Type Parameters:
T
- type of the result- Parameters:
ifJsOb
- function that returns a T and is invoked if the element of this pair is a json objectifJsArr
- function that returns a T and is invoked if the element of this pair is not a json arrayifNotJson
- function that returns a T and is invoked if the element of this pair is not a json- Returns:
- object of type T
-
ifElse
public <R> R ifElse(Predicate<? super JsPair> predicate, Function<? super JsPair,R> ifTrue, Function<? super JsPair,R> ifFalse)
Declarative way of implementing an if-else. This pair is tested on a given predicate, executing the ifTrue function when true and the ifFalse function otherwise- Type Parameters:
R
- the type of the returned value- Parameters:
predicate
- the given predicateifTrue
- function to invoked when the predicate is evaluated to true, taking this pair as a parameterifFalse
- function to invoked when the predicate is evaluated to false, taking this pair as a parameter- Returns:
- an object of type R
-
-