1 package com.randomnoun.common;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.Comparator;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.ListIterator;
13 import java.util.Set;
14 import java.util.Spliterator;
15 import java.util.function.Consumer;
16 import java.util.function.Predicate;
17 import java.util.function.UnaryOperator;
18 import java.util.stream.Stream;
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 public class ErrorList implements List<ErrorList.ErrorData>, Serializable {
49
50
51 private static final long serialVersionUID = 4246736116021572339L;
52
53
54
55 List<ErrorList.ErrorData> delegate;
56 boolean threadsafe = false;
57
58
59 public static final int SEVERITY_OK = 0;
60
61
62 public static final int SEVERITY_INVALID = 1;
63
64
65 public static final int SEVERITY_ERROR = 2;
66
67
68 public static final int SEVERITY_FATAL = 3;
69
70
71 public static final int SEVERITY_PANIC = 4;
72
73
74
75
76
77
78 public static final int SEVERITY_INFO = 5;
79
80
81 public static final int SEVERITY_WARNING = 6;
82
83
84
85
86
87 public static class ErrorData extends HashMap<String, Object>
88 {
89
90 private static final long serialVersionUID = -176737615399095851L;
91
92
93
94
95
96
97
98
99
100
101 public ErrorData(String shortText, String longText, String errorField,
102 int severity)
103 {
104 super();
105 put("shortText", shortText);
106 put("longText", longText);
107 put("field", errorField);
108 put("severity", Integer.valueOf(severity));
109 }
110
111 @Override
112 public int hashCode() {
113 return super.hashCode();
114 }
115
116 @Override
117 public boolean equals(Object obj) {
118 return super.equals(obj);
119 }
120
121
122
123
124 public String getShortText()
125 {
126 return (String) get("shortText");
127 }
128
129
130
131
132 public String getLongText()
133 {
134 return (String) get("longText");
135 }
136
137
138
139
140 public String getField()
141 {
142 return (String) get("field");
143 }
144
145
146
147
148 public int getSeverity()
149 {
150 return ((Integer) get("severity")).intValue();
151 }
152
153
154
155
156 public String toString()
157 {
158 return "{shortText='" + getShortText() + "', longText='" + getLongText() +
159 "', fields='" + getField() + "', severity=" + getSeverity() + "}";
160 }
161 }
162
163
164
165
166 public ErrorList()
167 {
168 delegate = new ArrayList<>();
169 }
170
171
172
173
174
175 public synchronized void makeThreadsafe() {
176 if (!threadsafe) {
177 delegate = Collections.synchronizedList(delegate);
178 threadsafe = true;
179 }
180 }
181
182
183 public void removeDuplicates() {
184 Set<ErrorList.ErrorData> uniqueData = new HashSet<>();
185 synchronized(delegate) {
186 for (Iterator<ErrorList.ErrorData> i = this.iterator(); i.hasNext(); ) {
187 ErrorList.ErrorData ed = i.next();
188 if (uniqueData.contains(ed)) {
189 i.remove();
190 } else {
191 uniqueData.add(ed);
192 }
193 }
194 }
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209 public void addError(String errorField, String shortText, String longText,
210 int severity)
211 {
212 delegate.add(new ErrorData(shortText, longText, errorField, severity));
213 }
214
215
216
217
218
219
220
221
222
223
224 public void addError(String errorField, String shortText, String longText)
225 {
226 addError(errorField, shortText, longText, SEVERITY_ERROR);
227 }
228
229
230
231
232
233
234
235
236
237
238 public void addError(String shortText, String longText, int severity)
239 {
240 addError("", shortText, longText, severity);
241 }
242
243
244
245
246
247
248
249
250
251 public void addError(String shortText, String longText)
252 {
253 addError("", shortText, longText, SEVERITY_ERROR);
254 }
255
256
257
258
259 public void clearErrors()
260 {
261 delegate.clear();
262 }
263
264
265
266
267
268
269
270
271
272
273 public boolean addErrors(ErrorList errorList)
274 {
275 if (errorList == null) {
276 return false;
277 }
278 if (errorList.size() == 0) {
279 return false;
280 }
281 int maxSeverity = Integer.MIN_VALUE;
282 int severity;
283
284 for (int i = 0; i < errorList.size(); i++) {
285 severity = errorList.getSeverityAt(i);
286 add(errorList.get(i));
287 if (severity > maxSeverity) {
288 maxSeverity = severity;
289 }
290 }
291
292 if (maxSeverity >= SEVERITY_ERROR) {
293 return true;
294 } else {
295 return false;
296 }
297 }
298
299
300
301
302
303
304
305 public boolean hasErrorOn(String field)
306 {
307 ErrorData errorInfo;
308
309 synchronized(delegate) {
310 for (Iterator<ErrorData> i = delegate.iterator(); i.hasNext(); ) {
311 errorInfo = i.next();
312 if (errorInfo.getField()!=null) {
313 String[] errorFields = errorInfo.getField().split(",");
314 for (int j = 0; j < errorFields.length; j++) {
315 if (errorFields[j].equals(field)) {
316 return true;
317 }
318 }
319 }
320 }
321 }
322 return false;
323 }
324
325
326
327
328
329
330 public boolean hasErrors()
331 {
332 return size() > 0;
333 }
334
335
336
337
338
339
340
341
342
343
344 public boolean hasErrors(int severity)
345 {
346 synchronized(delegate) {
347 for (Iterator<ErrorData> i = this.iterator(); i.hasNext(); ){
348 ErrorData errorData = (ErrorData) i.next();
349 if (errorData.getSeverity() >= severity ) {
350 return true;
351 }
352 }
353 }
354 return false;
355 }
356
357
358
359
360
361
362
363
364
365
366 public int maxErrorSeverity()
367 {
368 int maxSeverity = -1;
369 int curSeverity;
370
371 synchronized(delegate) {
372 for (int i = 0; i < size(); i++) {
373 curSeverity = getSeverityAt(i);
374 if (curSeverity > maxSeverity) {
375 maxSeverity = curSeverity;
376 }
377 }
378 }
379 return maxSeverity;
380 }
381
382
383
384
385
386
387 public int size() {
388 return delegate.size();
389 }
390
391
392
393
394
395
396 public int getSize() {
397 return size();
398 }
399
400
401
402
403 public String getShortTextAt(int pos) {
404 return ((ErrorData) delegate.get(pos)).getShortText();
405 }
406
407
408
409
410 public String getLongTextAt(int pos) {
411 return ((ErrorData) delegate.get(pos)).getLongText();
412 }
413
414
415
416
417 public String getFieldAt(int pos) {
418 return ((ErrorData) delegate.get(pos)).getField();
419 }
420
421
422
423
424 public int getSeverityAt(int pos) {
425 return ((ErrorData) delegate.get(pos)).getSeverity();
426 }
427
428
429
430
431
432
433
434 public String toString() {
435 StringBuffer sb = new StringBuffer();
436 ErrorData errorData;
437 synchronized(delegate) {
438 for (Iterator<ErrorData> i = delegate.iterator(); i.hasNext(); ) {
439 errorData = (ErrorData)i.next();
440
441 sb.append("#");
442 sb.append(errorData.getShortText() + " - ");
443 sb.append(errorData.getLongText());
444 if (!Text.isBlank(errorData.getField())) {
445 sb.append(" [" + errorData.getField() + "]\n");
446 }
447 }
448 }
449 return sb.toString();
450 }
451
452
453
454
455
456 public String toJSON() {
457 StringBuffer sb = new StringBuffer();
458 ErrorList.ErrorData errorData;
459 sb.append("[");
460 for (int i=0; i<this.size(); i++) {
461 errorData = (ErrorData) get(i);
462 if (i>0) { sb.append(","); }
463 sb.append(
464 "{\"shortText\":\"" + Text.escapeJavascript(errorData.getShortText()) + "\"," +
465 "\"longText\":\"" + Text.escapeJavascript(errorData.getLongText()) + "\"," +
466 "\"fields\":\"" + Text.escapeJavascript(errorData.getField()) + "\"," +
467 "\"severity\":" + errorData.getSeverity() + "}");
468 }
469 sb.append("]");
470 return sb.toString();
471 }
472
473
474
475
476 public void forEach(Consumer<? super ErrorData> action) {
477 delegate.forEach(action);
478 }
479
480 public boolean isEmpty() {
481 return delegate.isEmpty();
482 }
483
484 public boolean contains(Object o) {
485 return delegate.contains(o);
486 }
487
488 public Iterator<ErrorData> iterator() {
489 return delegate.iterator();
490 }
491
492 public Object[] toArray() {
493 return delegate.toArray();
494 }
495
496 public <T> T[] toArray(T[] a) {
497 return delegate.toArray(a);
498 }
499
500 public boolean add(ErrorData e) {
501 return delegate.add(e);
502 }
503
504 public boolean remove(Object o) {
505 return delegate.remove(o);
506 }
507
508 public boolean containsAll(Collection<?> c) {
509 return delegate.containsAll(c);
510 }
511
512 public boolean addAll(Collection<? extends ErrorData> c) {
513 return delegate.addAll(c);
514 }
515
516 public boolean addAll(int index, Collection<? extends ErrorData> c) {
517 return delegate.addAll(index, c);
518 }
519
520 public boolean removeAll(Collection<?> c) {
521 return delegate.removeAll(c);
522 }
523
524 public boolean retainAll(Collection<?> c) {
525 return delegate.retainAll(c);
526 }
527
528 public void replaceAll(UnaryOperator<ErrorData> operator) {
529 delegate.replaceAll(operator);
530 }
531
532 public boolean removeIf(Predicate<? super ErrorData> filter) {
533 return delegate.removeIf(filter);
534 }
535
536 public void sort(Comparator<? super ErrorData> c) {
537 delegate.sort(c);
538 }
539
540 public void clear() {
541 delegate.clear();
542 }
543
544 public boolean equals(Object o) {
545 return delegate.equals(o);
546 }
547
548 public int hashCode() {
549 return delegate.hashCode();
550 }
551
552 public ErrorData get(int index) {
553 return delegate.get(index);
554 }
555
556 public ErrorData set(int index, ErrorData element) {
557 return delegate.set(index, element);
558 }
559
560 public void add(int index, ErrorData element) {
561 delegate.add(index, element);
562 }
563
564 public Stream<ErrorData> stream() {
565 return delegate.stream();
566 }
567
568 public ErrorData remove(int index) {
569 return delegate.remove(index);
570 }
571
572 public Stream<ErrorData> parallelStream() {
573 return delegate.parallelStream();
574 }
575
576 public int indexOf(Object o) {
577 return delegate.indexOf(o);
578 }
579
580 public int lastIndexOf(Object o) {
581 return delegate.lastIndexOf(o);
582 }
583
584 public ListIterator<ErrorData> listIterator() {
585 return delegate.listIterator();
586 }
587
588 public ListIterator<ErrorData> listIterator(int index) {
589 return delegate.listIterator(index);
590 }
591
592 public List<ErrorData> subList(int fromIndex, int toIndex) {
593 return delegate.subList(fromIndex, toIndex);
594 }
595
596 public Spliterator<ErrorData> spliterator() {
597 return delegate.spliterator();
598 }
599
600
601 }