/*
 * Copyright 2017 Yoshiyuki Takemori
 * 
 * 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.imakaramegane.soniccsv.internal;

import java.util.Iterator;
import java.util.NoSuchElementException;

import com.imakaramegane.soniccsv.CsvReader;
import com.imakaramegane.soniccsv.CsvRecord;

/**
 * @author Yoshiyuki Takemori
 */
final class CsvIterator implements Iterator<CsvRecord> {

    private final CsvReader reader;
    private CsvRecord nextLine;

    CsvIterator( CsvReader reader ) {
        this.reader = reader;
    }

    @Override
    public CsvRecord next() {
        if( !this.hasNext() ) {
            throw new NoSuchElementException();
        }
        CsvRecord line = this.nextLine;
        this.nextLine = null;
        return line;
    }

    @Override
    public boolean hasNext() {
        if( this.nextLine != null ) {
            return true;
        } else {
            this.nextLine = this.reader.readNext();
            return this.nextLine != null;
        }
    }
}
