/*
 * Copyright (C) 2010 TranceCode Software
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package org.trancecode.parallel;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.ValueFuture;

import java.util.Queue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;

import org.trancecode.collection.TcIterables;
import org.trancecode.function.TcPredicates;

/**
 * @author Herve Quiroz
 */
public final class ParallelIterables
{
    private static Future<?> LAST = ValueFuture.create();

    private static <T> Future<T> last()
    {
        @SuppressWarnings("unchecked")
        final Future<T> last = (Future<T>) LAST;
        return last;
    }

    /**
     * @see Iterables#transform(Iterable, Function)
     */
    public static <F, T> Iterable<T> transform(final Iterable<F> fromIterable,
            final Function<? super F, ? extends T> function, final ExecutorService executor)
    {
        // TODO the logic should probably be moved to some Iterator instead

        final Function<F, Callable<T>> applyFunction = ParallelFunctions.apply(function);
        final Iterable<Callable<T>> callables = Iterables.transform(fromIterable, applyFunction);

        final Queue<Future<T>> futures = new LinkedBlockingQueue<Future<T>>();
        final Function<Callable<T>, Future<T>> submitFunction = ParallelFunctions.submit(executor);
        Iterables.addAll(futures, Iterables.transform(callables, submitFunction));
        final Future<T> last = last();
        futures.add(last);

        return getUntilLast(futures);
    }

    /**
     * @see Iterables#filter(Iterable, Predicate)
     */
    public static <T> Iterable<T> filter(final Iterable<T> unfiltered, final Predicate<? super T> predicate,
            final ExecutorService executorService)
    {
        // TODO
        return Iterables.filter(unfiltered, predicate);
    }

    private static <T> Iterable<T> getUntilLast(final Iterable<Future<T>> futures)
    {
        final Future<T> last = last();
        final Iterable<Future<T>> futuresUntilLast = TcIterables.until(futures, TcPredicates.identicalTo(last));
        final Function<Future<T>, T> function = ParallelFunctions.get();
        return Iterables.transform(futuresUntilLast, function);
    }

    public static <T> Iterable<T> get(final Iterable<Future<T>> futures)
    {
        final Function<Future<T>, T> function = ParallelFunctions.get();
        return Iterables.transform(futures, function);
    }

    private ParallelIterables()
    {
        // No instantiation
    }
}

@@ -944,2212 +944,1035 @@
 es;%0A
-import com.google.common.util.concurrent.ValueFuture;%0A%0Aimport java.util.Queue;%0Aimport java.util.concurrent.Callable;%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Future;%0Aimport java.util.concurrent.LinkedBlockingQueue;%0A%0Aimport org.trancecode.collection.TcIterables;%0Aimport org.trancecode.function.TcPredicates;%0A%0A/**%0A * @author Herve Quiroz%0A */%0Apublic final class ParallelIterables%0A%7B%0A    private static Future%3C?%3E LAST = ValueFuture.create();%0A%0A    private static %3CT%3E Future%3CT%3E last()%0A    %7B%0A        @SuppressWarnings(%22unchecked%22)%0A        final Future%3CT%3E last = (Future%3CT%3E) LAST;%0A        return last;%0A    %7D%0A%0A    /**%0A     * @see Iterables#transform(Iterable, Function)%0A     */%0A    public static %3CF, T%3E Iterable%3CT%3E transform(final Iterable%3CF%3E fromIterable,%0A            final Function%3C? super F, ? extends T%3E function, final ExecutorService executor)%0A    %7B%0A        // TODO the logic should probably be moved to some Iterator instead%0A%0A        final Function%3CF, Callable%3CT%3E%3E applyFunction = ParallelFunctions.apply(function);%0A        final Iterable%3CCallable%3CT%3E%3E callables = Iterables.transform(fromIterable, applyFunction);%0A%0A        final Queue%3CFuture%3CT%3E%3E futures = new LinkedBlockingQueue%3CFuture%3CT%3E%3E();%0A        final Function%3CCallable%3CT%3E, Future%3CT%3E%3E submitFunction = ParallelFunctions.submit(executor);%0A        Iterables.addAll(futures, Iterables.transform(callables, submitFunction));%0A        final Future%3CT%3E last = last();%0A        futures.add(last);%0A%0A        return getUntilLast(futures);%0A    %7D%0A%0A    /**%0A     * @see Iterables#filter(Iterable, Predicate)%0A     */%0A    public static %3CT%3E Iterable%3CT%3E filter(final Iterable%3CT%3E unfiltered, final Predicate%3C? super T%3E predicate,%0A            final ExecutorService executorService)%0A    %7B%0A        // TODO%0A        return Iterables.filter(unfiltered, predicate);%0A    %7D%0A%0A    private static %3CT%3E Iterable%3CT%3E getUntilLast(final Iterable%3CFuture%3CT%3E%3E futures)%0A    %7B%0A        final Future%3CT%3E last = last();%0A        final Iterable%3CFuture%3CT%3E%3E futuresUntilLast = TcIterables.until(futures, TcPredicates.identicalTo(last));%0A        final Function%3CFuture%3CT%3E, T%3E function = ParallelFunctions.get();%0A        return Iterables.transform(futuresUntilLast, function
+%0Aimport java.util.Iterator;%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Future;%0A%0A/**%0A * Parallel equivalent of %7B@link Iterables%7D.%0A * %0A * @author Herve Quiroz%0A */%0Apublic final class ParallelIterables%0A%7B%0A    /**%0A     * @see Iterables#transform(Iterable, Function)%0A     */%0A    public static %3CF, T%3E Iterable%3CT%3E transform(final Iterable%3CF%3E fromIterable,%0A            final Function%3C? super F, ? extends T%3E function, final ExecutorService executor)%0A    %7B%0A        return new Iterable%3CT%3E()%0A        %7B%0A            @Override%0A            public Iterator%3CT%3E iterator()%0A            %7B%0A                return ParallelIterators.transform(fromIterable.iterator(), function, executor);%0A            %7D%0A        %7D;%0A    %7D%0A%0A    /**%0A     * @see Iterables#filter(Iterable, Predicate)%0A     */%0A    public static %3CT%3E Iterable%3CT%3E filter(final Iterable%3CT%3E unfiltered, final Predicate%3C? super T%3E predicate,%0A            final ExecutorService executorService)%0A    %7B%0A        // TODO%0A        return Iterables.filter(unfiltered, predicate
 );%0A 

Use ParallelIterators.transform()