001/*
002 * Java Genetic Algorithm Library (jenetics-8.0.0).
003 * Copyright (c) 2007-2024 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020package io.jenetics.internal.engine;
021
022import java.util.Spliterator;
023import java.util.function.Function;
024import java.util.function.Predicate;
025import java.util.function.Supplier;
026import java.util.stream.StreamSupport;
027
028import io.jenetics.Gene;
029import io.jenetics.engine.Evolution;
030import io.jenetics.engine.EvolutionResult;
031import io.jenetics.engine.EvolutionStart;
032import io.jenetics.engine.EvolutionStream;
033import io.jenetics.internal.util.LimitSpliterator;
034import io.jenetics.internal.util.StreamProxy;
035
036/**
037 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
038 * @since 3.0
039 * @version 5.1
040 */
041public final class EvolutionStreamImpl<
042        G extends Gene<?, G>,
043        C extends Comparable<? super C>
044>
045        extends StreamProxy<EvolutionResult<G, C>>
046        implements EvolutionStream<G, C>
047{
048
049        private final Spliterator<EvolutionResult<G, C>> _spliterator;
050
051        public EvolutionStreamImpl(
052                final Spliterator<EvolutionResult<G, C>> spliterator,
053                final boolean parallel
054        ) {
055                super(StreamSupport.stream(spliterator, parallel));
056                _spliterator = spliterator;
057        }
058
059        public EvolutionStreamImpl(
060                final Supplier<EvolutionStart<G, C>> start,
061                final Evolution<G, C> evolution
062        ) {
063                this(new EvolutionSpliterator<>(start, evolution), false);
064        }
065
066        @Override
067        public EvolutionStream<G, C>
068        limit(final Predicate<? super EvolutionResult<G, C>> proceed) {
069                return new EvolutionStreamImpl<>(
070                        LimitSpliterator.of(_spliterator, proceed),
071                        isParallel()
072                );
073        }
074
075        public static <G extends Gene<?, G>, C extends Comparable<? super C>>
076        EvolutionStreamImpl<G, C> of(
077                final Supplier<EvolutionStart<G, C>> start,
078                final Function<
079                        ? super EvolutionStart<G, C>,
080                        ? extends Evolution<G, C>> evolution
081        ) {
082                return new EvolutionStreamImpl<>(
083                        EvolutionSpliterator.of(start, evolution),
084                        false
085                );
086        }
087
088}