Package jsonvalues

Class MatchExp


  • public class MatchExp
    extends Object
    Java doesn't support Pattern Matching but we can implement some matching expressions using high order functions.
    • Method Detail

      • accept

        public static Consumer<JsElem> accept​(Consumer<JsElem> 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 JsValue
        ifObj - the consumer to be invoked if this JsElem is a JsObj
        ifArray - the consumer to be invoked if this JsElem is a JsArray
        Returns:
        consumer of a json element
      • ifArrElse

        public static <T> Function<JsElem,​T> ifArrElse​(Function<? super JsArray,​T> ifArr,
                                                             Function<? super JsElem,​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 JsArray
        ifNotArr - 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<JsElem,​T> ifBoolElse​(Function<? super Boolean,​T> ifBoolean,
                                                              Function<? super JsElem,​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 JsBool
        ifNotBoolean - 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<JsElem,​T> ifDecimalElse​(DoubleFunction<T> ifDouble,
                                                                 Function<BigDecimal,​T> ifBigDecimal,
                                                                 Function<? super JsElem,​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 JsDouble
        ifBigDecimal - the function to be applied if this JsElem is a JsBigDec
        ifOther - 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<JsElem,​T> ifIntegralElse​(IntFunction<T> ifInt,
                                                                  LongFunction<T> ifLong,
                                                                  Function<BigInteger,​T> ifBigInt,
                                                                  Function<? super JsElem,​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 JsInt
        ifLong - function to be applied if the JsElem is a JsLong
        ifBigInt - function to be applied if the JsElem is a JsBigInt
        ifOther - 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<JsElem,​T> ifJsonElse​(Function<? super JsObj,​T> ifObj,
                                                              Function<? super JsArray,​T> ifArr,
                                                              Function<? super JsElem,​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 JsObj
        ifArr - function to be applied if the JsElem is not a JsArr
        ifValue - 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<JsElem,​T> ifJsonElse​(Function<Json<?>,​T> ifJson,
                                                              Function<JsElem,​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 Json
        ifNotJson - 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<JsElem,​T> ifNothingElse​(Supplier<T> nothingSupplier,
                                                                 Function<JsElem,​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 JsNothing
        elseFn - 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<JsElem,​T> ifObjElse​(Function<? super JsObj,​T> ifObj,
                                                             Function<? super JsElem,​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 JsObj
        ifNotObj - 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<JsElem,​T> ifPredicateElse​(Predicate<JsElem> predicate,
                                                                   Function<JsElem,​T> ifTrue,
                                                                   Function<JsElem,​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 element
        ifTrue - the function to be applied if the predicate is evaluated to true
        ifFalse - 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<JsElem,​T> ifStrElse​(Function<? super String,​T> ifStr,
                                                             Function<? super JsElem,​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 JsStr
        ifNotStr - 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