com.helger.commons.csv.CSVIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ph-commons Show documentation
Show all versions of ph-commons Show documentation
Java 1.8+ Library with tons of utility classes required in all projects
/*
* Copyright (C) 2014-2024 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.helger.commons.csv;
import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.annotation.Nonnull;
import com.helger.commons.ValueEnforcer;
import com.helger.commons.collection.impl.ICommonsList;
/**
* Provides an Iterator over the data found in opencsv.
*
* @author OpenCSV
* @author Philip Helger
*/
public class CSVIterator implements Iterator >
{
private final CSVReader m_aReader;
private ICommonsList m_aNextLine;
/**
* @param aReader
* reader for the csv data. May not be null
.
* @throws IOException
* if unable to read data from the reader.
*/
public CSVIterator (@Nonnull final CSVReader aReader) throws IOException
{
ValueEnforcer.notNull (aReader, "Reader");
m_aReader = aReader;
m_aNextLine = aReader.readNext ();
}
/**
* Returns true
if the iteration has more elements. In other
* words, returns true
if next() would return an element rather
* than throwing an exception.
*
* @return true
if this {@link CSVIterator} has more elements.
*/
public boolean hasNext ()
{
return m_aNextLine != null;
}
/**
* Returns the next element in the iterator.
*
* @return The next element of the iterator. Never null
.
*/
@Nonnull
public ICommonsList next ()
{
final ICommonsList ret = m_aNextLine;
if (ret == null)
throw new NoSuchElementException ();
try
{
m_aNextLine = m_aReader.readNext ();
}
catch (final IOException ex)
{
throw new IllegalStateException ("Failed to read next CSV line", ex);
}
return ret;
}
}