-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAbstractOperation.java
More file actions
62 lines (50 loc) · 2.13 KB
/
AbstractOperation.java
File metadata and controls
62 lines (50 loc) · 2.13 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
52
53
54
55
56
57
58
59
60
61
62
package expression;
public abstract class AbstractOperation implements CommonExpression {
protected CommonExpression left;
protected CommonExpression right;
public AbstractOperation(CommonExpression left, CommonExpression right) {
this.left = left;
this.right = right;
}
protected abstract String getSign();
protected abstract int getPriority();
protected abstract boolean isOrderImportant(); // in the same priority level
protected abstract int compute(int x, int y);
public int evaluate(int x) {
return compute(left.evaluate(x), right.evaluate(x));
}
public int evaluate(int x, int y, int z) {
return compute(left.evaluate(x, y, z), right.evaluate(x, y, z));
}
public boolean equals(Object other) {
return (other instanceof AbstractOperation
&& ((AbstractOperation) other).left.equals(this.left)
&& ((AbstractOperation) other).right.equals(this.right)
&& other.getClass().equals(this.getClass()));
}
public int hashCode() {
return (left.hashCode() * 17 + right.hashCode()) * 239 + getClass().hashCode();
}
private boolean priorityParentheses(Expression a) {
return (((a instanceof AbstractOperation) &&
((AbstractOperation) a).getPriority() < this.getPriority()));
}
private boolean orderParentheses(Expression a) {
if (!(a instanceof AbstractOperation)) {
return false;
}
AbstractOperation binA = (AbstractOperation) a;
return (binA.isOrderImportant() || this.isOrderImportant()) &&
binA.getPriority() <= this.getPriority();
}
private String applyPriority(Expression exp, boolean priority) {
return (priority ? "(" : "") + exp.toMiniString() + (priority ? ")" : "");
}
public String toMiniString() {
return applyPriority(left, priorityParentheses(left)) + " " + getSign() +
" " + applyPriority(right, priorityParentheses(right) || orderParentheses(right));
}
public String toString() {
return "(" + left + " " + getSign() + " " + right + ")";
}
}