/*
 * Java Genetic Algorithm Library (@!identifier!@).
 * Copyright (c) @!year!@ Franz Wilhelmstötter
 *  
 * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 *     Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 *     
 */
package org.jenetics.util;

import static org.jenetics.util.object.nonNull;

import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;

import javolution.context.ConcurrentContext;
import javolution.context.ObjectFactory;
import javolution.util.FastList;

/**
 * Since the parallelization of the library is build on the {@link ConcurrentContext}
 * of the <a href="http://javolution.org/">Javolution</a> project, this class 
 * allows you to share a common {@link ForkJoinPool} for the GA and the rest of 
 * your application. 
 * 
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version $Id$
 */
public final class ForkJoinContext extends ConcurrentContext {

	private static final long serialVersionUID = 1L;

	private final static AtomicReference<ForkJoinPool> _POOL = new AtomicReference<>();
	
	private final FastList<Future<?>> _futures = new FastList<>(10);
	
	ForkJoinContext() {
	}
	
	/**
	 * Set the fork-join-pool used by this context from outside. If not set, a
	 * new one is created.
	 * 
	 * @param pool the fork-join-pool to use.
	 * @throws NullPointerException if the pool is {@code null}.
	 */
	public static void setForkkJoinPool(final ForkJoinPool pool) {
		_POOL.compareAndSet(null, nonNull(pool, "ForkJoinPool"));
	}
	
	/**
	 * Return the current fork-join-pool used by this context.
	 * 
	 * @return the current fork-join-pool used by this context. Can be 
	 *         {@code null}.
	 */
	public static ForkJoinPool getForkJoinPool() {
		return _POOL.get();
	}
	
	@Override
	protected void enterAction() {
		if (_POOL.get() == null) {
			throw new IllegalStateException("No ForkJoinPool set.");
		}
		_futures.clear();
	}
	
	@Override
	protected void executeAction(final Runnable logic) {
		_futures.add(_POOL.get().submit(logic));
	}



	@Override
	protected void exitAction() {
		try {
			for (FastList.Node<Future<?>> n = _futures.head(), 
				end = _futures.tail(); (n = n.getNext()) != end;) 
			{
				n.getValue().get();
			}
		} catch (InterruptedException e) {
			throw (CancellationException)new CancellationException().initCause(e);
		} catch (ExecutionException e) {
			throw (CancellationException)new CancellationException().initCause(e);
		}
	}

	static {
		ObjectFactory.setInstance(
			new ObjectFactory<ForkJoinContext>() {
				@Override protected ForkJoinContext create() {
					return new ForkJoinContext();
				}
			}, 
			ForkJoinContext.class
		);
	}
	
}





@@ -2082,105 +2082,296 @@
 text
- from outside. If not set, a%0A%09 * new one is created.%0A%09 * %0A%09 * @param pool the fork-join-pool to u
+. This method doesn't replace%0A%09 * an already set %7B@link ForkJoinPool%7D. Before the %3Ci%3Econtext%3C/i%3E can be%0A%09 * used a %7B@link ForkJoinPool%7D must be set.%0A%09 * %0A%09 * @param pool the fork-join-pool to use.%0A%09 * @return %7B@code true%7D if the given pool has been set, %7B@code false%7D %0A%09 *         otherwi
 se.%0A
@@ -2452,20 +2452,23 @@
  static 
-void
+boolean
  setFork
@@ -2506,16 +2506,23 @@
 ol) %7B%0A%09%09
+return 
 _POOL.co
@@ -2731,32 +2731,47 @@
     %7B@code null%7D
+ if not set jet
 .%0A%09 */%0A%09public s

Add javadoc.