Catalog alignment
[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  */
38 public final class StreamUtils {
39         private StreamUtils() {
40                 throw new UnsupportedOperationException();
41         }
42
43         /**
44          * Breaks the stream when the predicate is not met.<br>
45          * Does not evaluate elements after the stream breaks.<br>
46          * This method evaluates the stream.<br>
47          * 
48          * @param stream
49          * @param predicate
50          * @return
51          */
52         public static <T> Stream<T> takeWhilePlusOneNoEval(Stream<T> stream, Predicate<T> predicate) {
53                 List<T> results = new ArrayList<>();
54                 Consumer<T> listAdder = results::add;
55                 stream.map(e -> {
56                         listAdder.accept(e);
57                         return e;
58                 }).filter(e -> !predicate.test(e)).findFirst();
59                 return results.stream();
60         }
61
62         public static <T> Stream<T> takeWhile(Stream<T> stream, Predicate<T> predicate) {
63                 return StreamSupport.stream(takeWhile(stream.spliterator(), predicate), false);
64         }
65
66         public static <T> Stream<T> takeWhilePlusOne(Stream<T> stream, Predicate<T> predicate) {
67                 return StreamSupport.stream(takeWhile(stream.spliterator(), new StopAfterFailPredicate<>(predicate)), false);
68         }
69
70         private static <T> Spliterator<T> takeWhile(Spliterator<T> splitr, Predicate<T> predicate) {
71                 return new MySplitIterator<>(splitr, predicate);
72         }
73
74         public static class MySplitIterator<T> extends AbstractSpliterator<T> implements Spliterator<T> {
75                 boolean stillGoing = true;
76                 private Spliterator<T> innerItr;
77                 private Predicate<T> innerPred;
78
79                 private MySplitIterator(Spliterator<T> splitItr, Predicate<T> pred) {
80                         super(splitItr.estimateSize(), 0);
81                         innerItr = splitItr;
82                         innerPred = pred;
83                 }
84
85                 @Override
86                 public boolean tryAdvance(Consumer<? super T> action) {
87                         boolean canAdvance = true;
88                         if (stillGoing) {
89                                 stillGoing = innerItr.tryAdvance(createConsumerWrapper(action));
90                         } else {
91                                 canAdvance = false;
92                         }
93                         return canAdvance;
94                 }
95
96                 private Consumer<? super T> createConsumerWrapper(Consumer<? super T> action) {
97
98             return new Consumer<T>() {
99                 @Override
100                 public void accept(T t) {
101                     stillGoing = innerPred.test(t);
102                     if (stillGoing) {
103                         action.accept(t);
104                     }
105
106                 }
107             };
108                 }
109
110         }
111
112         public static class StopAfterFailPredicate<T> implements Predicate<T> {
113                 boolean hasNotFailed;
114                 Predicate<T> innerPredicate;
115
116                 private StopAfterFailPredicate(Predicate<T> pred) {
117                         hasNotFailed = true;
118                         innerPredicate = pred;
119                 };
120
121                 @Override
122                 public boolean test(T t) {
123                         boolean isPassed;
124                         if (hasNotFailed) {
125                                 isPassed = true;
126                                 hasNotFailed = innerPredicate.test(t);
127                         } else {
128                                 isPassed = false;
129                         }
130                         return isPassed;
131                 }
132
133         }
134
135 }