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

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

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

/**
 * Parallel equivalent of {@link Iterators}.
 * 
 * @author Herve Quiroz
 */
public final class ParallelIterators
{
    private static final int MAXIMUM_NUMBER_OF_ELEMENTS_IN_ADVANCE = 1024;

    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 ArrayBlockingQueue<Future<T>>(
                MAXIMUM_NUMBER_OF_ELEMENTS_IN_ADVANCE);
        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;
                        }
                    }
                });
            }
        };

        final Future<T> submitTask = executor.submit(new Callable<T>()
        {
            @Override
            public T call()
            {
                Iterators.addAll(futures, Iterators.transform(callables, submitFunction));
                final Future<T> last = last();
                futures.add(last);
                return null;
            }
        });
        futures.add(submitTask);

        return getUntilLast(Iterators.filter(TcIterators.removeAll(futures),
                Predicates.not(TcPredicates.identicalTo(submitTask))));
    }

    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
    }
}

@@ -859,50 +859,8 @@
 on;%0A
-import com.google.common.base.Predicates;%0A
 impo
@@ -1186,24 +1186,76 @@
 rent.Future;
+%0Aimport java.util.concurrent.atomic.AtomicReference;
 %0A%0Aimport org
@@ -2184,24 +2184,113 @@
 Function);%0A%0A
+        final AtomicReference%3CFuture%3C?%3E%3E submitTask = new AtomicReference%3CFuture%3C?%3E%3E();%0A%0A
         fina
@@ -3041,34 +3041,343 @@
     
-TcFutures.cancel(futures);
+final Future%3C?%3E taskToCancel = submitTask.get();%0A                            if (taskToCancel != null)%0A                            %7B%0A                                taskToCancel.cancel(true);%0A                                final Future%3CT%3E last = last();%0A                                futures.add(last);%0A                            %7D
 %0A   
@@ -3511,32 +3511,16 @@
 %0A       
- final Future%3CT%3E
  submitT
@@ -3522,19 +3522,21 @@
 bmitTask
- = 
+.set(
 executor
@@ -3543,35 +3543,32 @@
 .submit(new 
-Call
+Runn
 able
-%3CT%3E
 ()%0A        %7B
@@ -3609,22 +3609,24 @@
  public 
-T call
+void run
 ()%0A     
@@ -3823,81 +3823,20 @@
     
-    return null;%0A            %7D%0A        %7D);%0A        futures.add(submitTask
+%7D%0A        %7D)
 );%0A%0A
@@ -3867,25 +3867,8 @@
 ast(
-Iterators.filter(
 TcIt
@@ -3897,79 +3897,8 @@
 res)
-,%0A                Predicates.not(TcPredicates.identicalTo(submitTask)))
 );%0A 

Fix canceling of tasks in case an error occurs