/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package io.jenetics.example.image;

import java.util.ListIterator;

import io.jenetics.Mutator;
import io.jenetics.util.MSeq;

/**
 * Polygon mutator class.
 *
 * @param <C> the fitness type
 */
final class PolygonMutator<C extends Comparable<? super C>>
	extends Mutator<PolygonGene, C>
{

	private final float _rate;
	private final float _magnitude;

	public PolygonMutator(final float rate, final float magnitude) {
		super(1.0);
		_rate = rate;
		_magnitude = magnitude;
	}

	@Override
	protected int mutate(final MSeq<PolygonGene> genes, final double p) {
		final ListIterator<PolygonGene> it = genes.listIterator();

		while (it.hasNext()) {
			final PolygonGene gene = it.next();
			final PolygonGene mutated = gene.newInstance(
				gene.getAllele().mutate(_rate, _magnitude)
			);
			it.set(mutated);
		}

		return genes.size();
	}

}

@@ -853,22 +853,47 @@
 til.
-ListIterator;%0A
+Random;%0A%0Aimport io.jenetics.Chromosome;
 %0Aimp
@@ -913,18 +913,25 @@
 cs.Mutat
-or
+ionResult
 ;%0Aimport
@@ -947,17 +947,15 @@
 ics.
+M
 ut
-il.MSeq
+ator
 ;%0A%0A/
@@ -1331,19 +1331,55 @@
 ted 
-int
+MutationResult%3CChromosome%3CPolygonGene%3E%3E
  mutate(
 fina
@@ -1378,18 +1378,27 @@
 ate(
+%0A%09%09
 final 
-MSeq
+Chromosome
 %3CPol
@@ -1411,15 +1411,22 @@
 ne%3E 
-genes, 
+chromosome,%0A%09%09
 fina
@@ -1435,19 +1435,17 @@
 double p
-) %7B
+,
 %0A%09%09final
@@ -1449,129 +1449,180 @@
 nal 
-ListIterator%3CPolygonGene%3E it = genes.listIterator();%0A%0A%09%09while (it.hasNext()) %7B%0A%09%09%09final PolygonGene gene = it.next();%0A%09%09%09
+Random random%0A%09) %7B%0A%09%09return MutationResult.of(%0A%09%09%09chromosome.newInstance(chromosome.toSeq().map(this::mutate)),%0A%09%09%09chromosome.length()%0A%09%09);%0A%09%7D%0A%0A%09private PolygonGene mutate(
 fina
@@ -1639,17 +1639,24 @@
 ene 
-mutated =
+gene) %7B%0A%09%09return
  gen
@@ -1673,13 +1673,8 @@
 nce(
-%0A%09%09%09%09
 gene
@@ -1715,60 +1715,8 @@
 ude)
-%0A%09%09%09);%0A%09%09%09it.set(mutated);%0A%09%09%7D%0A%0A%09%09return genes.size(
 );%0A%09

Fix 'PolygonMutator' in example.