简体   繁体   English

将MathContext设置为BinaryOperator参考方法

[英]Set MathContext to BinaryOperator reference methods

I have this enum: 我有这个枚举:

public enum Operator {
    add("+", BigDecimal::add),
    subtract("-", BigDecimal::subtract),
    multiply("*", BigDecimal::multiply),
    divide("/", BigDecimal::divide),
    mod("%", BigDecimal::remainder);

    Operator(final String symbol, final BinaryOperator<BigDecimal> operation) {
        this.symbol = symbol;
        this.operation = operation;
    }

    public BinaryOperator<BigDecimal> getOperation() {
        return operation;
    }
}

I want to use the some MathContext , one can easily do that when performing an operation like this: 我想使用一些MathContext ,当执行这样的操作时,可以轻松地做到这一点:

MathContext mc = MathContext.DECIMAL32;
BigDecimal t0 = new BigDecimal(100);
BigDecimal t1 = new BigDecimal(2); 
BigDecimal result = t0.add(t1, mc);

However if I want to use the reference to the BinaryOperator in the enum I can't see a way of giving it the MathContext : 但是,如果我想在枚举中使用对BinaryOperator的引用,我看不到给它MathContext

BigDecimal result = enum.getOperation().apply(t0, t1);

In the documentation nor the methods available for apply I see any option. 在文档或可用的适用方法中,我看到任何选项。

Depending on the use case, you can keep the scope of the custom functional interface at a minimum: 根据使用情况,您可以将自定义功能接口的范围最小化:

public enum Operator {
    add("+", BigDecimal::add),
    subtract("-", BigDecimal::subtract),
    multiply("*", BigDecimal::multiply),
    divide("/", BigDecimal::divide),
    mod("%", BigDecimal::remainder);

    private interface TriFunc {
        BigDecimal apply(BigDecimal a, BigDecimal b, MathContext c);
    }
    private String symbol;
    private TriFunc operation;

    Operator(String symbol, TriFunc operation) {
        this.symbol = symbol;
        this.operation = operation;
    }

    public BinaryOperator<BigDecimal> getOperation(MathContext c) {
        return (a, b) -> operation.apply(a, b, c);
    }

    // you can also provide a direct method:
    public BigDecimal apply(BigDecimal a, BigDecimal b, MathContext c) {
        return operation.apply(a, b, c);
    }
}

So anyone using the Operator enumeration, doesn't have to know anything about the internally used TriFunc interface. 因此,使用Operator枚举的任何人都不必了解内部使用的TriFunc接口。 Operator can be use either, directly like Operator可以直接使用

BigDecimal result = Operator.add
    .apply(new BigDecimal(100), new BigDecimal(2), MathContext.DECIMAL32);

or getting the standard BinaryOperator<BigDecimal> like 或获取标准BinaryOperator<BigDecimal>

BigDecimal result = Operator.add.getOperation(MathContext.DECIMAL32)
    .apply(new BigDecimal(100), new BigDecimal(2));

First option would be to implement something like a TriFunction<P1, P2, P3, R> and modify your code as following: 第一种选择是实现类似TriFunction<P1, P2, P3, R>类的东西TriFunction<P1, P2, P3, R>并按如下所示修改您的代码:

public enum Operator {
    add("+", (t0, t1, mc) -> t0.add(t1, mc)),
    subtract("-", (t0, t1, mc) -> t0.subtract(t1, mc)),
    multiply("*", (t0, t1, mc) -> t0.multiply(t1, mc)),
    divide("/", (t0, t1, mc) -> t0.divide(t1, mc)),
    mod("%", (t0, t1, mc) -> t0.remainder(t1, mc));

    Operator(final String symbol, final TriFunction<BigDecimal, BigDecimal, MathContext, BigDecimal> operation) {
        this.symbol = symbol;
        this.operation = operation;
    }

    public BinaryOperator<BigDecimal, BigDecimal, MathContext, BigDecimal> getOperation() {
        return operation;
    }
}

But you would have to implement the TriFunction by yourself (or find it online) to use, as java doesn't offer something like this from the scratch. 但是您必须自己实现TriFunction (或在线查找)才能使用,因为Java不会从头开始提供类似的功能。

A simpler (IMO quick and dirty) way could be this: 一种更简单(IMO快速又肮脏)的方式可能是这样的:

public enum Operator {
    add("+"), subtract("-"), multiply("*"), divide("/"), mod("%");

    // Attributes and constructors

    public BigDecimal apply(BigDecimal t1, BigDecimal t2, MathContext mc) {
        switch (this) {
            case add:      return t1.add(t2, mc);
            case subtract: return t1.subtract(t2, mc);
            case multiply: return t1.multiply(t2, mc);
            case divide:   return t1.divide(t2, mc);
            case mod:      return t1.remainder(t2, mc);
        }
        return null; // never reached
    }
}

And you could call it even easier by 您可以通过以下方式更轻松地调用它

BigDecimal result = enum.apply(t0, t1, mc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM