1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.model.attributes;
19
20 import java.io.Serializable;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import com.thoughtworks.xstream.annotations.XStreamAlias;
25
26
27
28
29
30
31
32 public abstract class AttributeMergePolicy implements Serializable {
33
34 private static final long serialVersionUID = 6800412459178310697L;
35
36
37
38
39
40
41
42 @XStreamAlias("MERGE_POLICY_LOGICAL_OR")
43 public static final class MergePolicyLogicalOr extends AttributeMergePolicy {
44 private static final long serialVersionUID = -6698802554723374885L;
45
46 private MergePolicyLogicalOr() {
47 super();
48 }
49
50
51
52
53
54
55
56 @Override
57 public Object mergeValues(Attribute attribute, Object firstValue, Object secondValue) {
58 checkIfBoolean(firstValue, secondValue);
59 return ((Boolean)firstValue) || ((Boolean)secondValue);
60 }
61
62
63
64
65
66
67 @Override
68 public String getId() {
69 return "MERGE_POLICY_LOGICAL_OR";
70 }
71 }
72
73
74
75
76
77
78
79 @XStreamAlias("MERGE_POLICY_LOGICAL_AND")
80 public static final class MergePolicyLogicalAnd extends AttributeMergePolicy {
81 private static final long serialVersionUID = 2306183502914013676L;
82
83 private MergePolicyLogicalAnd() {
84 super();
85 }
86
87
88
89
90
91
92
93 @Override
94 public Object mergeValues(Attribute attribute, Object firstValue, Object secondValue) {
95 checkIfBoolean(firstValue, secondValue);
96 return ((Boolean)firstValue) && ((Boolean)secondValue);
97 }
98
99
100
101
102
103
104 @Override
105 public String getId() {
106 return "MERGE_POLICY_LOGICAL_AND";
107 }
108 }
109
110
111
112
113
114
115 @XStreamAlias("MERGE_POLICY_ALWAYS_SECOND")
116 public static final class MergePolicyAlwaysSecond extends AttributeMergePolicy {
117 private static final long serialVersionUID = -6798718594185971003L;
118
119 private MergePolicyAlwaysSecond() {
120 super();
121 }
122
123
124
125
126
127
128
129 @Override
130 public Object mergeValues(Attribute attribute, Object firstValue, Object secondValue) {
131 return secondValue;
132 }
133
134
135
136
137
138
139 @Override
140 public String getId() {
141 return "MERGE_POLICY_ALWAYS_SECOND";
142 }
143 }
144
145
146
147
148
149
150 @XStreamAlias("MERGE_POLICY_ALWAYS_FIRST")
151 public static final class MergePolicyAlwaysFirst extends AttributeMergePolicy {
152 private static final long serialVersionUID = 3645856921319121245L;
153
154 private MergePolicyAlwaysFirst() {
155 super();
156 }
157
158
159
160
161
162
163
164 @Override
165 public Object mergeValues(Attribute attribute, Object firstValue, Object secondValue) {
166 return firstValue;
167 }
168
169
170
171
172
173
174 @Override
175 public String getId() {
176 return "MERGE_POLICY_ALWAYS_FIRST";
177 }
178
179 };
180
181
182
183
184 private static Map<String, AttributeMergePolicy> policyRegistry = new HashMap<String, AttributeMergePolicy>();
185
186
187
188
189 public static final AttributeMergePolicy MERGE_POLICY_ALWAYS_FIRST = new MergePolicyAlwaysFirst();
190
191
192
193
194 public static final AttributeMergePolicy MERGE_POLICY_ALWAYS_SECOND = new MergePolicyAlwaysSecond();
195
196
197
198
199
200 public static final AttributeMergePolicy MERGE_POLICY_LOGICAL_AND = new MergePolicyLogicalAnd();
201
202
203
204
205
206 public static final AttributeMergePolicy MERGE_POLICY_LOGICAL_OR = new MergePolicyLogicalOr();
207
208 protected AttributeMergePolicy() {
209 if (!policyRegistry.containsKey(getId())) {
210 policyRegistry.put(toString(), this);
211 } else if (!policyRegistry.get(getId()).getClass().equals(this.getClass())) {
212 throw new AttributeMergePolicyNotUniqueException(
213 "An instance of AttributeMergePolicy with given ID is already registered: "
214 + getId());
215 }
216 }
217
218
219
220
221
222
223
224
225 protected boolean checkIfBoolean(Object firstValue, Object secondValue) {
226 if (!(firstValue instanceof Boolean)) {
227 throw new IllegalArgumentException("First value is not of type Boolean");
228 }
229 if (!(secondValue instanceof Boolean)) {
230 throw new IllegalArgumentException("Second value is not of type Boolean");
231 }
232 return true;
233 }
234
235
236
237
238
239
240 public abstract String getId();
241
242
243
244
245
246
247
248
249
250
251
252
253
254 public abstract Object mergeValues(Attribute attribute, Object firstValue, Object secondValue);
255
256
257
258
259
260
261
262
263 public static final AttributeMergePolicy resolve(String id) {
264 return policyRegistry.get(id);
265 }
266
267
268
269
270
271
272 @Override
273 public boolean equals(Object obj) {
274 if (obj == this) {
275 return true;
276 }
277 if (!(obj instanceof AttributeMergePolicy)) {
278 return false;
279 }
280 AttributeMergePolicy that = (AttributeMergePolicy)obj;
281 if (!this.getId().equals(that.getId())) {
282 return false;
283 }
284 return true;
285 }
286
287
288
289
290
291
292 @Override
293 public String toString() {
294 return getId();
295 }
296
297
298
299
300
301
302 @Override
303 public int hashCode() {
304 return getId().hashCode();
305 }
306
307 }