1 package org.thegalactic.context.constraint.numerical;
2
3
4
5
6
7
8
9
10
11
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16
17
18
19 public final class NumericalStorage {
20
21
22
23
24 private final List<Double> inf;
25
26
27
28
29 private final List<Double> sup;
30
31
32
33
34 private static final String EXCEPTION_SIZE = "NumericalStorage objects must have the same size";
35
36
37
38
39
40
41
42
43 public static NumericalStorage create(final int size) {
44 return new NumericalStorage(size);
45 }
46
47
48
49
50
51
52 private NumericalStorage(final int size) {
53 this.inf = new ArrayList<Double>(size);
54 this.sup = new ArrayList<Double>(size);
55
56 for (int index = 0; index < size; index++) {
57 this.inf.add(index, Double.POSITIVE_INFINITY);
58 this.sup.add(index, Double.NEGATIVE_INFINITY);
59 }
60 }
61
62
63
64
65
66
67
68
69 public NumericalStorage setEmpty(final int index) {
70 this.inf.set(index, Double.POSITIVE_INFINITY);
71 this.sup.set(index, Double.NEGATIVE_INFINITY);
72 return this;
73 }
74
75
76
77
78
79
80
81
82 public boolean isEmpty(final int index) {
83 return this.inf.get(index) > this.sup.get(index);
84 }
85
86
87
88
89
90
91
92
93
94 public NumericalStorage setPoint(final int index, final double value) {
95 this.inf.set(index, value);
96 this.sup.set(index, value);
97 return this;
98 }
99
100
101
102
103
104
105
106
107 public boolean isPoint(final int index) {
108 return this.inf.get(index).equals(this.sup.get(index));
109 }
110
111
112
113
114
115
116
117
118
119 public NumericalStorage setInf(final int index, final double value) {
120 if (this.isEmpty(index)) {
121 this.setPoint(index, value);
122 } else if (value > this.sup.get(index)) {
123 this.setEmpty(index);
124 } else {
125 this.inf.set(index, value);
126 }
127 return this;
128 }
129
130
131
132
133
134
135
136
137
138 public NumericalStorage setSup(final int index, final double value) {
139 if (this.isEmpty(index)) {
140 this.setPoint(index, value);
141 } else if (value < this.inf.get(index)) {
142 this.setEmpty(index);
143 } else {
144 this.sup.set(index, value);
145 }
146 return this;
147 }
148
149
150
151
152
153
154
155
156 public double getInf(final int index) {
157 return this.inf.get(index);
158 }
159
160
161
162
163
164
165
166
167 public double getSup(final int index) {
168 return this.sup.get(index);
169 }
170
171
172
173
174
175
176
177
178
179 public NumericalStorage reduceInf(final int index, final double value) {
180 this.inf.set(index, Math.max(this.inf.get(index), value));
181 return this;
182 }
183
184
185
186
187
188
189
190
191
192 public NumericalStorage reduceSup(final int index, final double value) {
193 this.sup.set(index, Math.min(this.sup.get(index), value));
194 return this;
195 }
196
197
198
199
200
201
202
203
204
205 public NumericalStorage extendInf(final int index, final double value) {
206 this.inf.set(index, Math.min(this.inf.get(index), value));
207 return this;
208 }
209
210
211
212
213
214
215
216
217
218 public NumericalStorage extendSup(final int index, final double value) {
219 this.sup.set(index, Math.max(this.sup.get(index), value));
220 return this;
221 }
222
223
224
225
226
227
228
229
230
231 public NumericalStorage extend(final int index, final double value) {
232 this.extendInf(index, value);
233 this.extendSup(index, value);
234 return this;
235 }
236
237
238
239
240
241
242
243
244
245
246 public NumericalStorage intersection(final NumericalStorage storage) {
247 final int size = this.inf.size();
248 if (storage.inf.size() == size) {
249 for (int index = 0; index < size; index++) {
250 this.inf.set(index, Math.max(this.inf.get(index), storage.inf.get(index)));
251 this.sup.set(index, Math.min(this.sup.get(index), storage.sup.get(index)));
252 }
253 return this;
254 } else {
255 throw new IllegalArgumentException(EXCEPTION_SIZE);
256 }
257 }
258
259
260
261
262
263
264
265
266
267
268
269 public NumericalStorage union(final NumericalStorage storage) {
270 final int size = this.inf.size();
271 if (storage.inf.size() == size) {
272 for (int index = 0; index < size; index++) {
273 this.inf.set(index, Math.min(this.inf.get(index), storage.inf.get(index)));
274 this.sup.set(index, Math.max(this.sup.get(index), storage.sup.get(index)));
275 }
276 return this;
277 } else {
278 throw new IllegalArgumentException(EXCEPTION_SIZE);
279 }
280 }
281
282
283
284
285
286
287 public int size() {
288 return this.inf.size();
289 }
290
291
292
293
294
295
296 @Override
297 public String toString() {
298 final StringBuilder builder = new StringBuilder();
299
300 builder.append('[');
301
302 final int size = this.inf.size();
303 for (int index = 0; index < size; index++) {
304 if (index != 0) {
305 builder.append(", ");
306 }
307 if (this.isEmpty(index)) {
308 builder.append('@');
309 } else if (this.isPoint(index)) {
310 builder.append(this.inf.get(index));
311 } else {
312 builder.append('(');
313 builder.append(this.inf.get(index));
314 builder.append(',');
315 builder.append(this.sup.get(index));
316 builder.append(')');
317 }
318 }
319
320 builder.append(']');
321
322 return builder.toString();
323 }
324 }