39
39
*/
40
40
package org .glassfish .jersey .internal ;
41
41
42
+ import java .util .ArrayDeque ;
42
43
import java .util .ArrayList ;
44
+ import java .util .Collection ;
43
45
import java .util .Collections ;
46
+ import java .util .Deque ;
44
47
import java .util .List ;
45
48
import java .util .concurrent .Callable ;
46
49
import java .util .logging .Logger ;
@@ -117,36 +120,72 @@ public static void hint(final Object source, final String message) {
117
120
getInstance ().issues .add (new ErrorMessage (source , message , Severity .HINT ));
118
121
}
119
122
120
- private static List <ErrorMessage > processErrors (final boolean throwException ) {
121
- final List <ErrorMessage > messages = new ArrayList <ErrorMessage >(errors .get ().issues );
123
+ /**
124
+ * Log errors and throw an exception if there are any fatal issues detected and
125
+ * the {@code throwException} flag has been set to {@code true}.
126
+ *
127
+ * @param throwException if set to {@code true}, any fatal issues will cause a {@link ErrorMessagesException}
128
+ * to be thrown.
129
+ */
130
+ private static void processErrors (final boolean throwException ) {
131
+ final List <ErrorMessage > errors = new ArrayList <ErrorMessage >(Errors .errors .get ().issues );
132
+ boolean isFatal = logErrors (errors );
133
+ if (throwException && isFatal ) {
134
+ throw new ErrorMessagesException (errors );
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Log errors and return a status flag indicating whether a fatal issue has been found
140
+ * in the error collection.
141
+ * <p>
142
+ * The {@code afterMark} flag indicates whether only those issues should be logged that were
143
+ * added after a {@link #mark() mark has been set}.
144
+ * </p>
145
+ *
146
+ * @param afterMark if {@code true}, only issues added after a mark has been set are returned,
147
+ * if {@code false} all issues are returned.
148
+ * @return {@code true} if there are any fatal issues present in the collection, {@code false}
149
+ * otherwise.
150
+ */
151
+ public static boolean logErrors (final boolean afterMark ) {
152
+ return logErrors (getInstance ()._getErrorMessages (afterMark ));
153
+ }
154
+
155
+ /**
156
+ * Log supplied errors and return a status flag indicating whether a fatal issue has been found
157
+ * in the error collection.
158
+ *
159
+ * @param errors a collection of errors to be logged.
160
+ * @return {@code true} if there are any fatal issues present in the collection, {@code false}
161
+ * otherwise.
162
+ */
163
+ private static boolean logErrors (final Collection <ErrorMessage > errors ) {
122
164
boolean isFatal = false ;
123
165
124
- if (!messages .isEmpty ()) {
125
- StringBuilder errors = new StringBuilder ("\n " );
166
+ if (!errors .isEmpty ()) {
167
+ StringBuilder fatals = new StringBuilder ("\n " );
126
168
StringBuilder warnings = new StringBuilder ();
127
169
StringBuilder hints = new StringBuilder ();
128
170
129
- for (final ErrorMessage issue : messages ) {
130
- switch (issue .getSeverity ()) {
171
+ for (final ErrorMessage error : errors ) {
172
+ switch (error .getSeverity ()) {
131
173
case FATAL :
132
174
isFatal = true ;
133
- errors .append (LocalizationMessages .ERROR_MSG (issue .getMessage ())).append ('\n' );
175
+ fatals .append (LocalizationMessages .ERROR_MSG (error .getMessage ())).append ('\n' );
134
176
break ;
135
177
case WARNING :
136
- warnings .append (LocalizationMessages .WARNING_MSG (issue .getMessage ())).append ('\n' );
178
+ warnings .append (LocalizationMessages .WARNING_MSG (error .getMessage ())).append ('\n' );
137
179
break ;
138
180
case HINT :
139
- warnings .append (LocalizationMessages .HINT_MSG (issue .getMessage ())).append ('\n' );
181
+ warnings .append (LocalizationMessages .HINT_MSG (error .getMessage ())).append ('\n' );
140
182
break ;
141
183
}
142
184
}
143
185
144
186
if (isFatal ) {
145
- LOGGER .severe (LocalizationMessages .ERRORS_AND_WARNINGS_DETECTED (errors .append (warnings )
187
+ LOGGER .severe (LocalizationMessages .ERRORS_AND_WARNINGS_DETECTED (fatals .append (warnings )
146
188
.append (hints ).toString ()));
147
- if (throwException ) {
148
- throw new ErrorMessagesException (messages );
149
- }
150
189
} else {
151
190
if (warnings .length () > 0 ) {
152
191
LOGGER .warning (LocalizationMessages .WARNINGS_DETECTED (warnings .toString ()));
@@ -158,9 +197,10 @@ private static List<ErrorMessage> processErrors(final boolean throwException) {
158
197
}
159
198
}
160
199
161
- return messages ;
200
+ return isFatal ;
162
201
}
163
202
203
+
164
204
/**
165
205
* Check whether a fatal error is present in the list of all messages.
166
206
*
@@ -310,9 +350,10 @@ public static List<ErrorMessage> getErrorMessages() {
310
350
311
351
/**
312
352
* Get the list of error messages.
313
- * <p/ >
353
+ * <p>
314
354
* The {@code afterMark} flag indicates whether only those issues should be returned that were
315
355
* added after a {@link #mark() mark has been set}.
356
+ * </p>
316
357
*
317
358
* @param afterMark if {@code true}, only issues added after a mark has been set are returned,
318
359
* if {@code false} all issues are returned.
@@ -337,9 +378,8 @@ public static void unmark() {
337
378
}
338
379
339
380
/**
340
- * Removes all issues that have been added since the last marked position.
341
- * <p/>
342
- * If no mark has been set in the errors list, the method call is ignored.
381
+ * Removes all issues that have been added since the last marked position as well as
382
+ * removes the last mark.
343
383
*/
344
384
public static void reset () {
345
385
getInstance ()._reset ();
@@ -350,21 +390,23 @@ public static void reset() {
350
390
private Errors () {
351
391
}
352
392
353
- private int mark = - 1 ;
393
+ private Deque < Integer > mark = new ArrayDeque < Integer >( 4 ) ;
354
394
private int stack = 0 ;
355
395
356
396
private void _mark () {
357
- mark = issues .size ();
397
+ mark . addLast ( issues .size () );
358
398
}
359
399
360
400
private void _unmark () {
361
- mark = - 1 ;
401
+ mark . pollLast () ;
362
402
}
363
403
364
404
private void _reset () {
365
- if (mark >= 0 && mark < issues .size ()) {
366
- issues .subList (mark , issues .size ()).clear ();
367
- _unmark ();
405
+ final Integer _pos = mark .pollLast (); // also performs "unmark" functionality
406
+ final int markedPos = (_pos == null ) ? -1 : _pos ;
407
+
408
+ if (markedPos >= 0 && markedPos < issues .size ()) {
409
+ issues .subList (markedPos , issues .size ()).clear ();
368
410
}
369
411
}
370
412
@@ -387,11 +429,16 @@ private void postProcess(boolean throwException) {
387
429
}
388
430
389
431
private List <ErrorMessage > _getErrorMessages (final boolean afterMark ) {
390
- if (afterMark && mark >= 0 && mark < issues .size ()) {
391
- return Collections .unmodifiableList (new ArrayList <ErrorMessage >(issues .subList (mark , issues .size ())));
392
- } else {
393
- return Collections .unmodifiableList (new ArrayList <ErrorMessage >(issues ));
432
+ if (afterMark ) {
433
+ final Integer _pos = mark .peekLast ();
434
+ final int markedPos = (_pos == null ) ? -1 : _pos ;
435
+
436
+ if (markedPos >= 0 && markedPos < issues .size ()) {
437
+ return Collections .unmodifiableList (new ArrayList <ErrorMessage >(issues .subList (markedPos , issues .size ())));
438
+ } // else return all errors
394
439
}
440
+
441
+ return Collections .unmodifiableList (new ArrayList <ErrorMessage >(issues ));
395
442
}
396
443
397
444
/**
@@ -433,7 +480,7 @@ private ErrorMessage(final Object source, final String message, Severity severit
433
480
/**
434
481
* Get {@link Severity}.
435
482
*
436
- * @return severity of current {@link ErrorMessage}.
483
+ * @return severity of current {@code ErrorMessage}.
437
484
*/
438
485
public Severity getSeverity () {
439
486
return severity ;
0 commit comments