-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatcher.java
72 lines (61 loc) · 2.24 KB
/
Matcher.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* Similar to Kotlin's {@code when(param) { matcher -> value }}.
* <br>
* Usage: <pre>{@code
* when(value,
* matches(myVal, () -> "Matched myVal!"),
* instanceOf(MyClass.class, () -> "Instance of MyClass!"),
* satisfies(val -> val > 10 && val < 15, () -> "Between 10 & 15!"),
* otherwise(() -> "¯\_(ツ)_/¯")
* );
* }</pre>
*
* @param <T> Type of value to match
* @param <U> Return type of {@link Matcher#when}
* @see AnyMatcher
*/
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class Matcher<T, U> {
private final Predicate<T> predicate;
private final Supplier<U> resultSupplier;
/**
* @see Matcher#when(Object, Matcher[])
*/
@SafeVarargs
public static <T, U> Function<T, Optional<U>> whenValue(Matcher<T, U>... matchers) {
return value -> when(value, matchers);
}
/**
* @see Matcher
*/
@SafeVarargs
public static <T, U> Optional<U> when(T value, Matcher<T, U>... matchers) {
return Arrays.stream(matchers)
.filter(matcher -> matcher.predicate.test(value))
.findFirst()
.map(matcher -> matcher.resultSupplier.get());
}
public static <T, U> Matcher<T, U> matches(T value, Supplier<U> resultSupplier) {
return new Matcher<>(value::equals, resultSupplier);
}
public static <T, U> Matcher<T, U> matchesAny(List<T> values, Supplier<U> resultSupplier) {
return new Matcher<>(t -> values.stream().anyMatch(v -> v.equals(t)), resultSupplier);
}
public static <T, U, V> Matcher<T, U> instanceOf(Class<V> clazz, Supplier<U> resultSupplier) {
return new Matcher<>(clazz::isInstance, resultSupplier);
}
public static <T, U> Matcher<T, U> satisfies(Predicate<T> predicate, Supplier<U> resultSupplier) {
return new Matcher<>(predicate, resultSupplier);
}
public static <T, U> Matcher<T, U> otherwise(Supplier<U> resultSupplier) {
return new Matcher<>(__ -> true, resultSupplier);
}
}