View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.io.csv;
33  
34  import java.io.IOException;
35  import java.util.Iterator;
36  import java.util.function.Consumer;
37  import javax.annotation.Nonnull;
38  
39  /**
40   * Char separated value file Reader.
41   * A newly created reader will initially be positioned at START_DOCUMENT. (current())
42   * A example Token sequence will be:
43   * START_DOCUMENT, ELEMENT, ELEMENT, END_ROW, ELEMENT, ELEMENT, END_ROW, END_DOCUMENT
44   *
45   * @author zoly
46   */
47  public interface CsvReader {
48  
49    enum TokenType {
50       START_DOCUMENT, ELEMENT, END_ROW, END_DOCUMENT
51    }
52  
53    /**
54     * read next CSV element, and return its type.
55     *
56     * @return return CSV element type.
57     * @throws IOException exception is something goes wrong.
58     */
59    @Nonnull
60    TokenType next() throws IOException, CsvParseException;
61  
62    /**
63     * @return the currently parsed token type,  null if no current token is available (next has never been called)
64     * @throws IOException
65     * @throws CsvParseException
66     */
67    @Nonnull
68    TokenType current();
69  
70    /**
71     * the CSV element string. the underlying instance is reused, so you will need to make a copy of this if planning to
72     * use it.
73     *
74     * @return CharSequence representing a csv cell.
75     */
76    CharSequence getElement();
77  
78    /**
79     * Current CSV line number.
80     * @return
81     */
82    long currentLineNumber();
83  
84    default int skipRow() throws IOException, CsvParseException {
85      int skipped = 0;
86      TokenType current = current();
87      if (current == TokenType.START_DOCUMENT) { // beginning of file.
88        next();
89      }
90      while ((current = current()) != CsvReader.TokenType.END_ROW
91              && current != CsvReader.TokenType.END_DOCUMENT) {
92        next();
93        skipped++;
94      }
95      if (current != CsvReader.TokenType.END_DOCUMENT) {
96        next();
97      }
98      return skipped;
99    }
100 
101   default void readRow(final Consumer<CharSequence> consumer) throws IOException, CsvParseException {
102     TokenType current = current();
103     if (current == TokenType.START_DOCUMENT) { // beginning of file.
104       next();
105     }
106     while ((current = current()) != CsvReader.TokenType.END_ROW
107             && current != CsvReader.TokenType.END_DOCUMENT) {
108       consumer.accept(getElement());
109       next();
110     }
111     if (current != CsvReader.TokenType.END_DOCUMENT) {
112       next();
113     }
114   }
115 
116 
117   static CsvReader toReader(final Iterator<? extends CharSequence> it) {
118     return new IterableCsvReader(it);
119   }
120 }