Skip to content

Commit fe5d543

Browse files
committed
DATACMNS-867 - Further refinements to Optionals.
Made ifAllPresent(…) void as the returned value is never used. Added ifPresentOrElse(…).
1 parent d65d5a4 commit fe5d543

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/main/java/org/springframework/data/util/Optionals.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
*/
1616
package org.springframework.data.util;
1717

18-
import lombok.experimental.UtilityClass;
19-
2018
import java.util.Arrays;
2119
import java.util.Iterator;
2220
import java.util.Optional;
2321
import java.util.function.BiConsumer;
2422
import java.util.function.BiFunction;
23+
import java.util.function.Consumer;
2524
import java.util.function.Function;
2625
import java.util.function.Supplier;
2726
import java.util.stream.Stream;
@@ -34,10 +33,7 @@
3433
* @author Oliver Gierke
3534
* @author Christoph Strobl
3635
*/
37-
@UtilityClass
38-
public class Optionals {
39-
40-
private static final Object SUCCESS = new Object();
36+
public interface Optionals {
4137

4238
/**
4339
* Returns whether any of the given {@link Optional}s is present.
@@ -165,15 +161,15 @@ public static <T, S> Optional<Pair<T, S>> withBoth(Optional<T> left, Optional<S>
165161
* @param right must not be {@literal null}.
166162
* @param consumer must not be {@literal null}.
167163
*/
168-
public static <T, S> Optional<Object> ifAllPresent(Optional<T> left, Optional<S> right, BiConsumer<T, S> consumer) {
164+
public static <T, S> void ifAllPresent(Optional<T> left, Optional<S> right, BiConsumer<T, S> consumer) {
169165

170166
Assert.notNull(left, "Optional must not be null!");
171167
Assert.notNull(right, "Optional must not be null!");
172168
Assert.notNull(consumer, "Consumer must not be null!");
173169

174-
return mapIfAllPresent(left, right, (l, r) -> {
170+
mapIfAllPresent(left, right, (l, r) -> {
175171
consumer.accept(l, r);
176-
return SUCCESS;
172+
return null;
177173
});
178174
}
179175

@@ -195,11 +191,23 @@ public static <T, S, R> Optional<R> mapIfAllPresent(Optional<T> left, Optional<S
195191
return left.flatMap(l -> right.map(r -> function.apply(l, r)));
196192
}
197193

198-
public static <T extends Throwable> void ifBothAbsent(Optional<?> left, Optional<?> right, Supplier<T> supplier)
199-
throws T {
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!");
200206

201-
if (!left.isPresent() && !right.isPresent()) {
202-
throw supplier.get();
207+
if (optional.isPresent()) {
208+
optional.ifPresent(consumer);
209+
} else {
210+
runnable.run();
203211
}
204212
}
205213
}

0 commit comments

Comments
 (0)