View Javadoc
1   /*
2    * Copyright 2017 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.bean;
17  
18  import com.opencsv.exceptions.CsvConstraintViolationException;
19  import com.opencsv.exceptions.CsvDataTypeMismatchException;
20  
21  import java.util.Locale;
22  
23  /**
24   * Classes implementing this interface perform a conversion from String to
25   * some type on reading and some type to String on writing.
26   * <p>This interface is used by BeanField to perform the actual data conversion.</p>
27   * <p><b><i>Synchronization:</i></b> All implementations of this interface must
28   * be thread-safe.</p>
29   *
30   * @author Andrew Rucker Jones
31   * @since 4.2
32   */
33  public interface CsvConverter {
34      /**
35       * Method for converting from a string to the proper data type of the
36       * destination field.
37       *
38       * @param value The string from the selected field of the CSV file. If the
39       *   field is marked as required in the annotation, this value is guaranteed
40       *   not to be {@code null}, empty or blank according to
41       *   {@link org.apache.commons.lang3.StringUtils#isBlank(java.lang.CharSequence)}
42       * @return An {@link java.lang.Object} representing the input data converted
43       *   into the proper type
44       * @throws CsvDataTypeMismatchException    If the input string cannot be converted into
45       *                                         the proper type
46       * @throws CsvConstraintViolationException When the internal structure of
47       *                                         data would be violated by the data in the CSV file
48       */
49      Object convertToRead(String value)
50              throws CsvDataTypeMismatchException, CsvConstraintViolationException;
51      
52      /**
53       * Method for converting from the data type of the destination field to a
54       * string.
55       * 
56       * @param value The contents of the field currently being processed from the
57       *   bean to be written. Can be {@code null} if the field is not marked as
58       *   required.
59       * @return A string representation of the value of the field in question in
60       *   the bean passed in, or an empty string if {@code value} is
61       *   {@code null}
62       * @throws CsvDataTypeMismatchException If the input cannot be converted to
63       *   a string by this converter
64       */
65      String convertToWrite(Object value)
66              throws CsvDataTypeMismatchException;
67      
68      /**
69       * Sets the locale for all error messages.
70       * @param errorLocale Locale for error messages. If {@code null}, the
71       *   default locale is used.
72       */
73      void setErrorLocale(Locale errorLocale);
74  
75      /**
76       * Sets the class of the type of the data being processed.
77       *
78       * @param type The type of the data being processed
79       * @since 4.3
80       */
81      void setType(Class<?> type);
82  
83      /**
84       * If not null or empty, specifies the locale used for converting
85       * locale-specific data types for reading.
86       *
87       * @param locale The name of the locale for locale-sensitive data
88       * @since 4.3
89       */
90      void setLocale(String locale);
91  
92      /**
93       * If not null or empty, specifies the locale used for converting
94       * locale-specific data types for writing.
95       *
96       * @param writeLocale The name of the locale for locale-sensitive data
97       * @since 5.0
98       */
99      void setWriteLocale(String writeLocale);
100 }