forked from john-bai/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.java
More file actions
148 lines (115 loc) · 2.9 KB
/
Copy pathInterpreter.java
File metadata and controls
148 lines (115 loc) · 2.9 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package designpatterns;
import java.util.Map;
import java.util.HashMap;
/**
*
* @author jtherrell
*/
interface BooleanExp {
public boolean evaluate(Context context);
public BooleanExp replace(String string, BooleanExp exp);
public BooleanExp copy();
}
class Context {
private Map<Object, Boolean> contextMap;
public Context() {
contextMap = new HashMap<Object, Boolean>();
contextMap.put(true, true);
contextMap.put(false, false);
}
public boolean lookup(Object obj) {
return contextMap.get(obj);
}
public void assign(VariableExp exp, boolean bool) {
contextMap.put(exp.name(), bool);
}
}
class VariableExp implements BooleanExp {
private String name;
public VariableExp(String name) {
this.name = name;
}
public String name() {
return name;
}
public boolean evaluate(Context context) {
return context.lookup(name);
}
public BooleanExp replace(String name, BooleanExp exp) {
if (this.name.equals(name))
return exp.copy();
else
return copy();
}
public BooleanExp copy() {
return new VariableExp(name);
}
}
class Constant implements BooleanExp {
Boolean bool;
public Constant(Boolean bool) {
this.bool = bool;
}
public boolean evaluate(Context context) {
return context.lookup(bool);
}
public BooleanExp replace(String string, BooleanExp exp) {
return this;
}
public BooleanExp copy() {
return new Constant(bool);
}
}
class AndExp implements BooleanExp {
private BooleanExp operand1, operand2;
public AndExp(BooleanExp operand1, BooleanExp operand2) {
this.operand1 = operand1;
this.operand2 = operand2;
}
public boolean evaluate(Context context) {
return operand1.evaluate(context) && operand2.evaluate(context);
}
public BooleanExp replace(String name, BooleanExp exp) {
return new AndExp(operand1.replace(name, exp),
operand2.replace(name, exp));
}
public BooleanExp copy() {
return new AndExp(operand1.copy(), operand2.copy());
}
}
class OrExp implements BooleanExp {
private BooleanExp operand1, operand2;
public OrExp(BooleanExp operand1, BooleanExp operand2) {
this.operand1 = operand1;
this.operand2 = operand2;
}
public boolean evaluate(Context context) {
return operand1.evaluate(context) || operand2.evaluate(context);
}
public BooleanExp replace(String name, BooleanExp exp) {
return new OrExp(operand1.replace(name, exp),
operand2.replace(name, exp));
}
public BooleanExp copy() {
return new OrExp(operand1.copy(), operand2.copy());
}
}
class NotExp implements BooleanExp {
private BooleanExp exp;
public NotExp(BooleanExp exp) {
this.exp = exp;
}
public boolean evaluate(Context context) {
return !exp.evaluate(context);
}
public BooleanExp replace(String name, BooleanExp exp) {
return new NotExp(exp.replace(name, exp));
}
public BooleanExp copy() {
return new NotExp(exp.copy());
}
}