/*
 * 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.internal.engine;

import static java.util.Objects.requireNonNull;

import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import io.jenetics.Gene;
import io.jenetics.engine.EvolutionResult;
import io.jenetics.engine.EvolutionStart;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
 * @since 3.0
 * @version 4.1
 */
public final class EvolutionSpliterator<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
>
	implements Spliterator<EvolutionResult<G, C>>
{

	private final Supplier<EvolutionStart<G, C>> _start;
	private final Function<? super EvolutionStart<G, C>, EvolutionResult<G, C>> _evolution;

	private long _estimate;
	private EvolutionStart<G, C> _next = null;

	private EvolutionSpliterator(
		final Supplier<EvolutionStart<G, C>> start,
		final Function<? super EvolutionStart<G, C>, EvolutionResult<G, C>> evolution,
		final long estimate
	) {
		_evolution = requireNonNull(evolution);
		_start = requireNonNull(start);
		_estimate = estimate;
	}

	public EvolutionSpliterator(
		final Supplier<EvolutionStart<G, C>> start,
		final Function<? super EvolutionStart<G, C>, EvolutionResult<G, C>> evolution
	) {
		this(start, evolution, Long.MAX_VALUE);
	}

	@Override
	public boolean
	tryAdvance(final Consumer<? super EvolutionResult<G, C>> action) {
		if (_next == null) {
			_next = _start.get();
		}

		final EvolutionResult<G, C> result = _evolution.apply(_next);
		action.accept(result);
		_next = result.next();
		return true;
	}

	@Override
	public Spliterator<EvolutionResult<G, C>> trySplit() {
		return _estimate > 0
			? new EvolutionSpliterator<>(_start, _evolution, _estimate >>>= 1)
			: null;
	}

	@Override
	public long estimateSize() {
		return _estimate;
	}

	@Override
	public int characteristics() {
		return NONNULL | IMMUTABLE | ORDERED;
	}

}

@@ -1078,16 +1078,164 @@
 t;%0A%0A/**%0A
+ * The %7B@code Spliterator%7D implementation of the %7B@code EvolutionStream%7D.%0A *%0A * @param %3CG%3E the gene type%0A * @param %3CC%3E the evolution result type%0A *%0A
  * @auth
@@ -1341,11 +1341,21 @@
 ion 
-4.1
+!__version__!
 %0A */
@@ -2007,16 +2007,301 @@
 te;%0A%09%7D%0A%0A
+%09/**%0A%09 * Create a new evolution spliterator with the given %7B@code start%7D element%0A%09 * and the %7B@code evolution%7D function.%0A%09 *%0A%09 *%0A%09 * @param start the start element%0A%09 * @param evolution the evolution function%0A%09 * @throws NullPointerException if one of the argument is %7B@code null%7D%0A%09 */%0A
 %09public 
@@ -3107,10 +3107,729 @@
 ED;%0A%09%7D%0A%0A
+%09/**%0A%09 * Create a new spliterator with the given %7B@code start%7D element and the%0A%09 * %7B@code evolution%7D function-function.%0A%09 *%0A%09 * @param start the start element%0A%09 * @param evolution the evolution function-function%0A%09 * @param %3CG%3E the gene type%0A%09 * @param %3CC%3E the evolution result type%0A%09 * @return a new evolution spliterator%0A%09 */%0A%09public static %3CG extends Gene%3C?, G%3E, C extends Comparable%3C? super C%3E%3E%0A%09EvolutionSpliterator%3CG, C%3E of(%0A%09%09final Supplier%3CEvolutionStart%3CG, C%3E%3E start,%0A%09%09final Function%3C%0A%09%09%09? super EvolutionStart%3CG, C%3E,%0A%09%09%09Function%3C? super EvolutionStart%3CG, C%3E, EvolutionResult%3CG, C%3E%3E%3E evolution%0A%09) %7B%0A%09%09return new EvolutionSpliterator%3CG, C%3E(%0A%09%09%09start,%0A%09%09%09result -%3E evolution.apply(result).apply(result)%0A%09%09);%0A%09%7D%0A%0A
 %7D%0A

Add new factory method.