/*
 * 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@gmx.at)
 */
package org.jenetics.optimizer;

import java.util.function.DoubleFunction;
import java.util.function.Function;

import org.jenetics.DoubleChromosome;
import org.jenetics.DoubleGene;
import org.jenetics.Genotype;
import org.jenetics.engine.Codec;
import org.jenetics.util.DoubleRange;
import org.jenetics.util.Factory;
import org.jenetics.util.ISeq;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version !__version__!
 * @since !__version__!
 */
public class CtorCodec<T> implements Codec<T, DoubleGene> {

	private final Factory<Genotype<DoubleGene>> _encoding;
	private final Function<Genotype<DoubleGene>, T> _decoder;

	public CtorCodec(
		final Class<T> type,
		final ISeq<DoubleRange> ranges,
		final ISeq<DoubleFunction<Object>> parameters
	) {
		_encoding = Genotype.of(DoubleChromosome.of(
			ranges.stream()
				.map(DoubleGene::of)
				.toArray(DoubleGene[]::new)
		));

		final Ctor<T> ctor = Ctor.of(
			type,
			parameters
		);

		_decoder = gt -> ctor.cons(
			gt.toSeq().stream()
				.mapToDouble(c -> c.getGene().doubleValue())
				.toArray()
		);
	}

	@Override
	public Factory<Genotype<DoubleGene>> encoding() {
		return _encoding;
	}

	@Override
	public Function<Genotype<DoubleGene>, T> decoder() {
		return _decoder;
	}

}

@@ -844,16 +844,48 @@
 unction;
+%0Aimport java.util.stream.Stream;
 %0A%0Aimport
@@ -1511,57 +1511,8 @@
 nges
-,%0A%09%09final ISeq%3CDoubleFunction%3CObject%3E%3E parameters
 %0A%09) 
@@ -1643,16 +1643,130 @@
 %0A%09%09));%0A%0A
+%09%09final ISeq%3CDoubleFunction%3CObject%3E%3E parameters = ranges%0A%09%09%09.map(r -%3E (DoubleFunction%3CObject%3E) Double::valueOf);%0A%0A
 %09%09final 
@@ -2091,24 +2091,24 @@
 decoder() %7B%0A
-
 %09%09return _de
@@ -2118,10 +2118,687 @@
 er;%0A%09%7D%0A%0A
+%0A%09public static %3CT%3E Codec%3CT, DoubleGene%3E of(%0A%09%09final Class%3CT%3E type,%0A%09%09final ISeq%3CDoubleRange%3E parameters%0A%09) %7B%0A%09%09return Codec.of(%0A%09%09%09Genotype.of(DoubleChromosome.of(%0A%09%09%09%09parameters.stream()%0A%09%09%09%09%09.map(DoubleGene::of)%0A%09%09%09%09%09.toArray(DoubleGene%5B%5D::new)%0A%09%09%09)),%0A%09%09%09gt -%3E %7B%0A%09%09%09%09final Object%5B%5D args =  gt.toSeq().stream()%0A%09%09%09%09%09.map(c -%3E c.getGene().getAllele())%0A%09%09%09%09%09.toArray();%0A%0A%09%09%09%09final Class%3C?%3E%5B%5D parameterTypes = Stream.of(args)%0A%09%09%09%09%09.map(Object::getClass)%0A%09%09%09%09%09.toArray(Class%5B%5D::new);%0A%0A%09%09%09%09try %7B%0A%09%09%09%09%09return type%0A%09%09%09%09%09%09.getConstructor(parameterTypes)%0A%09%09%09%09%09%09.newInstance(args);%0A%09%09%09%09%7D catch (ReflectiveOperationException e) %7B%0A%09%09%09%09%09throw new RuntimeException(e);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09);%0A%09%7D%0A%0A
 %7D%0A

Add 'CtorCodec' factory method.