<commit_before>/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ <NAME>
 *
 * 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:
 *    <NAME> (<EMAIL>)
 */
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;

/**
 * This simple {@code Constraint} implementation <em>repairs</em> an invalid
 * phenotype by creating new individuals until a valid one has been created.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version !__version__!
 * @since !__version__!
 */
public final class RetryConstraint<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
>
	implements Constraint<G, C>
{

	/**
	 * The default retry-count for creating new, valid phenotypes.
	 */
	public 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;

	/**
	 * Create a new retry-constraint with the given parameters.
	 *
	 * @param validator the phenotype validator
	 * @param genotypeFactory the genotype factory used for creating new
	 *        phenotypes
	 * @param retryLimit the limit of the phenotype creation retries. If more
	 *        re-creation tries are necessary, an invalid phenotype is returned.
	 *        This limit guarantees the termination of the
	 *        {@link #repair(Phenotype)} method.
	 * @throws NullPointerException if the {@code validator} or
	 *         {@code genotypeFactory} is {@code null}
	 */
	public 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;
	}
}<commit_msg>#50: Add 'RetryConstraint' factory method.<commit_after>/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ <NAME>
 *
 * 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:
 *    <NAME> (<EMAIL>)
 */
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;

/**
 * This simple {@code Constraint} implementation <em>repairs</em> an invalid
 * phenotype by creating new individuals until a valid one has been created.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version !__version__!
 * @since !__version__!
 */
public final class RetryConstraint<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
>
	implements Constraint<G, C>
{

	/**
	 * The default retry-count for creating new, valid phenotypes.
	 */
	public 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;

	/**
	 * Create a new retry-constraint with the given parameters.
	 *
	 * @param validator the phenotype validator
	 * @param genotypeFactory the genotype factory used for creating new
	 *        phenotypes
	 * @param retryLimit the limit of the phenotype creation retries. If more
	 *        re-creation tries are necessary, an invalid phenotype is returned.
	 *        This limit guarantees the termination of the
	 *        {@link #repair(Phenotype)} method.
	 * @throws NullPointerException if the {@code validator} or
	 *         {@code genotypeFactory} is {@code null}
	 */
	public 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;
	}

	/**
	 * Return a new constraint with the given genotype factory. The phenotype
	 * validator is set to {@link Phenotype#isValid()} and the retry count to
	 * {@link #DEFAULT_RETRY_COUNT}.
	 *
	 * @param genotypeFactory the genotype factory used for creating new
	 *        phenotypes
	 * @param <G> the gene type
	 * @param <C> the fitness value type
	 * @return a new constraint strategy
	 */
	public static <G extends Gene<?, G>, C extends Comparable<? super C>>
	RetryConstraint<G, C> of(final Factory<Genotype<G>> genotypeFactory) {
		return new RetryConstraint<>(
			Phenotype::isValid,
			genotypeFactory,
			DEFAULT_RETRY_COUNT
		);
	}

}
{'fraction_non_alphanumeric': 0.10065880189980082, 'fraction_numerical': 0.0024513559062356366, 'mean_word_length': 4.09204368174727, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2495445', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1853, 'n_words': 676}