/*
 * 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;

import org.jenetics.util.Function;

/**
 * Some default GA termination strategies.
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version $Id$
 */
public final class termination {

	private termination() {
		throw new AssertionError("Don't create an 'Until' instance.");
	}
	
	static class SteadyFitness<C extends Comparable<? super C>>
		implements Function<Statistics<?, C>, Boolean>
	{
		private final int _genenerations;
		
		private C _fitness;
		private int _stableGenerations = 0;
		
		public SteadyFitness(final int generations) {
			_genenerations = generations;
		}
		
		@Override
		public Boolean apply(final Statistics<?, C> statistics) {
			boolean proceed = true;
			
			if (_fitness == null) {
				_fitness = statistics.getBestFitness();
				_stableGenerations = 1;
			} else {
				final Optimize opt = statistics.getOptimize();
				if (opt.compare(_fitness, statistics.getBestFitness()) >= 0) {
					proceed = ++_stableGenerations <= _genenerations;
				} else {
					_fitness = statistics.getBestFitness();
					_stableGenerations = 1;
				}
			}
			
			return proceed ? Boolean.TRUE : Boolean.FALSE;
		}	
	}
	
	/**
	 * Create a <i>terminator</i> which returns {@code false} if the fitness
	 * hasn't improved for a given number of generations.
	 *
	 * @param <C> the fitness type.
	 * @param generation the number of generations the fitness don't have been
	 *        improved.
	 * @return the GA terminator.
	 */
	public static <C extends Comparable<? super C>>
	Function<Statistics<?, C>, Boolean> SteadyFitness(final int generation) {
		return new SteadyFitness<>(generation);
	}
	
	static class Generation implements Function<Statistics<?, ?>, Boolean> {
		private final int _generation;
		
		public Generation(final int generation) {
			_generation = generation;
		}
		
		@Override
		public Boolean apply(final Statistics<?, ?> statistics) {
			return statistics.getGeneration() < _generation ? Boolean.TRUE : Boolean.FALSE;
		}		
	}
	
	/**
	 * Return a <i>termination predicate</i> which returns {@code false} if the
	 * current GA generation is {@code >=} as the given {@code generation}.
	 *
	 * [code]
	 * final GeneticAlgortihm<Float64Gene, Float64> ga = ...
	 * ga.evolve(Until.Generation(100));
	 * [/code]
	 *
	 * @param generation the maximal GA generation.
	 * @return the termination predicate.
	 */
	public static Function<Statistics<?, ?>, Boolean> Generation(final int generation) {
		return new Generation(generation);
	}
	
}

@@ -1231,21 +1231,27 @@
 ate an '
-Until
+termination
 ' instan
@@ -1256,26 +1256,25 @@
 ance.%22);%0A%09%7D%0A
-%09
 %0A
+
 %09static clas
@@ -1410,19 +1410,17 @@
 ations;%0A
-%09%09
 %0A
+
 %09%09privat
@@ -1471,19 +1471,17 @@
 ns = 0;%0A
-%09%09
 %0A
+
 %09%09public
@@ -1553,27 +1553,25 @@
 ations;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09@Override%0A
@@ -1657,20 +1657,17 @@
 = true;%0A
-%09%09%09
 %0A
+
 %09%09%09if (_
@@ -2044,12 +2044,9 @@
 %09%09%7D%0A
-%09%09%09
 %0A
+
 %09%09%09r
@@ -2094,23 +2094,21 @@
 LSE;%0A%09%09%7D
-%09
 %0A%09%7D%0A
-%09
 %0A
+
 %09/**%0A%09 *
@@ -2572,18 +2572,17 @@
 on);%0A%09%7D%0A
-%09
 %0A
+
 %09static 
@@ -2680,19 +2680,17 @@
 ration;%0A
-%09%09
 %0A
+
 %09%09public
@@ -2758,19 +2758,17 @@
 on;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09@Overr
@@ -2921,16 +2921,13 @@
 %0A%09%09%7D
-%09%09
 %0A%09%7D%0A
-%09
 %0A
+
 %09/**
@@ -3423,12 +3423,11 @@
 on);%0A%09%7D%0A
-%09
 %0A%7D%0A

Change exception message.