java链式编程

链式编程代码可读性高,值得推荐,以下为几种计算方式,大家自行体会
在这里插入图片描述

package com.xin.demo.chaindemo;

import cn.hutool.core.util.NumberUtil;

import java.math.BigDecimal;

public class BigNumChainTest {
    public static void main(String[] args) {
        double a = 1.2;
        double b = 2.3d;
        double c = 3.04d;
        // 1
        double d = a + b - c;
        System.out.println("d: " + d);

        // 2 BigDecimal自己的链式编程
        BigDecimal decimal = new BigDecimal(a);
        double dd = decimal.add(BigDecimal.valueOf(b))
                .subtract(BigDecimal.valueOf(c))
                .doubleValue();
        System.out.println("dd: " + dd);

        // 3 hutool工具类需要前面的方法先返回值用来当后面方法的入参,步骤冗余不易读
        double d1 = NumberUtil.add(a, b);
        double ddd = NumberUtil.sub(d1, c);
        System.out.println("ddd: " + ddd);

        // 4 链式编程
        double dddd = BigNumChain.startOf(a)
                      .add(b)
                      .subtract(c)
                      .getDouble();
        System.out.println("dddd: " + dddd);
    }
}

package com.xin.demo.chaindemo;

import java.math.BigDecimal;
import java.util.function.BiFunction;
import java.util.function.Function;

public class BigNumChain {

    private BigDecimal value;

    public BigNumChain(Object value) {
        this.value = convertBigDecimal(value, null);
    }

    /**
     * 开始计算的初始值
     * @param value
     * @return
     */
    public static BigNumChain startOf(Object value) {
        return new BigNumChain(value);
    }

    /**
     * 加
     * @param other
     * @return
     */
    public BigNumChain add(Object other) {
        return operator(BigDecimal::add, other);
    }

    /**
     * 加
     * @param other
     * @param beforeOperteScale 加之前先把 other 四舍五入法保留 beforeOperteScale 位小数
     * @return
     */
    public BigNumChain add(Object other, Integer beforeOperteScale) {
        return operator(BigDecimal::add, other, beforeOperteScale);
    }

    /**
     * 减
     * @param other
     * @return
     */
    public BigNumChain subtract(Object other) {
        return operator(BigDecimal::subtract, other);
    }

    /**
     * 减
     * @param other
     * @param beforeOperteScale 减之前先把 other 四舍五入法保留 beforeOperteScale 位小数
     * @return
     */
    public BigNumChain subtract(Object other, Integer beforeOperteScale) {
        return operator(BigDecimal::subtract, other, beforeOperteScale);
    }

    /**
     * 乘
     * @param other
     * @return
     */
    public BigNumChain multiply(Object other) {
        return operator(BigDecimal::multiply, other);
    }

    /**
     * 乘
     * @param other
     * @param beforeOperteScale 乘之前先把 other 四舍五入法保留 beforeOperteScale 位小数
     * @return
     */
    public BigNumChain multiply(Object other, Integer beforeOperteScale) {
        return operator(BigDecimal::multiply, other, beforeOperteScale);
    }

    /**
     * 除以
     * @param other
     * @return
     */
    public BigNumChain divide(Object other) {
        return operator(BigDecimal::divide, other);
    }
    /**
     * 除以
     * @param other
     * @param beforeOperteScale 除之前先把 other 四舍五入法保留 beforeOperteScale 位小数
     * @return
     */
    public BigNumChain divide(Object other, Integer beforeOperteScale) {
        return operator(BigDecimal::divide, other, beforeOperteScale);
    }

    /**
     * 除以
     * @param other
     * @param scale 结果保留 scale 位小数
     * @return
     */
    public BigNumChain divideWithScale(Object other, Integer scale) {
        return baseOperator(otherValue -> this.value.divide(otherValue, scale, BigDecimal.ROUND_HALF_UP), other, null);
    }

    /**
     * 除以
     * @param other
     * @param scale 结果保留 scale 位小数
     * @param beforeOperteScale 除以之前先把 other 四舍五入法保留 beforeOperteScale 位小数
     * @return
     */
    public BigNumChain divideWithScale(Object other, Integer scale, Integer beforeOperteScale) {
        return baseOperator(otherValue -> this.value.divide(otherValue, scale, BigDecimal.ROUND_HALF_UP), other, beforeOperteScale);
    }


    public BigDecimal getValue() {
        return value;
    }
    public BigDecimal getValue(int scale) {
        return value.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public Double getDouble() {
        return getValue().doubleValue();
    }

    public Double getDouble(int scale) {
        return getValue(scale).doubleValue();
    }
    public Long getLong() {
        return getValue().longValue();
    }
    public Integer getInteger() {
        return getValue().intValue();
    }

    private BigNumChain operator(BiFunction<BigDecimal, BigDecimal, BigDecimal> operator, Object other) {
        return operator(operator, other, null);
    }
    private BigNumChain operator(BiFunction<BigDecimal, BigDecimal, BigDecimal> operator, Object other, Integer beforeOperteScale) {
        return baseOperator(otherValue -> operator.apply(this.value, otherValue), other, beforeOperteScale);
    }
    private synchronized BigNumChain baseOperator(Function<BigDecimal, BigDecimal> operatorFunction, Object other, Integer beforeOperteScale) {
        if (other == null) {
            return this;
        }
        if (other instanceof BigNumChain) {
            this.value = operatorFunction.apply(((BigNumChain) other).getValue());
            return this;
        }
        this.value = operatorFunction.apply(convertBigDecimal(other, beforeOperteScale));
        return this;
    }

    private BigDecimal convertBigDecimal(Object value, Integer scale) {
        if (value == null) {
            return BigDecimal.ZERO;
        }
        BigDecimal res;
        if (value instanceof Number) {
            res = BigDecimal.valueOf(((Number) value).doubleValue());
        } else {
            try {
                res = new BigDecimal(value.toString());
            } catch (NumberFormatException e) {
                return BigDecimal.ZERO;
            }
        }

        if (scale != null) {
            res = BigDecimal.valueOf(res.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue());
        }
        return res;
    }

}

工具代码引用自up主程序员小山与Bug,在此感谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值