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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/*
* esodata - data structures and other things, of varying utility
* Copyright 2022, Ben Culkin
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package bjc.data.internals;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import bjc.data.Holder;
import bjc.data.Pair;
import bjc.data.Identity;
import bjc.data.LazyPair;
/**
* Implements a lazy pair that has been bound.
*
* @author Ben Culkin
*/
@SuppressWarnings("javadoc")
public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight>
implements Pair<NewLeft, NewRight> {
/* The supplier of the left value. */
private final Supplier<OldLeft> leftSupplier;
/* The supplier of the right value. */
private final Supplier<OldRight> rightSupplier;
/* The binder to transform values. */
private final BiFunction<OldLeft, OldRight, Pair<NewLeft, NewRight>> binder;
/* The bound pair. */
private Pair<NewLeft, NewRight> boundPair;
/* Whether the pair has been bound yet. */
private boolean pairBound;
/**
* Create a new bound lazy pair.
*
* @param leftSupp
* The supplier for the left value.
*
* @param rightSupp
* The supplier for the right value.
*
* @param bindr
* The function to use to bind the left and right into a new
* pair.
*/
public BoundLazyPair(final Supplier<OldLeft> leftSupp,
final Supplier<OldRight> rightSupp,
final BiFunction<OldLeft, OldRight, Pair<NewLeft, NewRight>> bindr) {
leftSupplier = leftSupp;
rightSupplier = rightSupp;
binder = bindr;
}
@Override
public <BoundLeft, BoundRight> Pair<BoundLeft, BoundRight> bind(
final BiFunction<NewLeft, NewRight, Pair<BoundLeft, BoundRight>> bindr) {
if (bindr == null)
throw new NullPointerException("Binder must not be null");
final Holder<Pair<NewLeft, NewRight>> newPair = new Identity<>(boundPair);
final Holder<Boolean> newPairMade = new Identity<>(pairBound);
final Supplier<NewLeft> leftSupp = () -> {
if (!newPairMade.getValue()) {
/*
* If the pair hasn't been bound before, bind it.
*/
newPair.replace(binder.apply(leftSupplier.get(), rightSupplier.get()));
newPairMade.replace(true);
}
return newPair.unwrap(pair -> pair.getLeft());
};
final Supplier<NewRight> rightSupp = () -> {
if (!newPairMade.getValue()) {
/*
* If the pair hasn't been bound before, bind it.
*/
newPair.replace(binder.apply(leftSupplier.get(), rightSupplier.get()));
newPairMade.replace(true);
}
return newPair.unwrap(pair -> pair.getRight());
};
return new BoundLazyPair<>(leftSupp, rightSupp, bindr);
}
@Override
public <BoundLeft> Pair<BoundLeft, NewRight>
bindLeft(final Function<NewLeft, Pair<BoundLeft, NewRight>> leftBinder) {
if (leftBinder == null)
throw new NullPointerException("Left binder must not be null");
final Supplier<NewLeft> leftSupp = () -> {
Pair<NewLeft, NewRight> newPair = boundPair;
if (!pairBound) {
/*
* If the pair hasn't been bound before, bind it.
*/
newPair = binder.apply(leftSupplier.get(), rightSupplier.get());
}
return newPair.getLeft();
};
return new HalfBoundLazyPair<>(leftSupp, leftBinder);
}
@Override
public <BoundRight> Pair<NewLeft, BoundRight>
bindRight(final Function<NewRight, Pair<NewLeft, BoundRight>> rightBinder) {
if (rightBinder == null)
throw new NullPointerException("Right binder must not be null");
final Supplier<NewRight> rightSupp = () -> {
Pair<NewLeft, NewRight> newPair = boundPair;
if (!pairBound) {
/*
* If the pair hasn't been bound before, bind it.
*/
newPair = binder.apply(leftSupplier.get(), rightSupplier.get());
}
return newPair.getRight();
};
return new HalfBoundLazyPair<>(rightSupp, rightBinder);
}
@Override
public <OtherLeft, OtherRight, CombinedLeft, CombinedRight>
Pair<CombinedLeft, CombinedRight>
combine(final Pair<OtherLeft, OtherRight> otherPair,
final BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner,
final BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) {
if (otherPair == null) {
throw new NullPointerException("Other pair must not be null");
} else if (leftCombiner == null) {
throw new NullPointerException("Left combiner must not be null");
} else if (rightCombiner == null) {
throw new NullPointerException("Right combiner must not be null");
}
return otherPair.bind((otherLeft, otherRight) -> bind((leftVal, rightVal) -> {
CombinedLeft cLeft = leftCombiner.apply(leftVal, otherLeft);
CombinedRight cRight = rightCombiner.apply(rightVal, otherRight);
return new LazyPair<>(cLeft, cRight);
}));
}
@Override
public <NewLeftType> Pair<NewLeftType, NewRight>
mapLeft(final Function<NewLeft, NewLeftType> mapper) {
if (mapper == null)
throw new NullPointerException("Mapper must not be null");
final Supplier<NewLeftType> leftSupp = () -> {
if (!pairBound) {
final NewLeft leftVal
= binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft();
return mapper.apply(leftVal);
}
return mapper.apply(boundPair.getLeft());
};
final Supplier<NewRight> rightSupp = () -> {
if (!pairBound)
return binder.apply(leftSupplier.get(), rightSupplier.get()).getRight();
return boundPair.getRight();
};
return new LazyPair<>(leftSupp, rightSupp);
}
@Override
public <NewRightType> Pair<NewLeft, NewRightType>
mapRight(final Function<NewRight, NewRightType> mapper) {
if (mapper == null)
throw new NullPointerException("Mapper must not be null");
final Supplier<NewLeft> leftSupp = () -> {
if (!pairBound)
return binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft();
return boundPair.getLeft();
};
final Supplier<NewRightType> rightSupp = () -> {
if (!pairBound) {
final NewRight rightVal = binder
.apply(leftSupplier.get(), rightSupplier.get()).getRight();
return mapper.apply(rightVal);
}
return mapper.apply(boundPair.getRight());
};
return new LazyPair<>(leftSupp, rightSupp);
}
@Override
public <MergedType> MergedType
merge(final BiFunction<NewLeft, NewRight, MergedType> merger) {
if (merger == null)
throw new NullPointerException("Merger must not be null");
if (!pairBound) {
/*
* If the pair isn't bound yet, bind it.
*/
boundPair = binder.apply(leftSupplier.get(), rightSupplier.get());
pairBound = true;
}
return boundPair.merge(merger);
}
@Override
public String toString() {
if (pairBound)
return boundPair.toString();
return "(un-materialized)";
}
}
|