EvolutionStreamImpl.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-3.5.0).
03  * Copyright (c) 2007-2016 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
19  */
20 package org.jenetics.engine;
21 
22 import static java.util.Objects.requireNonNull;
23 
24 import java.util.function.Function;
25 import java.util.function.Predicate;
26 import java.util.function.Supplier;
27 import java.util.stream.Stream;
28 import java.util.stream.StreamSupport;
29 
30 import org.jenetics.Gene;
31 
32 /**
33  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
34  * @since 3.0
35  * @version 3.0
36  */
37 final class EvolutionStreamImpl<
38     G extends Gene<?, G>,
39     C extends Comparable<? super C>
40 >
41     extends StreamProxy<EvolutionResult<G, C>>
42     implements EvolutionStream<G, C>
43 {
44 
45     private final Supplier<EvolutionStart<G, C>> _start;
46     private final Function<? super EvolutionStart<G, C>, EvolutionResult<G, C>> _evolution;
47     private final Predicate<? super EvolutionResult<G, C>> _proceed;
48 
49     private EvolutionStreamImpl(
50         final Supplier<EvolutionStart<G, C>> start,
51         final Function<? super EvolutionStart<G, C>, EvolutionResult<G, C>> evolution,
52         final Stream<EvolutionResult<G, C>> stream,
53         final Predicate<? super EvolutionResult<G, C>> proceed
54     ) {
55         super(stream);
56         _evolution = requireNonNull(evolution);
57         _start = requireNonNull(start);
58         _proceed = requireNonNull(proceed);
59     }
60 
61     EvolutionStreamImpl(
62         final Supplier<EvolutionStart<G, C>> start,
63         final Function<? super EvolutionStart<G, C>, EvolutionResult<G, C>> evolution
64     ) {
65         this(
66             start, evolution,
67             StreamSupport.stream(
68                 new EvolutionSpliterator<>(start, evolution, TRUE()),
69                 false
70             ),
71             TRUE()
72         );
73     }
74 
75     @Override
76     public EvolutionStream<G, C>
77     limit(final Predicate<? super EvolutionResult<G, C>> proceed) {
78         final Predicate<? super EvolutionResult<G, C>> prcd =
79             _proceed == TRUE ? proceed : r -> proceed.test(r) & _proceed.test(r);
80 
81         return new EvolutionStreamImpl<>(
82             _start,
83             _evolution,
84             StreamSupport.stream(
85                 new EvolutionSpliterator<>(_start, _evolution, prcd),
86                 false
87             ),
88             prcd
89         );
90     }
91 
92     private static final Predicate<?> TRUE = a -> true;
93 
94     @SuppressWarnings("unchecked")
95     private static <T> Predicate<T> TRUE() {
96         return (Predicate<T>)TRUE;
97     }
98 
99 }