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.EOFException;
35  import java.util.Iterator;
36  
37  /**
38   *
39   * @author Zoltan Farkas
40   */
41  class IterableCsvReader implements CsvReader {
42  
43    private CsvReader.TokenType currentToken;
44    private CharSequence current;
45    private final Iterator<? extends CharSequence> it;
46    private boolean finished;
47  
48    IterableCsvReader(final Iterator<? extends CharSequence> it) {
49      this.it = it;
50      current = null;
51      finished = false;
52      currentToken = TokenType.START_DOCUMENT;
53    }
54  
55    @Override
56    public CsvReader.TokenType next() throws EOFException {
57      if (finished) {
58        currentToken = CsvReader.TokenType.END_DOCUMENT;
59        return currentToken;
60      }
61      if (it.hasNext()) {
62        current = it.next();
63        currentToken = CsvReader.TokenType.ELEMENT;
64      } else {
65        finished = true;
66        currentToken = CsvReader.TokenType.END_ROW;
67      }
68      return currentToken;
69    }
70  
71    @Override
72    public CsvReader.TokenType current() {
73      return currentToken;
74    }
75  
76    @Override
77    public CharSequence getElement() {
78      if (currentToken != TokenType.ELEMENT) {
79        throw new IllegalStateException("Not at a element " + currentToken);
80      }
81      return current;
82    }
83  
84    @Override
85    public String toString() {
86      return "IterableCsvReader{" + "current=" + current + ", it=" + it + ", finished=" + finished + '}';
87    }
88  
89    @Override
90    public long currentLineNumber() {
91      return 0;
92    }
93  
94  }