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

org.vertexium.util.ConvertingIterable Maven / Gradle / Ivy

There is a newer version: 4.10.0
Show newest version
package org.vertexium.util;

import java.util.Iterator;

public abstract class ConvertingIterable implements Iterable {
    private Iterable iterable;
    private Iterator iterator;

    public ConvertingIterable(Iterable iterable) {
        this.iterable = iterable;
    }

    public ConvertingIterable(Iterator iterator) {
        this.iterator = iterator;
    }

    @Override
    public Iterator iterator() {
        final Iterator it = iterator == null ? iterable.iterator() : iterator;
        return new CloseableIterator() {
            @Override
            public boolean hasNext() {
                boolean hasNext = it.hasNext();
                if (!hasNext) {
                    close();
                }
                return hasNext;
            }

            @Override
            public TDest next() {
                return convert(it.next());
            }

            @Override
            public void remove() {
                it.remove();
            }

            @Override
            public void close() {
                CloseableUtils.closeQuietly(it);
            }
        };
    }

    protected abstract TDest convert(TSource o);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy