基于Java的函数式实现的构建者

该文介绍了使用Java的函数式接口如Supplier、Consumer、Function、BiConsumer和BiFunction来实现构建者模式,提供了不同类型的构建者类,支持在构建过程中添加操作并执行。这些构建者类允许在对象创建后按顺序应用一系列操作,增强了代码的灵活性和可读性。
摘要由CSDN通过智能技术生成

基于Java的函数式实现的构建者

一、基于Java的Supplier实现的构建者

package com.function.bulider;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class SupplierBulider<T> {

    /**
     * 待创建的类型
     */
    private final Supplier<T> supplier;

    /**
     *  存储需要执行的过程
     */
    private final List<Consumer<T>> list = new ArrayList<>();

    public SupplierBulider(Supplier<T> supplier) {
        this.supplier = supplier;
    }

    public static <T> SupplierBulider<T> of(Supplier<T> supplier) {
        return new SupplierBulider<>(supplier);
    }


    /**
     * 把 Value 赋值给 supplier
     *
     * @param biConsumer
     * @param value
     * @param <V>
     * @return
     */
    public <V> SupplierBulider<T> width(BiConsumer<T, V> biConsumer, V value) {
        list.add(t -> biConsumer.accept(t, value));
        return this;
    }

    public <V> SupplierBulider<T> width(BiConsumer<T, V> biConsumer, V value, V orElse) {
        if (null == value) {
            list.add(t -> biConsumer.accept(t, value));
        } else {
            list.add(t -> biConsumer.accept(t, orElse));
        }
        return this;
    }

    public <V> SupplierBulider<T> width(boolean accept, BiConsumer<T, V> biConsumer, V value) {
        if (accept) {
            list.add(t -> biConsumer.accept(t, value));
        }
        return this;
    }

    public <V> SupplierBulider<T> width(boolean accept, BiConsumer<T, V> biConsumer, V value, V orElse) {
        if (accept) {
            list.add(t -> biConsumer.accept(t, value));
        } else {
            list.add(t -> biConsumer.accept(t, orElse));
        }
        return this;
    }

    /**
     * 开始构建
     *
     * @return
     */
    public T build() {
        T t = supplier.get();
        Optional.of(list)
                .orElse(new ArrayList<>())
                .stream()
                .filter(Objects::nonNull)
                .forEach(item -> item.accept(t));
        return t;
    }


    /**
     * 开始构建
     *
     * @return
     */
    public T build(List<Consumer<T>> list) {
        T t = supplier.get();
        Optional.of(list)
                .orElse(new ArrayList<>())
                .stream()
                .filter(Objects::nonNull)
                .forEach(item -> item.accept(t));
        return t;
    }
}

二、基于Java的Consumer实现的构建者

package com.function.bulider;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class ConsumerBulider<T> {
    /**
     * 消费数据
     */
    private final T consumer;

    /**
     * 存储需要执行的过程
     */
    private final List<Consumer<T>> list = new ArrayList<>();

    public ConsumerBulider(T consumer) {
        this.consumer = consumer;
    }

    public static <T> ConsumerBulider<T> of(T consumer) {
        return new ConsumerBulider<>(consumer);
    }


    public <V> ConsumerBulider<T> width(BiConsumer<T, V> biConsumer, V value) {
        list.add(e -> biConsumer.accept(e, value));
        return this;
    }

    public <V> ConsumerBulider<T> width(BiConsumer<T, V> biConsumer, V value, V orElse) {
        if (null == value) {
            list.add(e -> biConsumer.accept(e, value));
        } else {
            list.add(e -> biConsumer.accept(e, orElse));
        }
        return this;
    }

    public <V> ConsumerBulider<T> width(boolean accept, BiConsumer<T, V> biConsumer, V value) {
        if (accept) {
            list.add(e -> biConsumer.accept(e, value));
        }
        return this;
    }

    public <V> ConsumerBulider<T> width(boolean accept, BiConsumer<T, V> biConsumer, V value, V orElse) {
        if (accept) {
            list.add(e -> biConsumer.accept(e, value));
        } else {
            list.add(e -> biConsumer.accept(e, orElse));
        }
        return this;
    }

    public T build() {
        Optional.of(list)
                .orElse(new ArrayList<>())
                .stream()
                .filter(Objects::nonNull)
                .forEach(e -> e.accept(consumer));
        return consumer;
    }

    /**
     * 开始构建
     *
     * @return
     */
    public T build(List<Consumer<T>> list) {
        Optional.of(list)
                .orElse(new ArrayList<>())
                .stream()
                .filter(Objects::nonNull)
                .forEach(e -> e.accept(consumer));
        return consumer;
    }


}

三、基于Java的Function实现的构建者

package com.function.bulider;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class FunctionBulider<T, S> {
    /**
     * 消费数据
     */
    private final S s;
    /**
     * 待创建的类型
     */
    private final Supplier<T> supplier;

    private final List<Consumer<T>> list = new ArrayList<>();

    public FunctionBulider(S s, Supplier<T> supplier) {
        this.s = s;
        this.supplier = supplier;
    }

    /**
     * 静态工厂方法,获取类型转换
     *
     * @param s        数据源
     * @param supplier 目标值
     * @param <T>      目标类型
     * @param <S>      数据源类型
     * @return 构造函数
     */
    public static <T, S> FunctionBulider<T, S> of(S s, Supplier<T> supplier) {
        return new FunctionBulider<>(s, supplier);
    }

    /*** ++++++++++ 引用赋值 ++++++++++ ***/

    public <V> FunctionBulider<T, S> width(BiConsumer<T, V> biConsumer, V value) {
        list.add(t -> biConsumer.accept(t, value));
        return this;
    }

    public <V> FunctionBulider<T, S> width(BiConsumer<T, V> biConsumer, V value, V orElse) {
        if (null == value) {
            list.add(t -> biConsumer.accept(t, value));
        } else {
            list.add(t -> biConsumer.accept(t, orElse));
        }
        return this;
    }

    public <V> FunctionBulider<T, S> width(boolean accept, BiConsumer<T, V> biConsumer, V value) {
        if (accept) {
            list.add(t -> biConsumer.accept(t, value));
        }
        return this;
    }

    public <V> FunctionBulider<T, S> width(boolean accept, BiConsumer<T, V> biConsumer, V value, V orElse) {
        if (accept) {
            list.add(t -> biConsumer.accept(t, value));
        } else {
            list.add(t -> biConsumer.accept(t, orElse));
        }
        return this;
    }

    /*** ++++++++++ 映射赋值 ++++++++++ ***/

    public <V> FunctionBulider<T, S> mapper(BiConsumer<T, V> biConsumer, Function<S, V> function) {
        list.add(t -> biConsumer.accept(t, function.apply(s)));
        return this;
    }

    public <V> FunctionBulider<T, S> mapper(BiConsumer<T, V> biConsumer, Function<S, V> function, V orElse) {
        if (function == null) {
            list.add(t -> biConsumer.accept(t, orElse));
        } else {
            list.add(t -> biConsumer.accept(t, function.apply(s)));
        }
        return this;
    }


    public <V> FunctionBulider<T, S> mapper(boolean accept, BiConsumer<T, V> biConsumer, Function<S, V> function) {
        if (accept) {
            list.add(t -> biConsumer.accept(t, function.apply(s)));
        }
        return this;
    }

    public <V> FunctionBulider<T, S> mapper(boolean accept, BiConsumer<T, V> biConsumer, Function<S, V> function, V orElse) {
        if (accept) {
            list.add(t -> biConsumer.accept(t, function.apply(s)));
        } else {
            list.add(t -> biConsumer.accept(t, orElse));
        }
        return this;
    }

    /**
     * 开始构建
     *
     * @return
     */
    public T build() {
        T t = supplier.get();
        Optional.of(list)
                .orElse(new ArrayList<>())
                .stream()
                .filter(Objects::nonNull)
                .forEach(item -> item.accept(t));
        return t;
    }


    /**
     * 开始构建
     *
     * @return
     */
    public T build(List<Consumer<T>> list) {
        T t = supplier.get();
        Optional.of(list)
                .orElse(new ArrayList<>())
                .stream()
                .filter(Objects::nonNull)
                .forEach(item -> item.accept(t));
        return t;
    }
}

四、基于Java的BiConsumer实现的构建者

package com.function.bulider;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

public class BiConsumerBulider<T, S> {
    /**
     * 消费数据2
     */
    private final T t;
    /**
     * 消费数据2
     */
    private final S s;

    /**
     *  存储需要执行的过程
     */
    private final List<Consumer<T>> list = new ArrayList<>();

    public BiConsumerBulider(T t, S s) {
        this.t = t;
        this.s = s;
    }

    /**
     * 静态工厂方法,获取类型转换
     *
     * @param t   目标源
     * @param s   数据源
     * @param <T> 目标类型
     * @param <S> 数据类型
     * @return 构造函数
     */
    public static <T, S> BiConsumerBulider<T, S> of(T t, S s) {
        return new BiConsumerBulider<>(t, s);
    }

    /*** ++++++++++ 引用赋值 ++++++++++ ***/

    public <V> BiConsumerBulider<T, S> width(BiConsumer<T, V> biConsumer, V value) {
        list.add(t -> biConsumer.accept(t, value));
        return this;
    }


    public <V> BiConsumerBulider<T, S> width(boolean accept, BiConsumer<T, V> biConsumer, V value) {
        if (accept) {
            list.add(t -> biConsumer.accept(t, value));
        }
        return this;
    }


    public <V> BiConsumerBulider<T, S> width(BiConsumer<T, V> biConsumer, V value, V orElse) {
        if (null == value) {
            list.add(e -> biConsumer.accept(e, orElse));
        } else {
            list.add(e -> biConsumer.accept(e, value));
        }
        return this;
    }


    public <V> BiConsumerBulider<T, S> width(boolean accept, BiConsumer<T, V> biConsumer, V value, V orElse) {
        if (accept) {
            list.add(t -> biConsumer.accept(t, value));
        } else {
            list.add(t -> biConsumer.accept(t, orElse));
        }
        return this;
    }

    /**** ++++++++++ 映射赋值 ++++++++++  ***/

    public <V> BiConsumerBulider<T, S> mapper(BiConsumer<T, V> biConsumer, Function<S, V> function) {
        list.add(t -> biConsumer.accept(t, function.apply(s)));
        return this;
    }


    public <V> BiConsumerBulider<T, S> mapper(BiConsumer<T, V> biConsumer, Function<S, V> function, V orElse) {
        if (function == null) {
            list.add(t -> biConsumer.accept(t, orElse));
        } else {
            list.add(t -> biConsumer.accept(t, function.apply(s)));
        }
        return this;
    }

    public <V> BiConsumerBulider<T, S> mapper(boolean accept, BiConsumer<T, V> biConsumer, Function<S, V> function) {
        if (accept) {
            list.add(t -> biConsumer.accept(t, function.apply(s)));
        }
        return this;
    }

    public <V> BiConsumerBulider<T, S> mapper(boolean accept, BiConsumer<T, V> biConsumer, Function<S, V> function, V orElse) {
        if (accept) {
            list.add(t -> biConsumer.accept(t, function.apply(s)));
        } else {
            list.add(t -> biConsumer.accept(t, orElse));
        }
        return this;
    }

    /**
     * 开始构建
     *
     * @return
     */
    public T build() {
        Optional.of(list)
                .orElse(new ArrayList<>())
                .stream()
                .filter(Objects::nonNull)
                .forEach(item -> item.accept(t));
        return t;
    }

    /**
     * 开始构建
     *
     * @return
     */
    public T build(List<Consumer<T>> list) {
        Optional.of(list)
                .orElse(new ArrayList<>())
                .stream()
                .filter(Objects::nonNull)
                .forEach(item -> item.accept(t));
        return t;
    }


}

四、基于Java的BiFunction实现的构建者

package com.function.bulider;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class BiFunctionBulider<T, U, R> {
    /**
     * 消费类型1
     */
    private final T t;
    /**
     * 消费类型2
     */
    private final U u;
    /**
     * 待创建的类型
     */
    private final Supplier<R> supplier;

    /**
     * 存储返回值 R 需要执行的过程
     */
    private final List<Consumer<R>> list = new ArrayList<>();

    public BiFunctionBulider(T t, U u, Supplier<R> supplier) {
        this.t = t;
        this.u = u;
        this.supplier = supplier;
    }

    /**
     * 两个数据合并成一个数据
     *
     * @param t        源数据1
     * @param u        源数据2
     * @param supplier 返回合并的数据
     * @param <T>      源数据1类型
     * @param <U>      源数据2类型
     * @param <R>      返回的数据类型
     * @return 构造函数
     */
    public static <T, U, R> BiFunctionBulider<T, U, R> of(T t, U u, Supplier<R> supplier) {
        return new BiFunctionBulider<>(t, u, supplier);
    }


    /*** ++++++++++ 引用赋值 ++++++++++ ***/

    public <V> BiFunctionBulider<T, U, R> width(BiConsumer<R, V> biConsumer, V value) {
        list.add(r -> biConsumer.accept(r, value));
        return this;
    }

    public <V> BiFunctionBulider<T, U, R> width(boolean accept, BiConsumer<R, V> biConsumer, V value) {
        if (accept) {
            list.add(r -> biConsumer.accept(r, value));
        }
        return this;
    }


    public <V> BiFunctionBulider<T, U, R> width(BiConsumer<R, V> biConsumer, V value, V orElse) {
        if (null == value) {
            list.add(r -> biConsumer.accept(r, orElse));
        } else {
            list.add(r -> biConsumer.accept(r, value));
        }
        return this;
    }


    public <V> BiFunctionBulider<T, U, R> width(boolean accept, BiConsumer<R, V> biConsumer, V value, V orElse) {
        if (accept) {
            list.add(r -> biConsumer.accept(r, value));
        } else {
            list.add(r -> biConsumer.accept(r, orElse));
        }
        return this;
    }

    /*** ++++++++++ 映射赋值,来源数据类型1 ++++++++++ ***/

    public <V> BiFunctionBulider<T, U, R> width(BiConsumer<R, V> biConsumer, Function<T, V> function) {
        list.add(r -> biConsumer.accept(r, function.apply(t)));
        return this;
    }

    public <V> BiFunctionBulider<T, U, R> width(boolean accept, BiConsumer<R, V> biConsumer, Function<T, V> function) {
        if (accept) {
            list.add(r -> biConsumer.accept(r, function.apply(t)));
        }
        return this;
    }


    public <V> BiFunctionBulider<T, U, R> width(BiConsumer<R, V> biConsumer, Function<T, V> function, V orElse) {
        if (null == function) {
            list.add(r -> biConsumer.accept(r, orElse));
        } else {
            list.add(r -> biConsumer.accept(r, function.apply(t)));
        }
        return this;
    }


    public <V> BiFunctionBulider<T, U, R> width(boolean accept, BiConsumer<R, V> biConsumer, Function<T, V> function, V orElse) {
        if (accept) {
            list.add(r -> biConsumer.accept(r, function.apply(t)));
        } else {
            list.add(r -> biConsumer.accept(r, orElse));
        }
        return this;
    }

    /*** ++++++++++ 映射赋值,来源数据类型2 ++++++++++ ***/

    public <V> BiFunctionBulider<T, U, R> tMapper(BiConsumer<R, V> biConsumer, Function<U, V> function) {
        list.add(r -> biConsumer.accept(r, function.apply(u)));
        return this;
    }


    public <V> BiFunctionBulider<T, U, R> tMapper(boolean accept, BiConsumer<R, V> biConsumer, Function<U, V> function) {
        if (accept) {
            list.add(r -> biConsumer.accept(r, function.apply(u)));
        }
        return this;
    }


    public <V> BiFunctionBulider<T, U, R> tMapper(BiConsumer<R, V> biConsumer, Function<U, V> function, V orElse) {
        if (null == function) {
            list.add(r -> biConsumer.accept(r, orElse));
        } else {
            list.add(r -> biConsumer.accept(r, function.apply(u)));
        }
        return this;
    }


    public <V> BiFunctionBulider<T, U, R> tMapper(boolean accept, BiConsumer<R, V> biConsumer, Function<U, V> function, V orElse) {
        if (accept) {
            list.add(r -> biConsumer.accept(r, function.apply(u)));
        } else {
            list.add(r -> biConsumer.accept(r, orElse));
        }
        return this;
    }

    /**
     * 开始构建
     *
     * @return
     */
    public R build() {
        R r = supplier.get();
        Optional.of(list)
                .orElse(new ArrayList<>())
                .stream()
                .filter(Objects::nonNull)
                .forEach(item -> item.accept(r));
        return r;
    }


    /**
     * 开始构建
     *
     * @return
     */
    public R build(List<Consumer<R>> list) {
        R r = supplier.get();
        Optional.of(list)
                .orElse(new ArrayList<>())
                .stream()
                .filter(Objects::nonNull)
                .forEach(item -> item.accept(r));
        return r;
    }

}

五、基于Java的 Predicate实现的构建者

package com.function.bulider;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

public class PredicateBulider<T> {

    private T t;

    private List<Predicate<T>> list = new ArrayList<>();

    /**
     * 获取判断列表
     *
     * @return
     */
    public List<Predicate<T>> getList() {
        return list;
    }

    public PredicateBulider(T t) {
        this.t = t;
    }

    /**
     * 获取判断的函数
     *
     * @param <T>
     * @return
     */
    public static <T> PredicateBulider<T> of(T t) {
        return new PredicateBulider<>(t);
    }

    /**
     * @param function 判断的字段
     * @param value    值
     * @param <V>      判断的字段类型
     * @return
     */
    public <V> PredicateBulider<T> width(Function<T, V> function, V value) {
        if (function.apply(t) == null) {
            list.add(t -> false);
            return this;
        }
        list.add(t -> function.apply(t).equals(value));
        return this;
    }


    /**
     * and 判断
     *
     * @param function
     * @param v1
     * @param v2
     * @param <V1>
     * @param <V2>
     * @return
     */
    public <V1, V2> PredicateBulider<T> and(Function<T, V1> function, V1 v1, V2 v2) {
        if (function.apply(t) == null) {
            list.add(t -> false);
            return this;
        }
        list.add(t -> (function.apply(t).equals(v1)) && (function.apply(t).equals(v2)));
        return this;
    }

    /**
     * or 判断
     *
     * @param function
     * @param v1
     * @param v2
     * @param <V1>
     * @param <V2>
     * @return
     */
    public <V1, V2> PredicateBulider<T> or(Function<T, V1> function, V1 v1, V2 v2) {
        if (function.apply(t) == null) {
            list.add(t -> false);
            return this;
        }
        list.add(t -> (function.apply(t).equals(v1)) || (function.apply(t).equals(v2)));
        return this;
    }

    /**
     * 非判断
     *
     * @param function 判断的字段
     * @param value    值
     * @param <V>      判断的字段类型
     * @return
     */
    public <V> PredicateBulider<T> no(Function<T, V> function, V value) {
        if (function.apply(t) == null) {
            list.add(t -> false);
            return this;
        }
        list.add(t -> (!function.apply(t).equals(value)));
        return this;
    }


    /**
     * 异或判断
     *
     * @param function
     * @param v1
     * @param v2
     * @param <V1>
     * @param <V2>
     * @return
     */
    public <V1, V2> PredicateBulider<T> xor(Function<T, V1> function, V1 v1, V2 v2) {
        if (function.apply(t) == null) {
            list.add(t -> false);
            return this;
        }
        list.add(t -> (function.apply(t).equals(v1)) ^ (function.apply(t).equals(v2)));
        return this;
    }


    /**
     * 开始构建,获取判断值
     *
     * @return
     */
    public boolean build() {
        if (t == null) {
            throw new RuntimeException("判断值不能为空");
        }
        return list.stream().reduce(x -> true, Predicate::and).test(t);
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值