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.Iterator;
37  import java.util.NoSuchElementException;
38  
39  /**
40   * @author Zoltan Farkas
41   */
42  public final class OneRowIterator implements Iterator<CharSequence> {
43  
44    private final CsvReader reader;
45    private CsvReader.TokenType next;
46  
47    public OneRowIterator(final CsvReader reader) {
48      this.reader = reader;
49    }
50  
51    private CsvReader.TokenType nextToken() {
52      try {
53        if (next == null) {
54          next = reader.next();
55        }
56        return next;
57      } catch (IOException ex) {
58        throw new UncheckedIOException(ex);
59      } catch (CsvParseException ex) {
60        throw new UncheckedCsvParseException(ex);
61      }
62    }
63  
64    @Override
65    public boolean hasNext() {
66      return nextToken() == CsvReader.TokenType.ELEMENT;
67    }
68  
69    @Override
70    public CharSequence next() {
71      if (hasNext()) {
72        next = null;
73        return reader.getElement();
74      } else {
75        throw new NoSuchElementException();
76      }
77    }
78  
79    @Override
80    public String toString() {
81      return "OneRowIterator{" + "reader=" + reader + ", next=" + next + '}';
82    }
83  
84  }