-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnyMatcher.java
41 lines (35 loc) · 1.18 KB
/
AnyMatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Supplier;
/**
* Similar to Kotlin's {@code when { predicate -> value }}.
* <br>
* Usage: <pre>{@code
* when(
* satisfies(() -> amount < 0, () -> "none"),
* satisfies(() -> required > 10 && amount < 10, () -> "less"),
* satisfies(() -> required < 10 && amount < 15, () -> "enough"),
* satisfies(() -> true, () -> "too much")
* );
* }</pre>
*
* @param <T> Return type of {@link AnyMatcher#when(AnyMatcher[])}
* @see Matcher
*/
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class AnyMatcher<T> {
private final Supplier<Boolean> predicate;
private final Supplier<T> result;
@SafeVarargs
public static <T> Optional<T> when(AnyMatcher<T>... matchers) {
return Arrays.stream(matchers)
.filter(matcher -> matcher.predicate.get())
.findFirst()
.map(matcher -> matcher.result.get());
}
public static <T> AnyMatcher<T> satisfies(Supplier<Boolean> predicate, Supplier<T> result) {
return new AnyMatcher<>(predicate, result);
}
}