blob: 6ccf115569d07567cd8f84962f9e96fa7b19821e (
plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
package bjc.utils.data.experimental;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.UnaryOperator;
/**
* A holder of a single value.
*
* @author ben
*
* @param <ContainedType>
* The type of value held
*/
public interface IHolder<ContainedType> {
/**
* Bind a function across the value in this container
*
* @param <BoundType>
* The type of value in this container
* @param binder
* The function to bind to the value
* @return A holder from binding the value
*/
public <BoundType> IHolder<BoundType> bind(
Function<ContainedType, IHolder<BoundType>> binder);
/**
* Apply an action to the value
*
* @param action
* The action to apply to the value
*/
public default void doWith(Consumer<ContainedType> action) {
transform((value) -> {
action.accept(value);
return value;
});
}
/**
* Get the value contained in this holder without changing it.
*
* @return The value held in this holder
*/
public default ContainedType getValue() {
return unwrap((value) -> value);
}
/**
* Create a new holder with a mapped version of the value in this
* holder.
*
* Does not change the internal state of this holder
*
* @param <MappedType>
* The type of the mapped value
* @param mapper
* The function to do mapping with
* @return A holder with the mapped value
*/
public <MappedType> IHolder<MappedType> map(
Function<ContainedType, MappedType> mapper);
/**
* Transform the value held in this holder
*
* @param transformer
* The function to transform the value with
* @return The holder itself, for easy chaining
*/
public IHolder<ContainedType> transform(
UnaryOperator<ContainedType> transformer);
/**
* Unwrap the value contained in this holder so that it is no longer
* held
*
* @param <UnwrappedType>
* The type of the unwrapped value
* @param unwrapper
* The function to use to unwrap the value
* @return The unwrapped held value
*/
public <UnwrappedType> UnwrappedType unwrap(
Function<ContainedType, UnwrappedType> unwrapper);
/**
* Replace the held value with a new one
*
* @param newValue
* The value to hold instead
* @return The holder itself
*/
public default IHolder<ContainedType> replace(ContainedType newValue) {
return transform((oldValue) -> {
return newValue;
});
}
}
|