/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
 */
package io.jenetics.engine;

import static java.util.Objects.requireNonNull;

import java.util.function.Predicate;

import io.jenetics.Gene;
import io.jenetics.Genotype;
import io.jenetics.Phenotype;
import io.jenetics.util.Factory;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
 * @version !__version__!
 * @since !__version__!
 */
final class RetryConstraint<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
>
	implements Constraint<G, C>
{

	static final int DEFAULT_RETRY_COUNT = 15;

	private final Predicate<? super Phenotype<G, C>> _validator;
	private final Factory<Genotype<G>> _genotypeFactory;
	private final int _retryLimit;

	RetryConstraint(
		final Predicate<? super Phenotype<G, C>> validator,
		final Factory<Genotype<G>> genotypeFactory,
		final int retryLimit
	) {
		_validator = requireNonNull(validator);
		_genotypeFactory = requireNonNull(genotypeFactory);
		_retryLimit = retryLimit;
	}

	@Override
	public boolean test(final Phenotype<G, C> individual) {
		return _validator.test(individual);
	}

	@Override
	public Phenotype<G, C> repair(final Phenotype<G, C> individual) {
		final long generation = individual.getGeneration();

		int count = 0;
		Phenotype<G, C> phenotype;
		do {
			phenotype = Phenotype.of(_genotypeFactory.newInstance(), generation);
		} while (++count < _retryLimit && !test(phenotype));

		return phenotype;
	}
}

@@ -975,16 +975,173 @@
 y;%0A%0A/**%0A
+ * This simple %7B@code Constraint%7D implementation %3Cem%3Erepairs%3C/em%3E an invalid%0A * phenotype by creating new individuals until a valid one has been created.%0A *%0A
  * @auth
@@ -1217,16 +1217,16 @@
 ter%3C/a%3E%0A
-
  * @vers
@@ -1271,16 +1271,23 @@
 __!%0A */%0A
+public 
 final cl
@@ -1307,16 +1307,16 @@
 traint%3C%0A
-
 %09G exten
@@ -1398,16 +1398,97 @@
  C%3E%0A%7B%0A%0A%09
+/**%0A%09 * The default retry-count for creating new, valid phenotypes.%0A%09 */%0A%09public 
 static f
@@ -1672,17 +1672,604 @@
 Limit;%0A%0A
-
 %09
+/**%0A%09 * Create a new retry-constraint with the given parameters.%0A%09 *%0A%09 * @param validator the phenotype validator%0A%09 * @param genotypeFactory the genotype factory used for creating new%0A%09 *        phenotypes%0A%09 * @param retryLimit the limit of the phenotype creation retries. If more%0A%09 *        re-creation tries are necessary, an invalid phenotype is returned.%0A%09 *        This limit guarantees the termination of the%0A%09 *        %7B@link #repair(Phenotype)%7D method.%0A%09 * @throws NullPointerException if the %7B@code validator%7D or%0A%09 *         %7B@code genotypeFactory%7D is %7B@code null%7D%0A%09 */%0A%09public 
 RetryCon

Add Javadoc.