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

org.leialearns.utilities.TransformingIterable Maven / Gradle / Ivy

package org.leialearns.utilities;

import java.util.Iterator;

/**
 * Produces iterators that transform each element returned by an iterator of a backing iterable.
 * @param  The type of the transformed elements
 */
public class TransformingIterable extends TypedIterable {

    /**
     * Creates a new TransformingIterable instance.
     * @param iterable The backing iterable
     * @param type The type of the transformed elements
     * @param function The function that is used to transform each element
     */
    public TransformingIterable(Iterable iterable, Class type, Function function) {
        super(getIterable(iterable, function), type);
    }

    protected static  Iterable getIterable(final Iterable iterable, final Function transformation) {
        return new Iterable() {
            @Override
            public Iterator iterator() {
                final Iterator delegate = iterable.iterator();
                return new Iterator() {
                    @Override
                    public boolean hasNext() {
                        return delegate.hasNext();
                    }

                    @Override
                    public T next() {
                        return transformation.get(delegate.next());
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy