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
102
103
104
105
106
107
108
109
|
package bjc.utils.data.lazy;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import bjc.utils.data.IHolder;
import bjc.utils.data.IPair;
import bjc.utils.data.Pair;
/**
* A lazy holder of two values
*
* Lazy variant of {@link IPair}
*
* @author ben
*
* @param <L>
* The type of value stored on the left side of the pair
* @param <R>
* The type of value stored on the right side of the pair
*/
public class LazyPair<L, R> implements IPair<L, R> {
/**
* The backing store for this pair
*/
protected IHolder<IPair<L, R>> del;
/**
* Create a new blank lazy pair
*/
public LazyPair() {
del = new LazyHolder<>(new Pair<>());
}
/**
* Create a new lazy pair with the specified initial values
*
* @param leftVal
* The initial value for the left side of the pair
* @param rightVal
* The initial value for the right side of the pair
*/
public LazyPair(L leftVal, R rightVal) {
del = new LazyHolder<>(new Pair<>(leftVal, rightVal));
}
/**
* Create a new lazy pair with the specified sources for initial values
*
* @param leftValSrc
* The function to call for the left initial value
* @param rightValSrc
* The function to call for the right initial value
*/
public LazyPair(Supplier<L> leftValSrc, Supplier<R> rightValSrc) {
del = new LazyHolder<>(() -> {
return new Pair<>(leftValSrc.get(), rightValSrc.get());
});
}
/**
* Create a new lazy pair with a specified internal delegate
*
* @param deleg
* The internal delegate for the pair
*/
private LazyPair(IHolder<IPair<L, R>> deleg) {
del = deleg;
}
/*
* (non-Javadoc)
*
* @see bjc.utils.data.IPair#apply(java.util.function.Function,
* java.util.function.Function)
*/
@Override
public <L2, R2> IPair<L2, R2> apply(Function<L, L2> lf,
Function<R, R2> rf) {
IHolder<IPair<L2, R2>> newPair =
del.map(par -> par.apply(lf, rf));
return new LazyPair<>(newPair);
}
/*
* (non-Javadoc)
*
* @see bjc.utils.data.IPair#doWith(java.util.function.BiConsumer)
*/
@Override
public void doWith(BiConsumer<L, R> bc) {
del.doWith((par) -> {
par.doWith(bc);
});
}
/*
* (non-Javadoc)
*
* @see bjc.utils.data.IPair#merge(java.util.function.BiFunction)
*/
@Override
public <E> E merge(BiFunction<L, R, E> bf) {
return del.unwrap((par) -> par.merge(bf));
}
}
|