View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
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 org.supercsv.io;
17  
18  import java.io.IOException;
19  
20  import org.supercsv.cellprocessor.ift.CellProcessor;
21  import org.supercsv.exception.SuperCsvConstraintViolationException;
22  import org.supercsv.exception.SuperCsvException;
23  import org.supercsv.exception.SuperCsvReflectionException;
24  
25  /**
26   * Interface for CSV readers reading into objects/beans.
27   * 
28   * @author Kasper B. Graversen
29   * @author James Bassett
30   */
31  public interface ICsvBeanReader extends ICsvReader {
32  	
33  	/**
34  	 * Reads a row of a CSV file and populates an instance of the specified class, using the supplied name mapping to
35  	 * map column values to the appropriate fields.
36  	 * 
37  	 * @param clazz
38  	 *            the type to instantiate. If the type is a class then a new instance will be created using the default
39  	 *            no-args constructor. If the type is an interface, a proxy object which implements the interface will
40  	 *            be created instead.
41  	 * @param nameMapping
42  	 *            an array of Strings linking the CSV columns to their corresponding field in the bean (the array length
43  	 *            should match the number of columns). A <tt>null</tt> entry in the array indicates that the column
44  	 *            should be ignored (the field in the bean will be null - or its default value).
45  	 * @param <T>
46  	 *            the bean type
47  	 * @return a populated bean or null if EOF
48  	 * @throws IOException
49  	 *             if an I/O error occurred
50  	 * @throws IllegalArgumentException
51  	 *             if nameMapping.length != number of columns read
52  	 * @throws NullPointerException
53  	 *             if clazz or nameMapping are null
54  	 * @throws SuperCsvException
55  	 *             if there was a general exception while reading/processing
56  	 * @throws SuperCsvReflectionException
57  	 *             if there was an reflection exception while mapping the values to the bean
58  	 * @since 1.0
59  	 */
60  	<T> T read(Class<T> clazz, String... nameMapping) throws IOException;
61  	
62  	/**
63  	 * Reads a row of a CSV file and populates the bean, using the supplied name mapping to map column values to the
64  	 * appropriate fields.
65  	 * 
66  	 * @param bean
67  	 *            the bean to populate
68  	 * @param nameMapping
69  	 *            an array of Strings linking the CSV columns to their corresponding field in the bean (the array length
70  	 *            should match the number of columns). A <tt>null</tt> entry in the array indicates that the column
71  	 *            should be ignored (the field in the bean will be null - or its default value).
72  	 * @param <T>
73  	 *            the bean type
74  	 * @return a populated bean or null if EOF
75  	 * @throws IOException
76  	 *             if an I/O error occurred
77  	 * @throws IllegalArgumentException
78  	 *             if nameMapping.length != number of columns read
79  	 * @throws NullPointerException
80  	 *             if bean or nameMapping are null
81  	 * @throws SuperCsvException
82  	 *             if there was a general exception while reading/processing
83  	 * @throws SuperCsvReflectionException
84  	 *             if there was an reflection exception while mapping the values to the bean
85  	 * @since 2.2.0
86  	 */
87  	<T> T read(T bean, String... nameMapping) throws IOException;
88  	
89  	/**
90  	 * Reads a row of a CSV file and populates an instance of the specified class, using the supplied name mapping to
91  	 * map column values to the appropriate fields. Before population the data can be further processed by cell
92  	 * processors (as with the nameMapping array, each element in the processors array corresponds with a CSV column). A
93  	 * <tt>null</tt> entry in the processors array indicates no further processing is required (the unprocessed String
94  	 * value will be set on the bean's field).
95  	 * 
96  	 * @param clazz
97  	 *            the type to instantiate. If the type is a class then a new instance will be created using the default
98  	 *            no-args constructor. If the type is an interface, a proxy object which implements the interface will
99  	 *            be created instead.
100 	 * @param nameMapping
101 	 *            an array of Strings linking the CSV columns to their corresponding field in the bean (the array length
102 	 *            should match the number of columns). A <tt>null</tt> entry in the array indicates that the column
103 	 *            should be ignored (the field in the bean will be null - or its default value).
104 	 * @param processors
105 	 *            an array of CellProcessors used to further process data before it is populated on the bean (each
106 	 *            element in the processors array corresponds with a CSV column - the number of processors should match
107 	 *            the number of columns). A <tt>null</tt> entry indicates no further processing is required (the
108 	 *            unprocessed String value will be set on the bean's field).
109 	 * @param <T>
110 	 *            the bean type
111 	 * @return a populated bean or null if EOF
112 	 * @throws IOException
113 	 *             if an I/O error occurred
114 	 * @throws NullPointerException
115 	 *             if clazz, nameMapping, or processors are null
116 	 * @throws SuperCsvConstraintViolationException
117 	 *             if a CellProcessor constraint failed
118 	 * @throws SuperCsvException
119 	 *             if there was a general exception while reading/processing
120 	 * @throws SuperCsvReflectionException
121 	 *             if there was an reflection exception while mapping the values to the bean
122 	 * @since 1.0
123 	 */
124 	<T> T read(Class<T> clazz, String[] nameMapping, CellProcessor... processors) throws IOException;
125 	
126 	/**
127 	 * Reads a row of a CSV file and populates the bean, using the supplied name mapping to map column values to the
128 	 * appropriate fields. Before population the data can be further processed by cell processors (as with the
129 	 * nameMapping array, each element in the processors array corresponds with a CSV column). A <tt>null</tt> entry in
130 	 * the processors array indicates no further processing is required (the unprocessed String value will be set on the
131 	 * bean's field).
132 	 * 
133 	 * @param bean
134 	 *            the bean to populate
135 	 * @param nameMapping
136 	 *            an array of Strings linking the CSV columns to their corresponding field in the bean (the array length
137 	 *            should match the number of columns). A <tt>null</tt> entry in the array indicates that the column
138 	 *            should be ignored (the field in the bean will be null - or its default value).
139 	 * @param processors
140 	 *            an array of CellProcessors used to further process data before it is populated on the bean (each
141 	 *            element in the processors array corresponds with a CSV column - the number of processors should match
142 	 *            the number of columns). A <tt>null</tt> entry indicates no further processing is required (the
143 	 *            unprocessed String value will be set on the bean's field).
144 	 * @param <T>
145 	 *            the bean type
146 	 * @return a populated bean or null if EOF
147 	 * @throws IOException
148 	 *             if an I/O error occurred
149 	 * @throws NullPointerException
150 	 *             if bean, nameMapping, or processors are null
151 	 * @throws SuperCsvConstraintViolationException
152 	 *             if a CellProcessor constraint failed
153 	 * @throws SuperCsvException
154 	 *             if there was a general exception while reading/processing
155 	 * @throws SuperCsvReflectionException
156 	 *             if there was an reflection exception while mapping the values to the bean
157 	 * @since 2.2.0
158 	 */
159 	<T> T read(T bean, String[] nameMapping, CellProcessor... processors) throws IOException;
160 }