1 /*
2  * Copyright 2016-2020 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.data.util;
17
18 import java.util.Arrays;
19 import java.util.Iterator;
20 import java.util.Optional;
21 import java.util.function.BiConsumer;
22 import java.util.function.BiFunction;
23 import java.util.function.Consumer;
24 import java.util.function.Function;
25 import java.util.function.Supplier;
26 import java.util.stream.Stream;
27
28 import org.springframework.util.Assert;
29
30 /**
31  * Utility methods to work with {@link Optional}s.
32  *
33  * @author Oliver Gierke
34  * @author Christoph Strobl
35  */

36 public interface Optionals {
37
38     /**
39      * Returns whether any of the given {@link Optional}s is present.
40      *
41      * @param optionals must not be {@literal null}.
42      * @return
43      */

44     public static boolean isAnyPresent(Optional<?>... optionals) {
45
46         Assert.notNull(optionals, "Optionals must not be null!");
47
48         return Arrays.stream(optionals).anyMatch(Optional::isPresent);
49     }
50
51     /**
52      * Turns the given {@link Optional} into a one-element {@link Stream} or an empty one if not present.
53      *
54      * @param optionals must not be {@literal null}.
55      * @return
56      */

57     @SafeVarargs
58     public static <T> Stream<T> toStream(Optional<? extends T>... optionals) {
59
60         Assert.notNull(optionals, "Optional must not be null!");
61
62         return Arrays.asList(optionals).stream().flatMap(it -> it.map(Stream::of).orElseGet(Stream::empty));
63     }
64
65     /**
66      * Applies the given function to the elements of the source and returns the first non-empty result.
67      *
68      * @param source must not be {@literal null}.
69      * @param function must not be {@literal null}.
70      * @return
71      */

72     public static <S, T> Optional<T> firstNonEmpty(Iterable<S> source, Function<S, Optional<T>> function) {
73
74         Assert.notNull(source, "Source must not be null!");
75         Assert.notNull(function, "Function must not be null!");
76
77         return Streamable.of(source).stream()//
78                 .map(function::apply)//
79                 .filter(Optional::isPresent)//
80                 .findFirst().orElseGet(Optional::empty);
81     }
82
83     /**
84      * Applies the given function to the elements of the source and returns the first non-empty result.
85      *
86      * @param source must not be {@literal null}.
87      * @param function must not be {@literal null}.
88      * @return
89      */

90     public static <S, T> T firstNonEmpty(Iterable<S> source, Function<S, T> function, T defaultValue) {
91
92         Assert.notNull(source, "Source must not be null!");
93         Assert.notNull(function, "Function must not be null!");
94
95         return Streamable.of(source).stream()//
96                 .map(function::apply)//
97                 .filter(it -> !it.equals(defaultValue))//
98                 .findFirst().orElse(defaultValue);
99     }
100
101     /**
102      * Invokes the given {@link Supplier}s for {@link Optional} results one by one and returns the first non-empty one.
103      *
104      * @param suppliers must not be {@literal null}.
105      * @return
106      */

107     @SafeVarargs
108     public static <T> Optional<T> firstNonEmpty(Supplier<Optional<T>>... suppliers) {
109
110         Assert.notNull(suppliers, "Suppliers must not be null!");
111
112         return firstNonEmpty(Streamable.of(suppliers));
113     }
114
115     /**
116      * Invokes the given {@link Supplier}s for {@link Optional} results one by one and returns the first non-empty one.
117      *
118      * @param suppliers must not be {@literal null}.
119      * @return
120      */

121     public static <T> Optional<T> firstNonEmpty(Iterable<Supplier<Optional<T>>> suppliers) {
122
123         Assert.notNull(suppliers, "Suppliers must not be null!");
124
125         return Streamable.of(suppliers).stream()//
126                 .map(Supplier::get)//
127                 .filter(Optional::isPresent)//
128                 .findFirst().orElse(Optional.empty());
129     }
130
131     /**
132      * Returns the next element of the given {@link Iterator} or {@link Optional#empty()} in case there is no next
133      * element.
134      *
135      * @param iterator must not be {@literal null}.
136      * @return
137      */

138     public static <T> Optional<T> next(Iterator<T> iterator) {
139
140         Assert.notNull(iterator, "Iterator must not be null!");
141
142         return iterator.hasNext() ? Optional.of(iterator.next()) : Optional.empty();
143     }
144
145     /**
146      * Returns a {@link Pair} if both {@link Optional} instances have values or {@link Optional#empty()} if one or both
147      * are missing.
148      *
149      * @param left
150      * @param right
151      * @return
152      */

153     public static <T, S> Optional<Pair<T, S>> withBoth(Optional<T> left, Optional<S> right) {
154         return left.flatMap(l -> right.map(r -> Pair.of(l, r)));
155     }
156
157     /**
158      * Invokes the given {@link BiConsumer} if all given {@link Optional} are present.
159      *
160      * @param left must not be {@literal null}.
161      * @param right must not be {@literal null}.
162      * @param consumer must not be {@literal null}.
163      */

164     public static <T, S> void ifAllPresent(Optional<T> left, Optional<S> right, BiConsumer<T, S> consumer) {
165
166         Assert.notNull(left, "Optional must not be null!");
167         Assert.notNull(right, "Optional must not be null!");
168         Assert.notNull(consumer, "Consumer must not be null!");
169
170         mapIfAllPresent(left, right, (l, r) -> {
171             consumer.accept(l, r);
172             return null;
173         });
174     }
175
176     /**
177      * Maps the values contained in the given {@link Optional} if both of them are present.
178      *
179      * @param left must not be {@literal null}.
180      * @param right must not be {@literal null}.
181      * @param function must not be {@literal null}.
182      * @return
183      */

184     public static <T, S, R> Optional<R> mapIfAllPresent(Optional<T> left, Optional<S> right,
185             BiFunction<T, S, R> function) {
186
187         Assert.notNull(left, "Optional must not be null!");
188         Assert.notNull(right, "Optional must not be null!");
189         Assert.notNull(function, "BiFunctionmust not be null!");
190
191         return left.flatMap(l -> right.map(r -> function.apply(l, r)));
192     }
193
194     /**
195      * Invokes the given {@link Consumer} if the {@link Optional} is present or the {@link Runnable} if not.
196      *
197      * @param optional must not be {@literal null}.
198      * @param consumer must not be {@literal null}.
199      * @param runnable must not be {@literal null}.
200      */

201     public static <T> void ifPresentOrElse(Optional<T> optional, Consumer<? super T> consumer, Runnable runnable) {
202
203         Assert.notNull(optional, "Optional must not be null!");
204         Assert.notNull(consumer, "Consumer must not be null!");
205         Assert.notNull(runnable, "Runnable must not be null!");
206
207         if (optional.isPresent()) {
208             optional.ifPresent(consumer);
209         } else {
210             runnable.run();
211         }
212     }
213 }
214