-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogical.java
86 lines (76 loc) · 2.21 KB
/
Logical.java
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
public class Logical extends Expression {
@Override
int calculate() {
int left_number = 0;
int right_number = 0;
if(left != null)
left_number = left.calculate();
if(right != null)
right_number = right.calculate();
int result = 0;
switch (opcode){
case and:
result = left_number & right_number;
break;
case or:
result = left_number | right_number;
break;
case xor:
result = left_number ^ right_number;
break;
}
return result;
}
@Override
String toJSON() {
StringBuilder json = new StringBuilder();
json.append("{\n");
switch (opcode){
case and:
json.append(" \"Operation\" : \"" + "and" + "\",\n");
break;
case or:
json.append(" \"Operation\" : \"" + "or" + "\",\n");
break;
case xor:
json.append(" \"Operation\" : \"" + "xor" + "\",\n");
break;
}
if(left != null){
json.append(" \"LeftExpression\" : \n").append(JSONHandler.handle(left.toJSON()));
}
if(right != null){
if(left != null)
json.append(",\n");
json.append(" \"RightExpression\" : \n").append(JSONHandler.handle(right.toJSON()));
}
json.append("\n}");
return json.toString();
}
protected enum Opcode { and, or, xor, none }
private Opcode opcode;
private Expression left, right;
Logical(Opcode opcode, Expression left, Expression right) {
this.opcode = opcode;
this.left = left;
this.right = right;
}
public Opcode getOpcode() {
return opcode;
}
public void setOpcode(Opcode opcode) {
this.opcode = opcode;
}
public Expression getLeft() {
return left;
}
public void setLeft(Expression left) {
this.left = left;
}
public Expression getRight() {
return right;
}
public void setRight(Expression right) {
this.right = right;
}
}