函数式编程处理加减法和空值处理

Published on with 0 views and 0 comments

函数式编程处理加减法和空值处理

import java.math.BigDecimal;


public class TestCalculator<T> {
    /**
     * 汇总结果对象
     */
    private final T collect;

    private TestCalculator(T collect) {
        this.collect = collect;
    }

    /**
     * Wrap a collect to store the math result
     *
     * @param collect 汇总对象
     * @return 汇总计算器
     * @param <T> 汇总结果存放对象
     */
    public static <T> TestCalculator<T> of(T collect) {
        return new TestCalculator<>(collect);
    }

    public <P> TestCalculator<T> setIt(SetterFunction<T, P> setter, P p) {
        setter.apply(collect, p);
        return this;
    }

    /**
     * 对象属性加法
     *
     * @param setter property setter function of {@link #collect}
     * @param mapper property getter function of value convert
     * @param right 加法项
     */
    public TestCalculator<T> addInt(SetterFunction<T, Integer> setter,
                                      GetterFunction<T, Integer> mapper, Integer right) {
        addInt(setter, mapper.apply(collect), right);
        return this;
    }

    /**
     * 判断属性是否为空
     */
    public TestCalculator<T> checkPropertyIsEmpty(TestFunc.GetterFunction<T, Object> mapper) {
        Object apply = mapper.apply(collect);
		if(null == apply) system.out.println("属性为空");
        return this;
    }

    /**
     * 对象属性加法
     *
     * @param setter property setter function of {@link #collect}
     * @param left 加法左项
     * @param right 加法右项
     */
    public TestCalculator<T> addInt(SetterFunction<T, Integer> setter,
                                                         Integer left, Integer right) {
        Integer result = null;
        if (null != left || null != right) {
            result = (null == left ? 0 : left) + (null == right ? 0 : right);
        }
        setter.apply(collect, result);
        return this;
    }

    /**
     * 对象属性减法
     *
     * @param setter property setter function of {@link #collect}
     * @param mapper 被减数,property getter function of value convert
     * @param right 减数
     */
    public TestCalculator<T> subtractInt(SetterFunction<T, Integer> setter,
                                                           GetterFunction<T, Integer> mapper, Integer right) {
        subtractInt(setter, mapper.apply(collect), right);
        return this;
    }

    /**
     * 对象属性减法
     *
     * @param setter property setter function of {@link #collect}
     * @param left 被减数
     * @param right 减数
     */
    public TestCalculator<T> subtractInt(SetterFunction<T, Integer> setter,
                                                              Integer left, Integer right) {
        Integer result = null;
        if (null != left || null != right) {
            result = (null == left ? 0 : left) - (null == right ? 0 : right);
        }
        setter.apply(collect, result);
        return this;
    }

    /**
     * 对象属性加法
     *
     * @param setter property setter function of {@link #collect}
     * @param mapper property getter function of value convert
     * @param right 加法项
     */
    public TestCalculator<T> addBigDecimal(SetterFunction<T, BigDecimal> setter,
                                      GetterFunction<T, BigDecimal> mapper, BigDecimal right) {
        addBigDecimal(setter, mapper.apply(collect), right);
        return this;
    }

    /**
     * 对象属性加法
     *
     * @param setter property setter function of {@link #collect}
     * @param left 加法左项
     * @param right 加法右项
     */
    public TestCalculator<T> addBigDecimal(SetterFunction<T, BigDecimal> setter,
                                                                BigDecimal left, BigDecimal right) {
        BigDecimal result = null;
        if (null != left || null != right) {
            result = new BigDecimal(0);
            if (null != left) {
                result = result.add(left);
            }
            if (null != right) {
                result = result.add(right);
            }
        }
        setter.apply(collect, result);
        return this;
    }

    /**
     * 对象属性减法
     *
     * @param setter property setter function of {@link #collect}
     * @param mapper 被减数,property getter function of value convert
     * @param right 减数
     */
    public TestCalculator<T> subtractBigDecimal(SetterFunction<T, BigDecimal> setter,
                                           GetterFunction<T, BigDecimal> mapper, BigDecimal right) {
        subtractBigDecimal(setter, mapper.apply(collect), right);
        return this;
    }

    /**
     * 对象属性减法
     *
     * @param setter property setter function of {@link #collect}
     * @param left 被减数
     * @param right 减数
     */
    public TestCalculator<T> subtractBigDecimal(SetterFunction<T, BigDecimal> setter,
                                                  BigDecimal left, BigDecimal right) {
        BigDecimal result = null;
        if (null != left || null != right) {
            result = new BigDecimal(0);
            if (null != left) {
                result = result.add(left);
            }
            if (null != right) {
                result = result.subtract(right);
            }
        }
        setter.apply(collect, result);
        return this;
    }

    public T result() {
        return collect;
    }

    @FunctionalInterface
    public static interface GetterFunction<T, R> {
        R apply(T t);
    }

    @FunctionalInterface
    public static interface SetterFunction<T, P> {
        void apply(T t, P p);
    }
}

测试类

public class test {
    public static void main(String[] args) {
        User user = new User();
        user.setName("xxx");
        TestFunc.of(user)
                .checkPropertyIsEmpty(User::getName)
                .checkPropertyIsEmpty(User::getAge);
    }
}


class User {

    private String name;

    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

输出结果

1654782070357.png


标题:函数式编程处理加减法和空值处理
作者:cuijianzhe
地址:https://cjzshilong.cn/articles/2022/06/09/1654782137984.html