将开关替换为二进制运算符



我正在尝试通过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);

但是我不明白如何避免switchcomputeExpressioncomputeOne相同.

您可以为每个算术运算定义一个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的示例。
请注意,由于运算符现在由负责执行函数的枚举Operator表示,因此该函数现在只需要两个参数:Integer和转换为intString
所以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);

最新更新