-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathOperator.java
More file actions
35 lines (28 loc) · 967 Bytes
/
Operator.java
File metadata and controls
35 lines (28 loc) · 967 Bytes
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
package com.programmers.engine.model;
import lombok.Getter;
import java.util.Arrays;
import java.util.function.BiFunction;
@Getter
public enum Operator {
ADD("+", 1, (a, b) -> a + b),
SUBTRACTION("-", 1, (a, b) -> a - b),
MULTIPLY("*", 2, (a, b) -> a * b),
DIVIDE("/", 2, (a, b) -> b / a);
private final String symbol;
private final int priority;
private final BiFunction<Integer, Integer, Integer> action;
Operator(String symbol, int priority, BiFunction<Integer, Integer, Integer> action) {
this.symbol = symbol;
this.priority = priority;
this.action = action;
}
public static Operator matchOperator(String operator) {
return Arrays.stream(values())
.filter(v -> operator.equals(v.symbol))
.findFirst()
.orElseThrow(IllegalArgumentException::new);
}
public int calculate(int a, int b) {
return action.apply(a, b);
}
}