1 package com.opencsv.exceptions;
2
3 import java.util.LinkedList;
4 import java.util.List;
5
6 /**
7 * An exception class for collecting multiple exceptions.
8 * For internal use only.
9 *
10 * @author Andrew Rucker Jones
11 * @since 5.3
12 */
13 public class CsvChainedException extends CsvException {
14
15 private final List<CsvFieldAssignmentException> exceptionChain = new LinkedList<>();
16
17 /**
18 * Constructor.
19 * @param csve The first exception for the list being collected.
20 * Must not be {@code null}.
21 */
22 public CsvChainedException(CsvFieldAssignmentException csve) {
23 exceptionChain.add(csve);
24 }
25
26 /**
27 * Add an exception to the chain of collections.
28 * @param csve Exception to be added to this chain.
29 * Must not be {@code null}.
30 */
31 public void add(CsvFieldAssignmentException csve) {
32 exceptionChain.add(csve);
33 }
34
35 /**
36 * @return A list of all exceptions collected
37 */
38 public List<CsvFieldAssignmentException> getExceptionChain() {
39 return exceptionChain;
40 }
41
42 /** Sets the line for all exceptions collected. */
43 // The rest of the Javadoc is inherited
44 @Override
45 public void setLine(String[] line) {
46 super.setLine(line);
47 exceptionChain.forEach(e -> e.setLine(line));
48 }
49
50 /** Sets the line number for all exceptions collected. */
51 // The rest of the Javadoc is inherited
52 @Override
53 public void setLineNumber(long lineNumber) {
54 super.setLineNumber(lineNumber);
55 exceptionChain.forEach(e -> e.setLineNumber(lineNumber));
56 }
57
58 /**
59 * Convenience method that checks if the chain only has a single exception.
60 *
61 * @return {@code true} if chain has only a single exception,
62 * {@code false} otherwise
63 */
64 public boolean hasOnlyOneException() {
65 return exceptionChain.size() == 1;
66 }
67
68 /**
69 * Convenience method to return the first exception from the exception chain.
70 *
71 * @return {@link CsvFieldAssignmentException} at the first position in the
72 * list, {@code null} otherwise.
73 */
74 public CsvFieldAssignmentException getFirstException() {
75 return exceptionChain.isEmpty() ? null : exceptionChain.get(0);
76 }
77 }