All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jsonvalues.MatchExp Maven / Gradle / Ivy

package jsonvalues;


import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;

/**
 Java doesn't support Pattern Matching but we can implement some matching expressions using high
 order functions.
 */
final class MatchExp {

    private MatchExp() {
    }


    /**
     return a matching expression to extract objs and arrays out of json elements.

     @param ifObj   function to be applied if the JsValue is a JsObj
     @param ifArr   function to be applied if the JsValue is not a JsArr
     @param ifValue function to be applied if the JsValue is not a Json
     @param      the type of the result
     @return a function that takes a JsValue and returns an object of type T
     */
    public static  Function ifJsonElse(final Function ifObj,
                                                      final Function ifArr,
                                                      final Function ifValue
                                                     ) {

        return elem ->
        {
            if (elem.isObj()) return requireNonNull(ifObj).apply(elem.toJsObj());
            if (elem.isArray()) return requireNonNull(ifArr).apply(elem.toJsArray());
            return ifValue.apply(elem);
        };
    }

    /**
     return a matching expression to extract jsons out of json elements.

     @param ifJson    function to be applied if the JsValue is a Json
     @param ifNotJson function to be applied if the JsValue is not a Json
     @param        the type of the result
     @return a function that takes a JsValue and returns an object of type T
     */
    public static  Function ifJsonElse(final Function, T> ifJson,
                                                      final Function ifNotJson
                                                     ) {
        return elem -> requireNonNull(elem).isJson() ? requireNonNull(ifJson).apply(elem.toJson()) : requireNonNull(ifNotJson).apply(elem);
    }

    /**
     return a matching expression to extract JsNothing out of json elements.

     @param nothingSupplier supplier to be invoked if the JsValue is JsNothing
     @param elseFn          function to be applied if the JsValue is not JsNothing
     @param              the type of the result
     @return a function that takes a JsValue and returns an object of type T
     */
    public static  Function ifNothingElse(final Supplier nothingSupplier,
                                                         final Function elseFn
                                                        ) {
        return elem -> elem.isNothing() ? requireNonNull(nothingSupplier).get() : requireNonNull(elseFn).apply(elem);
    }

    /**
     return a matching expression to extract json objects out of json elements.

     @param ifObj    function to be applied if the JsValue is a JsObj
     @param ifNotObj function to be applied if the JsValue is not a JsObj
     @param       the type of the result
     @return a function that takes a JsValue and returns an object of type T
     */
    public static  Function ifObjElse(final Function ifObj,
                                                     final Function ifNotObj
                                                    ) {
        return elem ->
        {
            if (elem.isObj()) return requireNonNull(ifObj).apply(elem.toJsObj());
            else return requireNonNull(ifNotObj).apply(elem);
        };
    }

    /**
     declarative way of implementing an if-else using high order functions

     @param predicate the condition that will be tested on the json element
     @param ifTrue    the function to be applied if the predicate is evaluated to true
     @param ifFalse   the function to be applied if the predicate is evaluated to false
     @param        the type of the result
     @return a function that takes a JsValue and returns an object of type T
     */
    public static  Function ifPredicateElse(final Predicate predicate,
                                                           final Function ifTrue,
                                                           final Function ifFalse
                                                          ) {
        return elem ->
        {
            if (requireNonNull(predicate).test(elem)) return requireNonNull(ifTrue).apply(elem);
            return requireNonNull(ifFalse).apply(elem);
        };
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy