[英]Replacing switch by BinaryOperator
我正在嘗試用BinaryOperator
功能接口替換用於算術運算的公共開關。
基本方法是:
private static int computeOne(int res, String operand, String operation) {
int number = Integer.parseInt(operand);
switch (operation) {
case "+":
res += number;
break;
case "-":
res -= number;
break;
case "*":
res *= number;
break;
case "/":
res = (number != 0 ? res / number : Integer.MAX_VALUE);
break;
default:
res = 0;
System.out.println("unknown operation");
}
return res;
}
據我所知,編寫類似的內容是必要的:
BinaryOperator<Integer> action = (a,b) -> computeExpression(a + operation + b);
action.apply(res, operand);
但是我不明白如何避免在computeExpression
中switch
與computeExpression
相同的computeOne
。
您可以為每個算術運算定義BinaryOperator<Integer>
:
// a = operand 1
// b = operand 2
(a, b) -> a * b;
(a, b) -> a + b;
(a, b) -> a / b;
(a, b) -> a - b;
然后你可以應用一個傳遞2個參數:
// result = operation.apply(a, b);
int result = ((BinaryOperator<Integer>) ((a, b) -> a * b)).apply(2, 2);
我會使用枚舉來枚舉這些操作:
class Test {
public static void main(String[] args) {
System.out.println(computeOne(4, "2", "/")); // 2
System.out.println(computeOne(4, "2", "*")); // 8
System.out.println(computeOne(4, "2", "-")); // 2
System.out.println(computeOne(4, "2", "+")); // 6
}
private static int computeOne(int res, String operand, String operation) {
return Operation.getOperationBySymbol(operation)
.getBinaryOperator()
.apply(res, Integer.parseInt(operand));
}
private enum Operation {
// operation = symbol, action
MULTIPLICATION("*", (a, b) -> a * b),
ADDITION("+", (a, b) -> a + b),
SUBTRACTION("-", (a, b) -> a - b),
DIVISION("/", (a, b) -> a / b);
private final BinaryOperator<Integer> binaryOperator;
private final String symbol;
Operation(String symbol, BinaryOperator<Integer> binaryOperator) {
this.symbol = symbol;
this.binaryOperator = binaryOperator;
}
public BinaryOperator<Integer> getBinaryOperator() {
return binaryOperator;
}
public String getSymbol() {
return symbol;
}
public static Operation getOperationBySymbol(String symbol) {
for (Operation operation : values()) {
if (operation.getSymbol().equals(symbol)) {
return operation;
}
}
throw new IllegalArgumentException("Unknown symbol: " + symbol);
}
}
}
您還可以使用BiFunction<BinaryOperator<?>, Pair<?, ?>, ?>
來“簡化”它:
// BiFunction<Operator, Operands, Result>
// Operator = BinaryOperator<?>
// Operands = Pair<?, ?>
BiFunction<BinaryOperator<Integer>, Pair<Integer, Integer>, Integer> f =
(operator, operands) ->
operator.apply(operands.getKey(), operands.getValue());
f.apply((a, b) -> a + b, new Pair<>(2, 2)); // 4
算術運算符不能是變量。
通過使用功能接口或不使用實際代碼,您將具有相同的約束:將String
運算符轉換為算術運算符。
此外,實際上在computeOne()
您接受一個int
和兩個String
作為參數,並返回一個int
。
BinaryOperator<Integer>
接受兩個Integer
並返回一個Integer
。
所以它不兼容。
你需要一個TriFunction
但它不存在。
創建自己的功能接口,例如TriFunction<T,U,V,R>
或減少傳遞給函數的參數數量。
下面是一個使用枚舉Operator
與BiFunction
結合的BiFunction
,它與您的實際方法執行相同的操作。
注意,運營商現在由枚舉表示Operator
負責執行功能,該功能只需要兩個參數現在:在Integer
和String
,你轉換成int
。
所以BiFunction<Integer, String, Integer>
很好。
public enum Operator{
ADD("+", (a,b) -> a + Integer.parseInt(b)),
SUBSTRACT("-", (a,b) -> a - Integer.parseInt(b)),
MULTIPLY("*", (a,b) -> a * Integer.parseInt(b)),
// ...
DEFAULT("", (a,b) -> 0);
public BiFunction<Integer, String, Integer> function;
private String symbol;
Operator(String symbol, BiFunction<Integer, String, Integer> function){
this.symbol = symbol;
this.function = function;
}
public int compute(int actualValue, String operand){
return function.apply(actualValue, operand);
}
public static Operator of(String symbol) {
for (Operator value : values()) {
if (symbol.equals(value.symbol)) {
return value;
}
}
return Operator.DEFAULT;
}
}
您可以創建如下操作:
int res = 10;
String operand = "15";
String symbol = "+";
res = Operator.of(symbol).compute(res, operand);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.