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

import static java.lang.String.format;
import static io.jenetics.internal.util.Hashes.hash;
import static io.jenetics.util.ISeq.toISeq;

import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.jenetics.util.ISeq;
import io.jenetics.util.Seq;

/**
 * Combines several alterers to one.
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
 * @since 1.0
 * @version 4.0
 */
final class CompositeAlterer<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
>
	extends AbstractAlterer<G, C>
{

	private final ISeq<Alterer<G, C>> _alterers;

	/**
	 * Combine the given alterers.
	 *
	 * @param alterers the alterers to combine.
	 * @throws NullPointerException if one of the alterers is {@code null}.
	 */
	CompositeAlterer(final Seq<Alterer<G, C>> alterers) {
		super(1.0);
		_alterers = normalize(alterers);
	}

	private static <G extends Gene<?, G>, C extends Comparable<? super C>>
	ISeq<Alterer<G, C>> normalize(final Seq<Alterer<G, C>> alterers) {
		final Function<Alterer<G, C>, Stream<Alterer<G, C>>> mapper =
			a -> a instanceof CompositeAlterer
				? ((CompositeAlterer<G, C>)a).getAlterers().stream()
				: Stream.of(a);

		return alterers.stream()
			.flatMap(mapper)
			.collect(toISeq());
	}

	@Override
	public AltererResult<G, C> alter(
		final Seq<Phenotype<G, C>> population,
		final long generation
	) {
		AltererResult<G, C> result = AltererResult.of(population.asISeq());
		for (Alterer<G, C> alterer : _alterers) {
			final AltererResult<G, C> as = alterer.alter(
				result.getPopulation(),
				generation
			);

			result = AltererResult.of(
				as.getPopulation(),
				as.getAlterations() + result.getAlterations()
			);
		}

		return result;
	}

	/**
	 * Return the alterers this alterer consists of. The returned array is sealed
	 * and cannot be changed.
	 *
	 * @return the alterers this alterer consists of.
	 */
	ISeq<Alterer<G, C>> getAlterers() {
		return _alterers;
	}

	@Override
	public int hashCode() {
		return hash(_alterers);
	}

	@Override
	public boolean equals(final Object obj) {
		return obj == this ||
			obj instanceof CompositeAlterer &&
			Objects.equals(((CompositeAlterer) obj)._alterers, _alterers);
	}

	@Override
	public String toString() {
		return format(
			"%s:\n%s", getClass().getSimpleName(),
			_alterers.stream()
				.map(a -> "   - " + a)
				.collect(Collectors.joining("\n"))
		);
	}

	/**
	 * Combine the given alterers.
	 *
	 * @param <G> the gene type
	 * @param <C> the fitness function result type
	 * @param alterers the alterers to combine.
	 * @return a new alterer which consists of the given one
	 * @throws NullPointerException if one of the alterers is {@code null}.
	 */
	@SafeVarargs
	static <G extends Gene<?, G>, C extends Comparable<? super C>>
	CompositeAlterer<G, C> of(final Alterer<G, C>... alterers) {
		return new CompositeAlterer<>(ISeq.of(alterers));
	}

	/**
	 * Joins the given alterer and returns a new CompositeAlterer object. If one
	 * of the given alterers is a CompositeAlterer the sub alterers of it are
	 * unpacked and appended to the newly created CompositeAlterer.
	 *
	 * @param <T> the gene type of the alterers.
	 *
	 * @param <C> the fitness function result type
	 * @param a1 the first alterer.
	 * @param a2 the second alterer.
	 * @return a new CompositeAlterer object.
	 * @throws NullPointerException if one of the given alterer is {@code null}.
	 */
	static <T extends Gene<?, T>, C extends Comparable<? super C>>
	CompositeAlterer<T, C> join(
		final Alterer<T, C> a1,
		final Alterer<T, C> a2
	) {
		return CompositeAlterer.of(a1, a2);
	}
}

@@ -828,105 +828,26 @@
 ics.
-internal.util.Hashes.hash;%0Aimport static io.jenetics.util.ISeq.toISeq;%0A%0Aimport java.util.Objects;
+util.ISeq.toISeq;%0A
 %0Aimp
@@ -1167,11 +1167,21 @@
 ion 
-4.0
+!__version__!
 %0A */
@@ -2717,260 +2717,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public int hashCode() %7B%0A%09%09return hash(_alterers);%0A%09%7D%0A%0A%09@Override%0A%09public boolean equals(final Object obj) %7B%0A%09%09return obj == this %7C%7C%0A%09%09%09obj instanceof CompositeAlterer &&%0A%09%09%09Objects.equals(((CompositeAlterer) obj)._alterers, _alterers);%0A%09%7D%0A%0A
 %09@Ov

Remove 'equals' and 'hashCode' from 'CompositeAlterer'.