blob: fba801b65c3af8a1ff6cd5b234fac10fc5dc17b7 (
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
|
package bjc.utils.data;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* Holds a single value of a specific type. This is used for indirect
* references to data, and more specifically for accessing non-final
* variables from a lambda. AKA the identity monad
*
* This is an eager variant of {@link IHolder}
*
* @author ben
*
* @param <T>
* The type of the data being held
*/
public class GenHolder<T> implements IHolder<T> {
/**
* The state this holder is responsible for.
*/
private T held;
/**
* Creates a new empty holder, with its state set to null
*/
public GenHolder() {
held = null;
}
/**
* Creates a new holder, with its state initialized to the provided
* value
*
* @param held
* The state to initialize this holder to.
*/
public GenHolder(T hld) {
held = hld;
}
/*
* (non-Javadoc)
*
* @see bjc.utils.data.IHolder#map(java.util.function.Function)
*/
@Override
public <NewT> IHolder<NewT> map(Function<T, NewT> f) {
return new GenHolder<NewT>(f.apply(held));
}
/*
* (non-Javadoc)
*
* @see bjc.utils.data.IHolder#transform(java.util.function.Function)
*/
@Override
public IHolder<T> transform(Function<T, T> f) {
held = f.apply(held);
return this;
}
/*
* (non-Javadoc)
*
* @see bjc.utils.data.IHolder#unwrap(java.util.function.Function)
*/
@Override
public <E> E unwrap(Function<T, E> f) {
return f.apply(held);
}
/*
* (non-Javadoc)
*
* @see bjc.utils.data.IHolder#doWith(java.util.function.Consumer)
*/
public void doWith(Consumer<T> f) {
f.accept(held);
}
}
|