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,Json<?>,T> ifJson, BiFunction<JsPath,JsElem,T> ifNotJson)
JsPair
mapElem(UnaryOperator<JsElem> map)
Returns a new pair with the same path and a new element result of applying the mapping functionstatic Function<JsPair,JsPair>
mapIfInt(IntUnaryOperator operator)
Declarative way of implementingif(pair.elem.isInt()) return Pair.parse(pair.path, pair.elem.asJsInt().map(operator)) else return pair
static Function<JsPair,JsPair>
mapIfStr(UnaryOperator<String> fn)
Declarative way of implementingif(pair.elem.isStr()) return Pair.parse(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(String path, boolean b)
Returns a json pair from the path-like string and the boolean.static JsPair
of(String path, double d)
Returns a json pair from the path-like string and the double.static JsPair
of(String path, int i)
Returns a json pair from the path-like string and the integer.static JsPair
of(String path, long l)
Returns a json pair from the path-like string and the long.static JsPair
of(String path, String s)
Returns a json pair from the path-like string and the string.static JsPair
of(String path, BigDecimal bd)
Returns a json pair from the path-like string and the big decimal.static JsPair
of(String path, BigInteger bi)
Returns a json pair from the path-like string and the big integer.static JsPair
of(String path, JsElem elem)
Returns a json pair from the path-like string and the json element.static 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 static Function<JsPair,JsPair> mapIfInt(IntUnaryOperator operator)
Declarative way of implementingif(pair.elem.isInt()) return Pair.parse(pair.path, pair.elem.asJsInt().map(operator)) else return pair
Examples:
JsPair pair = JsPair.parse(JsPath.parse("a.b"),JsInt.parse(1)) pair.mapIfLong(l->l+10) // ('a'.'b', 11) JsPair pair1 = JsPair.parse(JsPath.parse("a.b"),JsStr.parse("a")) pair1.mapIfLong(l->l+10).equals(pair1) // true, same pair is returned
- 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 static Function<JsPair,JsPair> mapIfStr(UnaryOperator<String> fn)
Declarative way of implementingif(pair.elem.isStr()) return Pair.parse(pair.path, pair.elem.asJsStr().map(mapFn)) else return pair
Examples:
JsPair pair = JsPair.parse(JsPath.parse("a.b"),JsStr.parse("a")) pair.mapIfStr(String::toUpperCase) // ('a'.'b', "A") JsPair pair1 = JsPair.parse(JsPath.parse("a.b"),JsInt.parse(1)) pair1.mapIfStr(String::toUpperCase).equals(pair1) // true, same pair is returned
- 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(String path, JsElem elem)
Returns a json pair from the path-like string and the json element.- Parameters:
path
- the path-like stringelem
- the JsElem- Returns:
- an immutable JsPair
-
of
public static JsPair of(String path, int i)
Returns a json pair from the path-like string and the integer.- Parameters:
path
- the path-like stringi
- the integer- Returns:
- an immutable JsPair
-
of
public static JsPair of(String path, double d)
Returns a json pair from the path-like string and the double.- Parameters:
path
- the path-like stringd
- the double- Returns:
- an immutable JsPair
-
of
public static JsPair of(String path, long l)
Returns a json pair from the path-like string and the long.- Parameters:
path
- the path-like stringl
- the long- Returns:
- an immutable JsPair
-
of
public static JsPair of(String path, boolean b)
Returns a json pair from the path-like string and the boolean.- Parameters:
path
- the path-like stringb
- the boolean- Returns:
- an immutable JsPair
-
of
public static JsPair of(String path, String s)
Returns a json pair from the path-like string and the string.- Parameters:
path
- the path-like strings
- the string- Returns:
- an immutable JsPair
-
of
public static JsPair of(String path, BigDecimal bd)
Returns a json pair from the path-like string and the big decimal.- Parameters:
path
- the path-like stringbd
- the big decimal- Returns:
- an immutable JsPair
-
of
public static JsPair of(String path, BigInteger bi)
Returns a json pair from the path-like string and the big integer.- Parameters:
path
- the path-like stringbi
- the big integer- Returns:
- an immutable JsPair
-
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)
-
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
-
-