[英]Set MathContext to BinaryOperator reference methods
我有這個枚舉:
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;
}
}
我想使用一些MathContext
,當執行這樣的操作時,可以輕松地做到這一點:
MathContext mc = MathContext.DECIMAL32;
BigDecimal t0 = new BigDecimal(100);
BigDecimal t1 = new BigDecimal(2);
BigDecimal result = t0.add(t1, mc);
但是,如果我想在枚舉中使用對BinaryOperator
的引用,我看不到給它MathContext
:
BigDecimal result = enum.getOperation().apply(t0, t1);
在文檔或可用的適用方法中,我看到任何選項。
根據使用情況,您可以將自定義功能接口的范圍最小化:
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);
}
}
因此,使用Operator
枚舉的任何人都不必了解內部使用的TriFunc
接口。 Operator
可以直接使用
BigDecimal result = Operator.add
.apply(new BigDecimal(100), new BigDecimal(2), MathContext.DECIMAL32);
或獲取標准BinaryOperator<BigDecimal>
BigDecimal result = Operator.add.getOperation(MathContext.DECIMAL32)
.apply(new BigDecimal(100), new BigDecimal(2));
第一種選擇是實現類似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;
}
}
但是您必須自己實現TriFunction
(或在線查找)才能使用,因為Java不會從頭開始提供類似的功能。
一種更簡單(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
}
}
您可以通過以下方式更輕松地調用它
BigDecimal result = enum.apply(t0, t1, mc);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.