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

import java.util.ArrayList;
import java.util.List;
import java.util.Spliterator;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import java.util.stream.BaseStream;
import java.util.stream.Collectors;

import io.jenetics.Gene;
import io.jenetics.engine.Engine;
import io.jenetics.engine.EvolutionResult;
import io.jenetics.engine.EvolutionStart;
import io.jenetics.engine.EvolutionStream;
import io.jenetics.internal.util.JoinedSpliterator;
import io.jenetics.util.ISeq;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
 * @version !__version__!
 * @since !__version__!
 */
public class EngineConcat<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
> {

	private static class Pair<A, B> {
		final A _1;
		final B _2;
		private Pair(final A a, final B b) {
			_1 = a;
			_2 = b;
		}
		static <A, B> Pair<A, B> of(final A a, final B b) {
			return new Pair<>(a, b);
		}
	}

	private final List<Pair<Engine<G, C>, Predicate<? super EvolutionResult<G, C>>>>
	_engines = new ArrayList<>();

	public EngineConcat<G, C> append(
		final Engine<G, C> engine,
		final Predicate<? super EvolutionResult<G, C>> proceed
	) {
		_engines.add(Pair.of(engine, proceed));
		return this;
	}

	public EvolutionStream<G, C> stream() {
		final AtomicReference<EvolutionStart<G, C>> start =
			new AtomicReference<>(EvolutionStart.of(ISeq.empty(), 1));

		final List<Spliterator<EvolutionResult<G, C>>> spliterators =
			_engines.stream()
				.map(p -> p._1.stream(start::get)
					.limit(p._2)
					.peek(r -> start.set(r.next())))
				.map(BaseStream::spliterator)
				.collect(Collectors.toList());

		final JoinedSpliterator<EvolutionResult<G, C>> spliterator =
			new JoinedSpliterator<>(spliterators);

		return null;
	}

	public static <G extends Gene<?, G>, C extends Comparable<? super C>>
	EngineConcat<G, C> serial() {
		return new EngineConcat<>();
	}

	public static <G extends Gene<?, G>, C extends Comparable<? super C>>
	EngineConcat<G, C> woven()  {
		return null;
	}

}

@@ -1189,24 +1189,80 @@
 tionStream;%0A
+import io.jenetics.internal.engine.EvolutionStreamImpl;%0A
 import io.je
@@ -2495,68 +2495,45 @@
 %0A%0A%09%09
-final JoinedSpliterator%3CEvolutionResult%3CG, C%3E%3E spliterator =
+return new EvolutionStreamImpl%3CG, C%3E(
 %0A%09%09%09
@@ -2573,24 +2573,22 @@
 ors)
-;%0A%0A%09%09return null
+,%0A%09%09%09false%0A%09%09)
 ;%0A%09%7D

Implement 'EngineConcat' class.