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  import java.lang.reflect.Field;
19  import java.util.Collections;
20  import java.util.List;
21  import org.apache.commons.collections4.CollectionUtils;
22  import org.apache.commons.collections4.list.UnmodifiableList;
23  
24  /**
25   * This exception should be thrown when a field marked as required is empty in
26   * the CSV file.
27   *
28   * @author Andrew Rucker Jones
29   * @since 3.8
30   */
31  public class CsvRequiredFieldEmptyException extends CsvFieldAssignmentException {
32      private static final long serialVersionUID = 1L;
33  
34      private final Class<?> beanClass;
35      private transient final List<Field> destinationFields;
36  
37      /**
38       * Default constructor, in case no further information is necessary.
39       */
40      public CsvRequiredFieldEmptyException() {
41          beanClass = null;
42          destinationFields = Collections.emptyList();
43      }
44  
45      /**
46       * Constructor with a simple text.
47       *
48       * @param message Human-readable error text
49       */
50      public CsvRequiredFieldEmptyException(String message) {
51          super(message);
52          beanClass = null;
53          destinationFields = Collections.emptyList();
54      }
55  
56      /**
57       * Constructor for setting the intended class and field of the target bean.
58       * <p>These may not be known in every context.</p>
59       * <p>This constructor is provided for backward compatibility and for
60       * convenience if you only have one missing destination field.</p>
61       *
62       * @param beanClass        Class of the destination bean
63       * @param destinationField Field of the destination field in the destination bean
64       */
65      public CsvRequiredFieldEmptyException(Class<?> beanClass, Field destinationField) {
66          this.beanClass = beanClass;
67          this.destinationFields = Collections.singletonList(destinationField);
68      }
69      
70      /**
71       * Constructor for setting the intended class and fields of the target bean.
72       * These may not be known in every context.
73       *
74       * @param beanClass        Class of the destination bean
75       * @param destinationFields Fields of the destination fields in the destination bean
76       * @since 4.2
77       */
78      public CsvRequiredFieldEmptyException(Class<?> beanClass, List<Field> destinationFields) {
79          this.beanClass = beanClass;
80          this.destinationFields = new UnmodifiableList<>(destinationFields);
81      }
82      
83      /**
84       * Constructor for setting the intended class of the target bean and a
85       * human-readable error message.
86       * These may not be known in every context.
87       *
88       * @param beanClass Class of the destination bean
89       * @param message Human-readable error text
90       * @since 3.10
91       */
92      public CsvRequiredFieldEmptyException(Class<?> beanClass, String message) {
93          super(message);
94          this.beanClass = beanClass;
95          this.destinationFields = Collections.emptyList();
96      }
97  
98      /**
99       * Constructor for setting the intended class and field of the target bean
100      * along with an error message.
101      * <p>The class and field may not be known in every context.</p>
102      * <p>This constructor is provided for backward compatibility and for
103      * convenience if you only have one missing destination field.</p>
104      *
105      * @param beanClass        Class of the destination bean
106      * @param destinationField Field of the destination field in the destination bean
107      * @param message          Human-readable error text
108      */
109     public CsvRequiredFieldEmptyException(Class<?> beanClass, Field destinationField, String message) {
110         super(message);
111         this.beanClass = beanClass;
112         this.destinationFields = Collections.singletonList(destinationField);
113     }
114 
115     /**
116      * Constructor for setting the intended class and fields of the target bean
117      * along with an error message.
118      * The class and field may not be known in every context.
119      *
120      * @param beanClass        Class of the destination bean
121      * @param destinationFields Field of the destination field in the destination bean
122      * @param message          Human-readable error text
123      * @since 4.2
124      */
125     public CsvRequiredFieldEmptyException(Class<?> beanClass, List<Field> destinationFields, String message) {
126         super(message);
127         this.beanClass = beanClass;
128         this.destinationFields = new UnmodifiableList<>(destinationFields);
129     }
130 
131     /**
132      * Gets the class of the bean to which the value was to be assigned.
133      *
134      * @return The class of the bean to which the destination field belongs
135      */
136     public Class<?> getBeanClass() {
137         return beanClass;
138     }
139 
140     /**
141      * Gets the field from the Reflection API that was to be assigned.
142      * <p>This method is provided as a convenience for when you know there can
143      * be only one field, or you really only care about the first field.</p>
144      * <p>{@code destinationFields} is marked {@code transient}, because
145      * {@link java.lang.reflect.Field} is not {@link java.io.Serializable}. If
146      * for any reason this exception is serialized and deserialized, this method
147      * will subsequently return {@code null}.</p>
148      *
149      * @return The first destination field that was to receive the empty value
150      */
151     public Field getDestinationField() {
152         return CollectionUtils.isEmpty(destinationFields)?null:destinationFields.get(0);
153     }
154     
155     /**
156      * Returns the complete list of all fields that were to be empty.
157      * {@code destinationFields} is marked {@code transient}, because
158      * {@link java.lang.reflect.Field} is not {@link java.io.Serializable}. If
159      * for any reason this exception is serialized and deserialized, this method
160      * will subsequently return {@code null}.
161      *
162      * @return All destination fields that were to receive the empty value
163      * @since 4.2
164      */
165     public List<Field> getDestinationFields() {
166         return destinationFields;
167     }
168 }