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.io.UncheckedIOException;
36  import java.util.ArrayList;
37  import java.util.Iterator;
38  import java.util.List;
39  import java.util.NoSuchElementException;
40  
41  final class CsvReader2Iterator implements Iterator<Iterable<String>> {
42  
43    private final CsvReader reader;
44  
45    private final List<String> row;
46  
47    private boolean haveParsedRow;
48  
49    CsvReader2Iterator(final CsvReader preader) {
50      this.reader = preader;
51      haveParsedRow = false;
52      row = new ArrayList<>();
53    }
54  
55    private CsvReader.TokenType readRow() throws IOException, CsvParseException {
56      row.clear();
57      CsvReader.TokenType token;
58      boolean loop = true;
59      do {
60        token = reader.next();
61        switch (token) {
62          case ELEMENT:
63            row.add(reader.getElement().toString());
64            break;
65          case END_DOCUMENT:
66          case END_ROW:
67            loop = false;
68            break;
69          default:
70            throw new IllegalStateException("Illegal token " + token);
71        }
72      } while (loop);
73      haveParsedRow = !row.isEmpty();
74      return token;
75    }
76  
77    @Override
78    public boolean hasNext() {
79      if (!haveParsedRow) {
80        try {
81          CsvReader.TokenType token = readRow();
82          if (haveParsedRow) {
83            return true;
84          } else {
85            return token != CsvReader.TokenType.END_DOCUMENT;
86          }
87        } catch (IOException ex) {
88          throw new UncheckedIOException(ex);
89        } catch (CsvParseException ex) {
90          throw new UncheckedCsvParseException(ex);
91        }
92      } else {
93        return true;
94      }
95    }
96  
97    @Override
98    public Iterable<String> next() {
99      if (!hasNext()) {
100       throw new NoSuchElementException();
101     } else {
102       haveParsedRow = false;
103       return row;
104     }
105   }
106 
107   @Override
108   public String toString() {
109     return "CsvReader2Iterator{" + "reader=" + reader + ", row=" + row + ", haveParsedRow=" + haveParsedRow + '}';
110   }
111 
112 }