View Javadoc
1   /*
2    * Copyright 2016 Andrew Rucker Jones.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.opencsv.exceptions;
17  
18  /**
19   * This exception is thrown when logical connections between data fields would
20   * be violated by the imported data.
21   * <p>This can be for constraints like making certain a number is in a certain
22   * range, or it can even be thrown by code using opencsv when constraints
23   * outside of opencsv would be violated. An example of the latter is importing
24   * into a database when one of the field in the CSV is supposed to contain the
25   * primary key for a foreign table, but the foreign key cannot be satisfied.</p>
26   * <p>This exception is not currently used by opencsv itself, since opencsv has
27   * no concept of what data consistency means in the context of the application
28   * using it. It is meant more for custom converters.</p>
29   *
30   * @author Andrew Rucker Jones
31   * @since 3.8
32   */
33  public class CsvConstraintViolationException extends CsvFieldAssignmentException {
34      private static final long serialVersionUID = 1L;
35  
36      private transient final Object sourceObject;
37  
38      /**
39       * Default constructor, in case no further information is necessary or
40       * available.
41       */
42      public CsvConstraintViolationException() {
43          sourceObject = null;
44      }
45  
46      /**
47       * Constructor for setting the source object that triggered the constraint
48       * violation.
49       *
50       * @param sourceObject The offending source object
51       */
52      public CsvConstraintViolationException(Object sourceObject) {
53          this.sourceObject = sourceObject;
54      }
55  
56      /**
57       * Constructor with a simple text.
58       *
59       * @param message Human-readable error text
60       */
61      public CsvConstraintViolationException(String message) {
62          super(message);
63          sourceObject = null;
64      }
65  
66      /**
67       * Constructor for setting the source object and an error message.
68       *
69       * @param sourceObject The offending source object
70       * @param message      Human-readable error text
71       */
72      public CsvConstraintViolationException(Object sourceObject, String message) {
73          super(message);
74          this.sourceObject = sourceObject;
75      }
76  
77      /**
78       * Gets the object that would have caused a constraint violation.
79       * {@code sourceObject} is marked {@code transient}, because
80       * {@link java.lang.Object} is not {@link java.io.Serializable}. If
81       * for any reason this exception is serialized and deserialized, this method
82       * will subsequently return {@code null}.
83       * 
84       * @return The source object that triggered the constraint violation
85       */
86      public Object getSourceObject() {
87          return sourceObject;
88      }
89  }