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

de.tsl2.nano.collection.TransformingIterator Maven / Gradle / Ivy

There is a newer version: 2.5.1
Show newest version
/*
 * File: $HeadURL$
 * Id  : $Id$
 * 
 * created by: Thomas Schneider
 * created on: Jun 25, 2013
 * 
 * Copyright: (c) Thomas Schneider 2013, all rights reserved
 */
package de.tsl2.nano.collection;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Iterator;
import java.util.Queue;

import de.tsl2.nano.core.ITransformer;
import de.tsl2.nano.core.cls.BeanClass;

/**
 * Runtime transforming Iterator. Wraps the Iterator of the given collection. {@link #remove()} is not supported! Please
 * see {@link #getTransformingIterable(Iterable, ITransformer)} to wrap your original collection into a filtered
 * collection.
 * 
 * @author Thomas Schneider
 * @version $Revision$
 */
public class TransformingIterator implements Iterator {
    Iterable parent;
    Queue previewItems;
    Iterator parentIt;
    ITransformer transformer;
    int i = 0;

    /**
     * constructor
     * 
     * @param parent
     * @param transformer
     */
    public TransformingIterator(Iterable parent, ITransformer transformer) {
        super();
        this.parent = parent;
        this.transformer = transformer;
        this.parentIt = parent.iterator();
    }

    @Override
    public boolean hasNext() {
        return parentIt.hasNext();
    }

    @Override
    public T next() {
        return transformer.transform(parentIt.next());
    }

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

    /**
     * creates a proxy using the given iterable instance. if an iterator is requested, the orginal iterator will be
     * wrapped into the {@link TransformingIterator}.
     * 

* As the original instance (the collection) will be preserved, all other methods like size(), contains() etc. will * work on the un-filtered content! * * @param iterable type - mostly at least a collection * @param member type of the iterable * @param iterable iterable instance * @param transformer item filter/selector * @return proxy, providing the given iterable filtered through the given predicate. */ @SuppressWarnings("unchecked") public static , S, T> I getTransformingIterable(final Iterable iterable, final ITransformer transformer) { return (I) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), BeanClass.getBeanClass(iterable.getClass()).getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = method.invoke(iterable, args); if (Iterator.class.isAssignableFrom(method.getReturnType())) { result = new TransformingIterator(iterable, transformer); } return result; } }); } }