fixing warnings from checkstyle in common-app-api
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / util / StreamUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.common.util;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Spliterator;
26 import java.util.Spliterators.AbstractSpliterator;
27 import java.util.function.Consumer;
28 import java.util.function.Predicate;
29 import java.util.stream.Stream;
30 import java.util.stream.StreamSupport;
31
32 /**
33  * Utility Class For Actions On Streams
34  *
35  * @author mshitrit
36  */
37 public final class StreamUtils {
38     private StreamUtils() {
39         throw new UnsupportedOperationException();
40     }
41
42     /**
43      * Breaks the stream when the predicate is not met.<br>
44      * Does not evaluate elements after the stream breaks.<br>
45      * This method evaluates the stream.<br>
46      *
47      * @param stream
48      * @param predicate
49      * @return
50      */
51     public static <T> Stream<T> takeWhilePlusOneNoEval(Stream<T> stream, Predicate<T> predicate) {
52         List<T> results = new ArrayList<>();
53         Consumer<T> listAdder = results::add;
54         stream.map(e -> {
55             listAdder.accept(e);
56             return e;
57         }).filter(e -> !predicate.test(e)).findFirst();
58         return results.stream();
59     }
60
61     public static <T> Stream<T> takeWhile(Stream<T> stream, Predicate<T> predicate) {
62         return StreamSupport.stream(takeWhile(stream.spliterator(), predicate), false);
63     }
64
65     public static <T> Stream<T> takeWhilePlusOne(Stream<T> stream, Predicate<T> predicate) {
66         return StreamSupport.stream(takeWhile(stream.spliterator(), new StopAfterFailPredicate<>(predicate)), false);
67     }
68
69     private static <T> Spliterator<T> takeWhile(Spliterator<T> splitr, Predicate<T> predicate) {
70         return new MySplitIterator<>(splitr, predicate);
71     }
72
73     public static class MySplitIterator<T> extends AbstractSpliterator<T> implements Spliterator<T> {
74         private boolean stillGoing = true;
75         private Spliterator<T> innerItr;
76         private Predicate<T> innerPred;
77
78         private MySplitIterator(Spliterator<T> splitItr, Predicate<T> pred) {
79             super(splitItr.estimateSize(), 0);
80             innerItr = splitItr;
81             innerPred = pred;
82         }
83
84         @Override
85         public boolean tryAdvance(Consumer<? super T> action) {
86             boolean canAdvance = true;
87             if (stillGoing) {
88                 stillGoing = innerItr.tryAdvance(createConsumerWrapper(action));
89             } else {
90                 canAdvance = false;
91             }
92             return canAdvance;
93         }
94
95         private Consumer<? super T> createConsumerWrapper(Consumer<? super T> action) {
96
97             return new Consumer<T>() {
98                 @Override
99                 public void accept(T t) {
100                     stillGoing = innerPred.test(t);
101                     if (stillGoing) {
102                         action.accept(t);
103                     }
104
105                 }
106             };
107         }
108
109     }
110
111     public static class StopAfterFailPredicate<T> implements Predicate<T> {
112         private boolean hasNotFailed;
113         private Predicate<T> innerPredicate;
114
115         private StopAfterFailPredicate(Predicate<T> pred) {
116             hasNotFailed = true;
117             innerPredicate = pred;
118         }
119
120         @Override
121         public boolean test(T t) {
122             boolean isPassed;
123             if (hasNotFailed) {
124                 isPassed = true;
125                 hasNotFailed = innerPredicate.test(t);
126             } else {
127                 isPassed = false;
128             }
129             return isPassed;
130         }
131
132     }
133
134 }