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 should be thrown when the provided string value for conversion
20 * cannot be converted to the required type of the destination field.
21 *
22 * @author Andrew Rucker Jones
23 * @since 3.8
24 */
25 public class CsvDataTypeMismatchException extends CsvFieldAssignmentException {
26 private static final long serialVersionUID = 1L;
27
28 private transient final Object sourceObject;
29 private final Class<?> destinationClass;
30
31 /**
32 * Default constructor, in case no further information is necessary or
33 * available.
34 */
35 public CsvDataTypeMismatchException() {
36 sourceObject = null;
37 destinationClass = null;
38 }
39
40 /**
41 * Constructor for setting the data and the class of the intended
42 * destination field.
43 *
44 * @param sourceObject Object that was to be assigned to the destination
45 * field. This may not be available in all contexts.
46 * @param destinationClass Class of the destination field. This may not be
47 * available in all contexts.
48 */
49 public CsvDataTypeMismatchException(Object sourceObject, Class<?> destinationClass) {
50 this.sourceObject = sourceObject;
51 this.destinationClass = destinationClass;
52 }
53
54 /**
55 * Constructor with a simple text.
56 *
57 * @param message Human-readable error text
58 */
59 public CsvDataTypeMismatchException(String message) {
60 super(message);
61 sourceObject = null;
62 destinationClass = null;
63 }
64
65 /**
66 * Constructor for setting the data and the class of the intended
67 * destination field along with an error message.
68 *
69 * @param sourceObject Object that was to be assigned to the destination
70 * field. This may not be available in all contexts.
71 * @param destinationClass Class of the destination field. This may not be
72 * available in all contexts.
73 * @param message Human-readable error text
74 */
75 public CsvDataTypeMismatchException(Object sourceObject, Class<?> destinationClass, String message) {
76 super(message);
77 this.sourceObject = sourceObject;
78 this.destinationClass = destinationClass;
79 }
80
81 /**
82 * Gets the object that was to be assigned to a field of the wrong type.
83 * {@code sourceObject} is marked {@code transient}, because
84 * {@link java.lang.Object} is not {@link java.io.Serializable}. If
85 * for any reason this exception is serialized and deserialized, this method
86 * will subsequently return {@code null}.
87 *
88 * @return The data that could not be assigned
89 */
90 public Object getSourceObject() {
91 return sourceObject;
92 }
93
94 /**
95 * Gets the type of the field to which the data were to be assigned.
96 *
97 * @return The class of the destination field
98 */
99 public Class<?> getDestinationClass() {
100 return destinationClass;
101 }
102 }