1 package com.opencsv.bean.exceptionhandler;
2
3 import com.opencsv.exceptions.CsvException;
4
5 import java.util.concurrent.atomic.AtomicInteger;
6
7 /**
8 * <p>An exception handler that queues the first x exceptions, then throws any
9 * further exceptions.</p>
10 *
11 * <p><b>Note:</b> when testing this on systems with a high number of cores/threads under
12 * load we noted discrepancies between the number of exceptions counted and the number
13 * exceptions queued. If it is actually important to see the exceptions thrown then
14 * we would heavily recommend you use the single threaded iterator() in CsvToBean
15 * and collecting the exceptions yourself.</p>
16 *
17 * @author Andrew Rucker Jones
18 * @since 5.2
19 */
20 final public class ExceptionHandlerQueueThenThrowAfter implements CsvExceptionHandler {
21
22 private final AtomicInteger count = new AtomicInteger();
23 private final int maxExceptions;
24
25 /**
26 * Creates an instance.
27 * @param maxExceptions The number of exceptions that will be queued. Any
28 * exception handled after this limit will be thrown.
29 */
30 public ExceptionHandlerQueueThenThrowAfter(int maxExceptions) {
31 this.maxExceptions = maxExceptions;
32 }
33
34 @Override
35 public CsvException handleException(CsvException e) throws CsvException {
36 if(count.incrementAndGet() > maxExceptions) {
37 throw e;
38 }
39 return e;
40 }
41 }