|
3 | 3 | import org.jetbrains.annotations.NotNull; |
4 | 4 | import org.jetbrains.annotations.Nullable; |
5 | 5 |
|
| 6 | +import java.util.function.BiConsumer; |
| 7 | +import java.util.function.Consumer; |
6 | 8 | import java.util.function.Function; |
7 | 9 |
|
8 | | -public interface ValueProvider<K, V> extends Function<K, ValueWrapper<V>> { |
| 10 | +public interface ValueProvider<K, V> extends BiConsumer<K, Consumer<ValueWrapper<V>>> { |
9 | 11 | static <K, V> ValueProvider<K, V> empty() { |
10 | | - return k -> ValueWrapper.notHandled(); |
| 12 | + return (k, callback) -> callback.accept(ValueWrapper.notHandled()); |
11 | 13 | } |
12 | 14 |
|
13 | 15 | static <K, V> ValueProvider<K, V> simple(Function<@NotNull K, @Nullable V> function) { |
14 | | - return k -> { |
| 16 | + return (k, callback) -> { |
15 | 17 | try { |
16 | | - return ValueWrapper.handled(function.apply(k)); |
| 18 | + callback.accept(ValueWrapper.handled(function.apply(k))); |
17 | 19 | } catch (Throwable e) { |
18 | | - return ValueWrapper.error("An error occurred while getting the value", e); |
| 20 | + callback.accept(ValueWrapper.error("An error occurred while getting the value", e)); |
19 | 21 | } |
20 | 22 | }; |
21 | 23 | } |
22 | 24 |
|
23 | 25 | static <K, V> ValueProvider<K, V> error(@NotNull String errorMessage) { |
24 | | - return k -> ValueWrapper.error(errorMessage); |
| 26 | + return (k, callback) -> callback.accept(ValueWrapper.error(errorMessage)); |
25 | 27 | } |
26 | 28 |
|
27 | 29 | @Override |
28 | | - @NotNull ValueWrapper<V> apply(@NotNull K key); |
| 30 | + void accept(K k, Consumer<ValueWrapper<V>> callback); |
29 | 31 |
|
30 | 32 | default <RK> ValueProvider<RK, V> beforeApply(Function<@NotNull RK, @Nullable K> mapper) { |
31 | | - return rawKey -> { |
| 33 | + return (rawKey, callback) -> { |
32 | 34 | try { |
33 | 35 | K key = mapper.apply(rawKey); |
34 | 36 | if (key == null) { |
35 | | - return ValueWrapper.notHandled(); |
| 37 | + callback.accept(ValueWrapper.notHandled()); |
| 38 | + return; |
36 | 39 | } |
37 | | - return apply(key); |
| 40 | + accept(key, callback); |
38 | 41 | } catch (Throwable e) { |
39 | | - return ValueWrapper.error("An error occurred while mapping the key", e); |
| 42 | + callback.accept(ValueWrapper.error("An error occurred while mapping the key", e)); |
40 | 43 | } |
41 | 44 | }; |
42 | 45 | } |
43 | 46 |
|
44 | 47 | default <F> ValueProvider<K, F> thenApply(Function<@NotNull V, @Nullable F> mapper) { |
45 | | - return key -> { |
46 | | - ValueWrapper<V> wrapper = apply(key); |
| 48 | + return (key, callback) -> accept(key, wrapper -> { |
47 | 49 | if (!wrapper.isHandled()) { |
48 | | - return ValueWrapper.copyNullWrapper(wrapper); |
| 50 | + callback.accept(ValueWrapper.copyNullWrapper(wrapper)); |
| 51 | + return; |
49 | 52 | } |
50 | 53 | if (wrapper.value == null) { |
51 | | - return ValueWrapper.error("The raw value is null"); |
| 54 | + callback.accept(ValueWrapper.error("The raw value is null")); |
| 55 | + return; |
52 | 56 | } |
53 | 57 | try { |
54 | | - return ValueWrapper.handled(mapper.apply(wrapper.value)); |
| 58 | + callback.accept(ValueWrapper.handled(mapper.apply(wrapper.value))); |
55 | 59 | } catch (Throwable e) { |
56 | | - return ValueWrapper.error("An error occurred while converting the raw value", e); |
| 60 | + callback.accept(ValueWrapper.error("An error occurred while converting the raw value", e)); |
57 | 61 | } |
58 | | - }; |
| 62 | + }); |
59 | 63 | } |
60 | 64 | } |
0 commit comments