Package jsonvalues
Class MatchExp
- java.lang.Object
-
- jsonvalues.MatchExp
-
public final class MatchExp extends Object
Java doesn't support Pattern Matching but we can implement some matching expressions using high order functions.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Consumer<JsValue>
accept(Consumer<JsValue> ifValue, Consumer<JsObj> ifObj, Consumer<JsArray> ifArray)
Declarative way of consuming an element based on its typestatic <T> Function<JsValue,T>
ifArrElse(Function<? super JsArray,T> ifArr, Function<? super JsValue,T> ifNotArr)
return a matching expression to extract arrays out of json elements.static <T> Function<JsValue,T>
ifBoolElse(Function<? super Boolean,T> ifBoolean, Function<? super JsValue,T> ifNotBoolean)
return a matching expression to extract booleans out of json elements.static <T> Function<JsValue,T>
ifDecimalElse(DoubleFunction<T> ifDouble, Function<BigDecimal,T> ifBigDecimal, Function<? super JsValue,T> ifOther)
return a matching expression to extract decimal numbers out of json elements.static <T> Function<JsValue,T>
ifIntegralElse(IntFunction<T> ifInt, LongFunction<T> ifLong, Function<BigInteger,T> ifBigInt, Function<? super JsValue,T> ifOther)
return a matching expression to extract integral numbers out of json elements.static <T> Function<JsValue,T>
ifJsonElse(Function<? super JsObj,T> ifObj, Function<? super JsArray,T> ifArr, Function<? super JsValue,T> ifValue)
return a matching expression to extract objs and arrays out of json elements.static <T> Function<JsValue,T>
ifJsonElse(Function<Json<?>,T> ifJson, Function<JsValue,T> ifNotJson)
return a matching expression to extract jsons out of json elements.static <T> Function<JsValue,T>
ifNothingElse(Supplier<T> nothingSupplier, Function<JsValue,T> elseFn)
return a matching expression to extract JsNothing out of json elements.static <T> Function<JsValue,T>
ifObjElse(Function<? super JsObj,T> ifObj, Function<? super JsValue,T> ifNotObj)
return a matching expression to extract json objects out of json elements.static <T> Function<JsValue,T>
ifPredicateElse(Predicate<JsValue> predicate, Function<JsValue,T> ifTrue, Function<JsValue,T> ifFalse)
declarative way of implementing an if-else using high order functionsstatic <T> Function<JsValue,T>
ifStrElse(Function<? super String,T> ifStr, Function<? super JsValue,T> ifNotStr)
returns a matching expression to extract strings out of json elements.
-
-
-
Method Detail
-
accept
public static Consumer<JsValue> accept(Consumer<JsValue> ifValue, Consumer<JsObj> ifObj, Consumer<JsArray> ifArray)
Declarative way of consuming an element based on its type- Parameters:
ifValue
- the consumer to be invoked if this JsElem is a JsValueifObj
- the consumer to be invoked if this JsElem is a JsObjifArray
- the consumer to be invoked if this JsElem is a JsArray- Returns:
- consumer of a json element
-
ifArrElse
public static <T> Function<JsValue,T> ifArrElse(Function<? super JsArray,T> ifArr, Function<? super JsValue,T> ifNotArr)
return a matching expression to extract arrays out of json elements.- Type Parameters:
T
- the type of the object returned- Parameters:
ifArr
- the function to be applied if this JsElem is a JsArrayifNotArr
- the function to be applied if this JsElem is not a JsArray- Returns:
- a function that takes a JsElem and returns an object of type T
-
ifBoolElse
public static <T> Function<JsValue,T> ifBoolElse(Function<? super Boolean,T> ifBoolean, Function<? super JsValue,T> ifNotBoolean)
return a matching expression to extract booleans out of json elements.- Type Parameters:
T
- the type of the object returned- Parameters:
ifBoolean
- the function to be applied if this JsElem is a JsBoolifNotBoolean
- the function to be applied if this JsElem is not a JsBool- Returns:
- a function that takes a JsElem and returns an object of type T
-
ifDecimalElse
public static <T> Function<JsValue,T> ifDecimalElse(DoubleFunction<T> ifDouble, Function<BigDecimal,T> ifBigDecimal, Function<? super JsValue,T> ifOther)
return a matching expression to extract decimal numbers out of json elements.- Type Parameters:
T
- the type of the object returned- Parameters:
ifDouble
- the function to be applied if this JsElem is a JsDoubleifBigDecimal
- the function to be applied if this JsElem is a JsBigDecifOther
- the function to be applied if this JsElem is a not a decimal JsNumber- Returns:
- a function that takes a JsElem and returns an object of type T
-
ifIntegralElse
public static <T> Function<JsValue,T> ifIntegralElse(IntFunction<T> ifInt, LongFunction<T> ifLong, Function<BigInteger,T> ifBigInt, Function<? super JsValue,T> ifOther)
return a matching expression to extract integral numbers out of json elements.- Type Parameters:
T
- the type of the result- Parameters:
ifInt
- function to be applied if the JsElem is a JsIntifLong
- function to be applied if the JsElem is a JsLongifBigInt
- function to be applied if the JsElem is a JsBigIntifOther
- function to be applied if the JsElem is a not an integral number- Returns:
- a function that takes a JsElem and returns an object of type T
-
ifJsonElse
public static <T> Function<JsValue,T> ifJsonElse(Function<? super JsObj,T> ifObj, Function<? super JsArray,T> ifArr, Function<? super JsValue,T> ifValue)
return a matching expression to extract objs and arrays out of json elements.- Type Parameters:
T
- the type of the result- Parameters:
ifObj
- function to be applied if the JsElem is a JsObjifArr
- function to be applied if the JsElem is not a JsArrifValue
- function to be applied if the JsElem is not a Json- Returns:
- a function that takes a JsElem and returns an object of type T
-
ifJsonElse
public static <T> Function<JsValue,T> ifJsonElse(Function<Json<?>,T> ifJson, Function<JsValue,T> ifNotJson)
return a matching expression to extract jsons out of json elements.- Type Parameters:
T
- the type of the result- Parameters:
ifJson
- function to be applied if the JsElem is a JsonifNotJson
- function to be applied if the JsElem is not a Json- Returns:
- a function that takes a JsElem and returns an object of type T
-
ifNothingElse
public static <T> Function<JsValue,T> ifNothingElse(Supplier<T> nothingSupplier, Function<JsValue,T> elseFn)
return a matching expression to extract JsNothing out of json elements.- Type Parameters:
T
- the type of the result- Parameters:
nothingSupplier
- supplier to be invoked if the JsElem is JsNothingelseFn
- function to be applied if the JsElem is not JsNothing- Returns:
- a function that takes a JsElem and returns an object of type T
-
ifObjElse
public static <T> Function<JsValue,T> ifObjElse(Function<? super JsObj,T> ifObj, Function<? super JsValue,T> ifNotObj)
return a matching expression to extract json objects out of json elements.- Type Parameters:
T
- the type of the result- Parameters:
ifObj
- function to be applied if the JsElem is a JsObjifNotObj
- function to be applied if the JsElem is not a JsObj- Returns:
- a function that takes a JsElem and returns an object of type T
-
ifPredicateElse
public static <T> Function<JsValue,T> ifPredicateElse(Predicate<JsValue> predicate, Function<JsValue,T> ifTrue, Function<JsValue,T> ifFalse)
declarative way of implementing an if-else using high order functions- Type Parameters:
T
- the type of the result- Parameters:
predicate
- the condition that will be tested on the json elementifTrue
- the function to be applied if the predicate is evaluated to trueifFalse
- the function to be applied if the predicate is evaluated to false- Returns:
- a function that takes a JsElem and returns an object of type T
-
ifStrElse
public static <T> Function<JsValue,T> ifStrElse(Function<? super String,T> ifStr, Function<? super JsValue,T> ifNotStr)
returns a matching expression to extract strings out of json elements.- Type Parameters:
T
- the type of the result- Parameters:
ifStr
- the function to be applied if the JsElem is a JsStrifNotStr
- the function to be applied if the JsElem is not a JsStr- Returns:
- a function that takes a JsElem and returns an object of type T
-
-