2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.common.util;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Spliterator;
25 import java.util.Spliterators.AbstractSpliterator;
26 import java.util.function.Consumer;
27 import java.util.function.Predicate;
28 import java.util.stream.Stream;
29 import java.util.stream.StreamSupport;
32 * Utility Class For Actions On Streams
36 public final class StreamUtils {
38 private StreamUtils() {
39 throw new UnsupportedOperationException();
43 * Breaks the stream when the predicate is not met.<br> Does not evaluate elements after the stream breaks.<br> This method evaluates the
50 public static <T> Stream<T> takeWhilePlusOneNoEval(Stream<T> stream, Predicate<T> predicate) {
51 List<T> results = new ArrayList<>();
52 Consumer<T> listAdder = results::add;
56 }).filter(e -> !predicate.test(e)).findFirst();
57 return results.stream();
60 public static <T> Stream<T> takeWhile(Stream<T> stream, Predicate<T> predicate) {
61 return StreamSupport.stream(takeWhile(stream.spliterator(), predicate), false);
64 public static <T> Stream<T> takeWhilePlusOne(Stream<T> stream, Predicate<T> predicate) {
65 return StreamSupport.stream(takeWhile(stream.spliterator(), new StopAfterFailPredicate<>(predicate)), false);
68 private static <T> Spliterator<T> takeWhile(Spliterator<T> splitr, Predicate<T> predicate) {
69 return new MySplitIterator<>(splitr, predicate);
72 public static class MySplitIterator<T> extends AbstractSpliterator<T> implements Spliterator<T> {
74 boolean stillGoing = true;
75 private Spliterator<T> innerItr;
76 private Predicate<T> innerPred;
78 private MySplitIterator(Spliterator<T> splitItr, Predicate<T> pred) {
79 super(splitItr.estimateSize(), 0);
85 public boolean tryAdvance(Consumer<? super T> action) {
86 boolean canAdvance = true;
88 stillGoing = innerItr.tryAdvance(createConsumerWrapper(action));
95 private Consumer<? super T> createConsumerWrapper(Consumer<? super T> action) {
96 return new Consumer<T>() {
98 public void accept(T t) {
99 stillGoing = innerPred.test(t);
108 public static class StopAfterFailPredicate<T> implements Predicate<T> {
110 boolean hasNotFailed;
111 Predicate<T> innerPredicate;
113 private StopAfterFailPredicate(Predicate<T> pred) {
115 innerPredicate = pred;
121 public boolean test(T t) {
125 hasNotFailed = innerPredicate.test(t);