-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathOperator.java
More file actions
51 lines (42 loc) · 1.53 KB
/
Operator.java
File metadata and controls
51 lines (42 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package co.programmers.domain;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import co.programmers.exception.ExceptionMessage;
public enum Operator {
ADDITION("+", 2, (operand1, operand2) -> operand1 + operand2),
SUBTRACTION("-", 2, (operand1, operand2) -> operand1 - operand2),
MULTIPLICATION("*", 1, (operand1, operand2) -> operand1 * operand2),
DIVISION("/", 1, (operand1, operand2) -> {
if (operand2 == 0) {
throw new ArithmeticException(ExceptionMessage.DIVIDED_BY_ZERO);
}
return operand1 / operand2;
});
private final String symbol;
private final int priority;
private final BiFunction<Double, Double, Double> operation;
private Operator(String symbol, int priority, BiFunction<Double, Double, Double> operation) {
this.symbol = symbol;
this.priority = priority;
this.operation = operation;
}
public static Double calculate(String operator, Double operand1, Double operand2) {
return getSymbol(operator).operation.apply(operand1, operand2);
}
public static Operator getSymbol(Object operator) {
return Arrays.stream(values())
.filter(o -> o.symbol.equals(operator))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(ExceptionMessage.INVALID_INPUT));
}
public static void decideCalculationOrder(List<String> operators) {
Collections.sort(operators, (operator1, operator2) ->
Integer.compare(getSymbol(operator1).getPriority(), getSymbol(operator2).getPriority())
);
}
public int getPriority() {
return priority;
}
}