Java1.8 工具类 函数式编程去掉if else

2021/12/11 17:19:53

本文主要是介绍Java1.8 工具类 函数式编程去掉if else,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

package com.ele.util;

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

/**
 * ele
 */
public interface IIfUtil {

    IIfUtil build = new IIfUtil() {};

    /**
     * 当content满足rule之后将会作为参数执行then并返回执行结果,否则将不会执行then并返回null
     * @param then
     * @param param
     * @param rule
     * @param <P>
     * @param <R>
     * @return
     */
    default <P,R> R thenIf(Function<P,R> then, P param, Predicate<P> rule) {
        return rule.test(param) ? then.apply(param) : null;
    }

    default <P,R> R ifThen(P param, Predicate<P> rule, Function<P,R> then) {
        return rule.test(param) ? then.apply(param) : null;
    }

    default <T,R> R ifOrElseFun(T param, Predicate<T> rule, Function<T,R> then, Function<T,R> orElse) {
        return rule.test(param) ? then.apply(param) : orElse.apply(param);
    }

    default <R> R ifOrElseGet(boolean rule, Supplier<R> then, Supplier<R> orElse) {
        return rule ? then.get() : orElse.get();
    }

    default <T> void ifOrElseVoid(T param, Predicate<T> rule, Consumer<T> then, Consumer<T> orElse) {
        if (rule.test(param)) {
            then.accept(param);
        } else {
            orElse.accept(param);
        }
    }


    default <P> void elseIfVoidByKey(P param, boolean ifRule, Consumer<P> ifThen, boolean elseRule, Consumer<P> elseThen) {
        if (ifRule) {
            ifThen.accept(param);
        } else if (elseRule) {
            elseThen.accept(param);
        }
    }

    default <P> void elseIfVoidByKey(P param, boolean ifRule, Consumer<P> ifThen, boolean elseRule, Consumer<P> elseThen, Consumer<P> defaultThen) {
        if (ifRule) {
            ifThen.accept(param);
        } else if (elseRule) {
            elseThen.accept(param);
        } else {
            defaultThen.accept(param);
        }
    }

    default <P,R> R elseIfFunByKey(P param, boolean ifRule, Function<P,R> ifThen, boolean elseRule, Function<P,R> elseThen) {
        return elseIfFunDefaultValByKey(param, ifRule, ifThen, elseRule, elseThen, null);
    }

    default <P,R> R elseIfFunDefaultValByKey(P param, boolean ifRule, Function<P,R> ifThen, boolean elseRule, Function<P,R> elseThen, R defaultVal) {
        if (ifRule) {
            return ifThen.apply(param);
        } else if (elseRule) {
            return elseThen.apply(param);
        } else {
            return defaultVal;
        }
    }

    default <P,R> R elseIfFunDefaultFunByKey(P param, boolean ifRule, Function<P,R> ifThen, boolean elseRule, Function<P,R> elseThen, Function<P,R> defaultThen) {
        if (ifRule) {
            return ifThen.apply(param);
        } else if (elseRule) {
            return elseThen.apply(param);
        } else {
            return defaultThen.apply(param);
        }
    }

    default <R> R elseIfGetByKey(boolean ifRule, Supplier<R> ifThen, boolean elseRule, Supplier<R> elseThen) {
        return elseIfGetDefaultValByKey(ifRule, ifThen, elseRule, elseThen, null);
    }

    default <R> R elseIfGetDefaultValByKey(boolean ifRule, Supplier<R> ifThen, boolean elseRule, Supplier<R> elseThen, R defaultVal) {
        if (ifRule) {
            return ifThen.get();
        } else if (elseRule) {
            return elseThen.get();
        } else {
            return defaultVal;
        }
    }

    default <R> R elseIfGetDefaultGetByKey(boolean ifRule, Supplier<R> ifThen, boolean elseRule, Supplier<R> elseThen, Supplier<R> defaultGet) {
        if (ifRule) {
            return ifThen.get();
        } else if (elseRule) {
            return elseThen.get();
        } else {
            return defaultGet.get();
        }
    }


    default <P> void elseIfVoidByFields(P param, Predicate<P> ifRule, Consumer<P> ifThen, Predicate<P> elseRule, Consumer<P> elseThen) {
        if (ifRule.test(param)) {
            ifThen.accept(param);
        } else if (elseRule.test(param)) {
            elseThen.accept(param);
        }
    }

    default <P> void elseIfVoidByFields(P param, Predicate<P> ifRule, Consumer<P> ifThen, Predicate<P> elseRule, Consumer<P> elseThen, Consumer<P> defaultThen) {
        if (ifRule.test(param)) {
            ifThen.accept(param);
        } else if (elseRule.test(param)) {
            elseThen.accept(param);
        } else {
            defaultThen.accept(param);
        }
    }

    default <P,R> R elseIfFunByFields(P param, Predicate<P> ifRule, Function<P,R> ifThen, Predicate<P> elseRule, Function<P,R> elseThen) {
        return elseIfFunDefaultValByFields(param, ifRule, ifThen, elseRule, elseThen, null);
    }

    default <P,R> R elseIfFunDefaultValByFields(P param, Predicate<P> ifRule, Function<P,R> ifThen, Predicate<P> elseRule, Function<P,R> elseThen, R defaultVal) {
        if (ifRule.test(param)) {
            return ifThen.apply(param);
        } else if (elseRule.test(param)) {
            return elseThen.apply(param);
        } else {
            return defaultVal;
        }
    }

    default <P,R> R elseIfFunDefaultFunByFields(P param, Predicate<P> ifRule, Function<P,R> ifThen, Predicate<P> elseRule, Function<P,R> elseThen, Function<P,R> defaultThen) {
        if (ifRule.test(param)) {
            return ifThen.apply(param);
        } else if (elseRule.test(param)) {
            return elseThen.apply(param);
        } else {
            return defaultThen.apply(param);
        }
    }

    default <R> R elseIfGetByTest(Supplier<Boolean> ifRule, Supplier<R> ifThen, Supplier<Boolean> elseRule, Supplier<R> elseThen) {
        return elseIfGetDefaultValByTest(ifRule, ifThen, elseRule, elseThen, null);
    }

    default <R> R elseIfGetDefaultValByTest(Supplier<Boolean> ifRule, Supplier<R> ifThen, Supplier<Boolean> elseRule, Supplier<R> elseThen, R defaultVal) {
        if (ifRule.get()) {
            return ifThen.get();
        } else if (elseRule.get()) {
            return elseThen.get();
        } else {
            return defaultVal;
        }
    }

    default <R> R elseIfGetDefaultGetByTest(Supplier<Boolean> ifRule, Supplier<R> ifThen, Supplier<Boolean> elseRule, Supplier<R> elseThen, Supplier<R> defaultGet) {
        if (ifRule.get()) {
            return ifThen.get();
        } else if (elseRule.get()) {
            return elseThen.get();
        } else {
            return defaultGet.get();
        }
    }
}
package com.ele.util;

import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * ele
 */
public interface IMapIfUtil {

    IMapIfUtil build = new IMapIfUtil() {};

    default <K, R> R mapIfFunDefaultNull(K key, Map<K, Function<K, R>> ruleMap) {
        return mapIfFunDefaultVal(key, null, ruleMap);
    }

    default <K, R> R mapIfFunDefaultVal(K key, R defaultVal, Map<K, Function<K, R>> ruleMap) {
        return ruleMap.containsKey(key) ? ruleMap.get(key).apply(key) : defaultVal;
    }

    default <K, R> R mapIfFunDefaultFun(K key, Function<K, R> defaultFun, Map<K, Function<K, R>> ruleMap) {
        return ruleMap.containsKey(key) ? ruleMap.get(key).apply(key) : defaultFun.apply(key);
    }

    default <K> void mapIfVoid(K key, Map<K, Consumer<K>> ruleMap) {
        if (ruleMap.containsKey(key)) {
            ruleMap.get(key).accept(key);
        }
    }

    default <K> void mapIfVoidDefault(K key, Consumer<K> defaultFun, Map<K, Consumer<K>> ruleMap) {
        if (ruleMap.containsKey(key)) {
            ruleMap.get(key).accept(key);
        } else {
            defaultFun.accept(key);
        }
    }

    default <K, R> R mapIfGetDefaultNull(K key, Map<K, Supplier<R>> ruleMap) {
        return mapIfGetDefaultVal(key, null, ruleMap);
    }

    default <K, R> R mapIfGetDefaultVal(K key, R defaultVal, Map<K, Supplier<R>> ruleMap) {
        return ruleMap.containsKey(key) ? ruleMap.get(key).get() : defaultVal;
    }

    default <K, R> R mapIfGetDefaultGet(K key, Supplier<R> defaultGet, Map<K, Supplier<R>> ruleMap) {
        return ruleMap.containsKey(key) ? ruleMap.get(key).get() : defaultGet.get();
    }

    default <K, P, R> R mapIfFunDefaultNullByKey(P param, K key, Map<K, Function<P, R>> ruleMap) {
        return mapIfFunDefaultValByKey(param, key, null, ruleMap);
    }

    default <K, P, R> R mapIfFunDefaultValByKey(P param, K key, R defaultVal, Map<K, Function<P, R>> ruleMap) {
        return ruleMap.containsKey(key) ? ruleMap.get(key).apply(param) : defaultVal;
    }

    default <K, P, R> R mapIfFunDefaultFunByKey(P param, K key, Function<P, R> defaultFun, Map<K, Function<P, R>> ruleMap) {
        return ruleMap.containsKey(key) ? ruleMap.get(key).apply(param) : defaultFun.apply(param);
    }

    default <K, P> void mapIfVoidByKey(P param, K key, Map<K, Consumer<P>> ruleMap) {
        if (ruleMap.containsKey(key)) {
            ruleMap.get(key).accept(param);
        }
    }

    default <K, P> void mapIfVoidDefaultByKey(P param, K key, Consumer<P> defaultFun, Map<K, Consumer<P>> ruleMap) {
        if (ruleMap.containsKey(key)) {
            ruleMap.get(key).accept(param);
        } else {
            defaultFun.accept(param);
        }
    }

    default <K, P extends Object, R> R mapIfFunDefaultNullByField(P param, Function<P,K> fieldKey, Map<K, Function<P, R>> ruleMap) {
        return mapIfFunDefaultValByField(param, fieldKey, null, ruleMap);
    }

    default <K, P extends Object, R> R mapIfFunDefaultValByField(P param, Function<P,K> fieldKey, R defaultVal, Map<K, Function<P, R>> ruleMap) {
        K k = fieldKey.apply(param);
        return ruleMap.containsKey(k) ? ruleMap.get(k).apply(param) : defaultVal;
    }

    default <K, P extends Object, R> R mapIfFunDefaultFunByField(P param, Function<P,K> fieldKey, Function<P, R> defaultFun, Map<K, Function<P, R>> ruleMap) {
        K k = fieldKey.apply(param);
        return ruleMap.containsKey(k) ? ruleMap.get(k).apply(param) : defaultFun.apply(param);
    }

    default <K, P extends Object> void mapIfVoidByField(P param, Function<P,K> fieldKey, Map<K, Consumer<P>> ruleMap) {
        K k = fieldKey.apply(param);
        if (ruleMap.containsKey(k)) {
            ruleMap.get(k).accept(param);
        }
    }

    default <K, P extends Object> void mapIfVoidDefaultByField(P param, Function<P,K> fieldKey, Consumer<P> defaultFun, Map<K, Consumer<P>> ruleMap) {
        K k = fieldKey.apply(param);
        if (ruleMap.containsKey(k)) {
            ruleMap.get(k).accept(param);
        } else {
            defaultFun.accept(param);
        }
    }
}
package com.ele.util;

import javafx.util.Pair;

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

/**
 * ele
 */
public interface IPairIfUtil {

    IPairIfUtil build = new IPairIfUtil() {};

    ///
    default <P> void pairsIfVoidByKey(P param, Pair<Boolean, Consumer<P>>... then) {
        pairsIfVoidByKey(param, null, then);
    }

    default <P> void pairsIfVoidByKey(P param, Consumer<P> defaultThen, Pair<Boolean, Consumer<P>> ... then) {
        boolean thenExecute = false;
        for (Pair<Boolean, Consumer<P>> pair : then) {
            if (pair.getKey()) {
                thenExecute = true;
                pair.getValue().accept(param);
            }
        }
        if (!thenExecute && Objects.nonNull(defaultThen)) {
            defaultThen.accept(param);
        }
    }

    default <P,R> R pairsIfFunByKey(P param, Pair<Boolean, Function<P,R>> ... then) {
        return pairsIfFunDefaultValByKey(param, null, then);
    }

    default <P,R> R pairsIfFunDefaultValByKey(P param, R defaultVal, Pair<Boolean, Function<P,R>> ... then) {
        for (Pair<Boolean, Function<P,R>> pair : then) {
            if (pair.getKey()) {
                return pair.getValue().apply(param);
            }
        }
        return defaultVal;
    }

    default <P,R> R pairsIfFunDefaultFunByKey(P param, Function<P,R> defaultThen, Pair<Boolean, Function<P,R>> ... then) {
        for (Pair<Boolean, Function<P,R>> pair : then) {
            if (pair.getKey()) {
                return pair.getValue().apply(param);
            }
        }
        return defaultThen.apply(param);
    }


    default <P> void pairsIfVoidByFields(P param, Pair<Predicate<P>, Consumer<P>> ... then) {
        pairsIfVoidByFields(param, null, then);
    }

    default <P> void pairsIfVoidByFields(P param, Consumer<P> defaultThen, Pair<Predicate<P>, Consumer<P>> ... then) {
        boolean thenExecute = false;
        for (Pair<Predicate<P>, Consumer<P>> pair : then) {
            if (pair.getKey().test(param)) {
                thenExecute = true;
                pair.getValue().accept(param);
            }
        }
        if (!thenExecute && Objects.nonNull(defaultThen)) {
            defaultThen.accept(param);
        }
    }

    default <P,R> R pairsIfFunByFields(P param, Pair<Predicate<P>, Function<P,R>> ... then) {
        return pairsIfFunDefaultValByFields(param, null, then);
    }

    default <P,R> R pairsIfFunDefaultValByFields(P param, R defaultVal, Pair<Predicate<P>, Function<P,R>> ... then) {
        for (Pair<Predicate<P>, Function<P, R>> pair : then) {
            if (pair.getKey().test(param)) {
                return pair.getValue().apply(param);
            }
        }
        return defaultVal;
    }

    default <P,R> R pairsIfFunDefaultFunByFields(P param, Function<P,R> defaultThen, Pair<Predicate<P>, Function<P,R>> ... then) {
        for (Pair<Predicate<P>, Function<P, R>> pair : then) {
            if (pair.getKey().test(param)) {
                return pair.getValue().apply(param);
            }
        }
        return defaultThen.apply(param);
    }

    default <R> R pairsIfGetByTest(Pair<Supplier<Boolean>, Supplier<R>> ... then) {
        return pairsIfGetDefaultValByTest(null, then);
    }

    default <R> R pairsIfGetDefaultValByTest(R defaultVal, Pair<Supplier<Boolean>, Supplier<R>> ... then) {
        for (Pair<Supplier<Boolean>, Supplier<R>> pair : then) {
            if (pair.getKey().get()) {
                return pair.getValue().get();
            }
        }
        return defaultVal;
    }

    default <R> R pairsIfGetDefaultGetByTest(Supplier<R> elseThen, Supplier<R> defaultGet, Pair<Supplier<Boolean>, Supplier<R>> ... then) {
        for (Pair<Supplier<Boolean>, Supplier<R>> pair : then) {
            if (pair.getKey().get()) {
                return pair.getValue().get();
            }
        }
        return defaultGet.get();
    }

}
package com.ele.util;

/**
 * ele
 */
public interface IIfUtils extends IIfUtil, IPairIfUtil, IMapIfUtil {

    IIfUtils build = new IIfUtils() {};

}



这篇关于Java1.8 工具类 函数式编程去掉if else的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程