/*
 * 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 java.util.concurrent.atomic.AtomicBoolean;

import io.jenetics.Gene;
import io.jenetics.Phenotype;

/**
 * This class allows to force a reevaluation of the fitness function. A
 * reevaluation is necessary, if the fitness function changes. Changing the
 * fitness function is not the usual use case, but is necessary for some
 * problems, like symbolic regression analyses with changing input data (time
 * series).
 *
 * <pre>{@code
 * final var nullifier = new FitnessNullifier<DoubleGene, Double>();
 *
 * final Engine<DoubleGene, Double> engine = Engine.builder(problem)
 *     .interceptor(nullifier)
 *     .build();
 *
 * // Invalidate fitness value by calling the 'nullifyFitness' method,
 * // possible from a different thread. This forces the reevaluation of
 * // the fitness values at the start of the next generation.
 * nullifier.nullifyFitness();
 * }</pre>
 *
 * @implNote
 * This interceptor is thread-safe and can be used from different threads. No
 * additional synchronization is needed.
 *
 * @see Phenotype#nullifyFitness()
 *
 * @param <G> the gene type
 * @param <C> the fitness result type
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
 * @since !__version__!
 * @version !__version__!
 */
public final class FitnessNullifier<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
>
	implements EvolutionInterceptor<G, C>
{

	private final AtomicBoolean _invalid = new AtomicBoolean(false);

	/**
	 * Nullifies the fitness values of the population, if requested. The
	 * <em>nullification flag</em> is reset after this call. Two consecutive
	 * calls of this method might lead to two different results.
	 *
	 * @see #nullifyFitness()
	 *
	 * @param start the evolution start object
	 * @return the evolution start object with the nullified fitness values,
	 *         if the nullification has been triggered
	 */
	@Override
	public EvolutionStart<G, C> before(final EvolutionStart<G, C> start) {
		final boolean invalid = _invalid.getAndSet(false);
		return invalid ? invalidate(start) : start;
	}

	private EvolutionStart<G, C> invalidate(final EvolutionStart<G, C> start) {
		return EvolutionStart.of(
			start.population().map(Phenotype::nullifyFitness),
			start.generation()
		);
	}

	/**
	 * Triggers the nullification of the fitness values of the population for
	 * the next generation.
	 *
	 * @see #before(EvolutionStart)
	 */
	public void nullifyFitness() {
		_invalid.set(true);
	}

}

@@ -3179,22 +3179,280 @@
 %0A%09 *
-/%0A%09public void
+%0A%09 * @return %7B@code true%7D if the %3Cem%3Enullification%3C/em%3E request will trigger%0A%09 *         a new fitness nullification. @%7Bcode false%7D if the fitness%0A%09 *         %3Cem%3Enullification%3C/em%3E has been requested before, without%0A%09 *         actually executing it.%0A%09 */%0A%09public boolean
  nul
@@ -3469,16 +3469,24 @@
 s() %7B%0A%09%09
+return !
 _invalid
@@ -3486,17 +3486,23 @@
 invalid.
-s
+getAndS
 et(true)

Improve 'nullifyFitnes' function.