/*
 * 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.collect.Iterators;
import com.google.common.util.concurrent.ValueFuture;

import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;

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

/**
 * Parallel equivalent of {@link Iterators}.
 * 
 * @author Herve Quiroz
 */
public final class ParallelIterators
{
    private static Future<?> LAST = ValueFuture.create();

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

    /**
     * @see Iterators#transform(Iterator, Function)
     */
    public static <F, T> Iterator<T> transform(final Iterator<F> fromIterable,
            final Function<? super F, ? extends T> function, final ExecutorService executor)
    {
        final Function<F, Callable<T>> applyFunction = ParallelFunctions.apply(function);
        final Iterator<Callable<T>> callables = Iterators.transform(fromIterable, applyFunction);

        final BlockingQueue<Future<T>> futures = new LinkedBlockingQueue<Future<T>>();
        final Function<Callable<T>, Future<T>> submitFunction = new Function<Callable<T>, Future<T>>()
        {
            @Override
            public Future<T> apply(final Callable<T> task)
            {
                return executor.submit(new Callable<T>()
                {
                    @Override
                    public T call() throws Exception
                    {
                        try
                        {
                            return task.call();
                        }
                        catch (final Exception e)
                        {
                            TcFutures.cancel(futures);
                            throw e;
                        }
                    }
                });
            }
        };

        executor.submit(new Runnable()
        {
            @Override
            public void run()
            {
                Iterators.addAll(futures, Iterators.transform(callables, submitFunction));
                final Future<T> last = last();
                futures.add(last);
            }
        });

        return getUntilLast(TcIterators.removeAll(futures));
    }

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

    private ParallelIterators()
    {
        // No instantiation
    }
}

@@ -855,16 +855,58 @@
 nction;%0A
+import com.google.common.base.Predicates;%0A
 import c
@@ -1023,16 +1023,64 @@
 erator;%0A
+import java.util.concurrent.ArrayBlockingQueue;%0A
 import j
@@ -1236,57 +1236,8 @@
 ure;
-%0Aimport java.util.concurrent.LinkedBlockingQueue;
 %0A%0Aim
@@ -1446,16 +1446,92 @@
 ators%0A%7B%0A
+    private static final int MAXIMUM_NUMBER_OF_ELEMENTS_IN_ADVANCE = 1024;%0A%0A
     priv
@@ -2235,14 +2235,13 @@
 new 
-Linked
+Array
 Bloc
@@ -2261,16 +2261,70 @@
 ure%3CT%3E%3E(
+%0A                MAXIMUM_NUMBER_OF_ELEMENTS_IN_ADVANCE
 );%0A     
@@ -3103,16 +3103,45 @@
 %0A       
+ final Future%3CT%3E submitTask =
  executo
@@ -3157,16 +3157,19 @@
 new 
-Runn
+Call
 able
+%3CT%3E
 ()%0A 
@@ -3222,16 +3222,14 @@
 lic 
-void run
+T call
 ()%0A 
@@ -3426,27 +3426,89 @@
         
-%7D%0A        %7D
+    return null;%0A            %7D%0A        %7D);%0A        futures.add(submitTask
 );%0A%0A    
@@ -3531,16 +3531,33 @@
 tilLast(
+Iterators.filter(
 TcIterat
@@ -3578,16 +3578,87 @@
 futures)
+,%0A                Predicates.not(TcPredicates.identicalTo(submitTask)))
 );%0A    %7D

Use an ArrayBlockingQueue to store futures in order to be able to set an upper-bound limit of elements that may be computed in advance at any given time