/*
 * 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.math.random.indexes;

import java.util.Random;

import io.jenetics.internal.util.Equality;
import io.jenetics.internal.util.Hash;
import io.jenetics.util.MSeq;

/**
 * The {@code SwapMutation} changes the order of genes in a chromosome, with the
 * hope of bringing related genes closer together, thereby facilitating the
 * production of building blocks. This mutation operator can also be used for
 * combinatorial problems, where no duplicated genes within a chromosome are
 * allowed, e.g. for the TSP.
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
 * @since 1.0
 * @version 4.0
 */
public class SwapMutator<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
>
	extends Mutator<G, C>
{

	/**
	 * Constructs an alterer with a given recombination probability.
	 *
	 * @param probability the crossover probability.
	 * @throws IllegalArgumentException if the {@code probability} is not in the
	 *          valid range of {@code [0, 1]}.
	 */
	public SwapMutator(final double probability) {
		super(probability);
	}

	/**
	 * Default constructor, with default mutation probability
	 * ({@link AbstractAlterer#DEFAULT_ALTER_PROBABILITY}).
	 */
	public SwapMutator() {
		this(DEFAULT_ALTER_PROBABILITY);
	}

	/**
	 * Swaps the genes in the given array, with the mutation probability of this
	 * mutation.
	 */
	@Override
	protected MutationResult<Chromosome<G>> mutate(
		final Chromosome<G> chromosome,
		final double p,
		final Random random
	) {
		final MSeq<G> genes = chromosome.toSeq().copy();
		final int mutations = (int)indexes(random, genes.length(), p)
			.peek(i -> genes.swap(i, random.nextInt(genes.length())))
			.count();

		return MutationResult.of(chromosome.newInstance(genes.toISeq()), mutations);
	}

	@Override
	public int hashCode() {
		return Hash.of(getClass()).and(super.hashCode()).value();
	}

	@Override
	public boolean equals(final Object obj) {
		return Equality.of(this, obj).test(super::equals);
	}

	@Override
	public String toString() {
		return format("%s[p=%f]", getClass().getSimpleName(), _probability);
	}

}

@@ -2328,16 +2328,96 @@
 om%0A%09) %7B%0A
+%09%09final MutationResult%3CChromosome%3CG%3E%3E result;%0A%09%09if (chromosome.length() %3E 1) %7B%0A%09
 %09%09final 
@@ -2459,16 +2459,17 @@
 copy();%0A
+%09
 %09%09final 
@@ -2527,16 +2527,17 @@
 , p)%0A%09%09%09
+%09
 .peek(i 
@@ -2589,16 +2589,17 @@
 ))))%0A%09%09%09
+%09
 .count()
@@ -2600,25 +2600,27 @@
 ount();%0A
-%0A
+%09
 %09%09re
-turn
+sult =
  Mutatio
@@ -2630,16 +2630,21 @@
 sult.of(
+%0A%09%09%09%09
 chromoso
@@ -2674,17 +2674,21 @@
 ISeq()),
- 
+%0A%09%09%09%09
 mutation
@@ -2688,17 +2688,97 @@
 utations
-)
+%0A%09%09%09);%0A%09%09%7D else %7B%0A%09%09%09result = MutationResult.of(chromosome);%0A%09%09%7D%0A%0A%09%09return result
 ;%0A%09%7D%0A%0A%09@

Fix failing 'SwapMutator' tests.