All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.ytsaurus.client.sync.SyncTableReaderImpl Maven / Gradle / Ivy

The newest version!
package tech.ytsaurus.client.sync;

import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.NoSuchElementException;

import tech.ytsaurus.client.AsyncReader;

class SyncTableReaderImpl implements SyncTableReader {
    private final AsyncReader reader;
    private Iterator iterator;
    private boolean noMoreRows;

    private SyncTableReaderImpl(AsyncReader reader) {
        this.reader = reader;
        this.iterator = Collections.emptyIterator();
    }

    static  SyncTableReader wrap(AsyncReader reader) {
        return new SyncTableReaderImpl<>(reader);
    }

    @Override
    public T next() {
        if (hasNext()) {
            return iterator.next();
        }
        throw new NoSuchElementException();
    }

    @Override
    public boolean hasNext() {
        if (noMoreRows) {
            return false;
        }
        if (iterator.hasNext()) {
            return true;
        }

        var rows = reader.next().join();
        if (rows == null || rows.size() == 0) {
            noMoreRows = true;
            return false;
        }

        this.iterator = rows.iterator();
        return true;
    }

    @Override
    public void close() throws IOException {
        reader.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy