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  import java.io.Reader;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.supercsv.cellprocessor.ift.CellProcessor;
24  import org.supercsv.prefs.CsvPreference;
25  
26  /**
27   * CsvListReader is a simple reader that reads a row from a CSV file into a <tt>List</tt> of Strings.
28   * 
29   * @author Kasper B. Graversen
30   * @author James Bassett
31   */
32  public class CsvListReader extends AbstractCsvReader implements ICsvListReader {
33  	
34  	/**
35  	 * Constructs a new <tt>CsvListReader</tt> with the supplied Reader and CSV preferences. Note that the
36  	 * <tt>reader</tt> will be wrapped in a <tt>BufferedReader</tt> before accessed.
37  	 * 
38  	 * @param reader
39  	 *            the reader
40  	 * @param preferences
41  	 *            the CSV preferences
42  	 * @throws NullPointerException
43  	 *             if reader or preferences are null
44  	 */
45  	public CsvListReader(final Reader reader, final CsvPreference preferences) {
46  		super(reader, preferences);
47  	}
48  	
49  	/**
50  	 * Constructs a new <tt>CsvListReader</tt> with the supplied (custom) Tokenizer and CSV preferences. The tokenizer
51  	 * should be set up with the Reader (CSV input) and CsvPreference beforehand.
52  	 * 
53  	 * @param tokenizer
54  	 *            the tokenizer
55  	 * @param preferences
56  	 *            the CSV preferences
57  	 * @throws NullPointerException
58  	 *             if tokenizer or preferences are null
59  	 */
60  	public CsvListReader(final ITokenizer tokenizer, final CsvPreference preferences) {
61  		super(tokenizer, preferences);
62  	}
63  	
64  	/**
65  	 * {@inheritDoc}
66  	 */
67  	public List<String> read() throws IOException {
68  		
69  		if( readRow() ) {
70  			return new ArrayList<String>(getColumns());
71  		}
72  		
73  		return null; // EOF
74  	}
75  	
76  	/**
77  	 * {@inheritDoc}
78  	 */
79  	public List<Object> read(final CellProcessor... processors) throws IOException {
80  		
81  		if( processors == null ) {
82  			throw new NullPointerException("processors should not be null");
83  		}
84  		
85  		if( readRow() ) {
86  			return executeProcessors(processors);
87  		}
88  		
89  		return null; // EOF
90  	}
91  	
92  	/**
93  	 * {@inheritDoc}
94  	 */
95  	public List<Object> executeProcessors(final CellProcessor... processors) {
96  		return super.executeProcessors(new ArrayList<Object>(getColumns().size()), processors);
97  	}
98  }