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
|
/*
* 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.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import bjc.data.Holder;
import bjc.data.Lazy;
import bjc.funcdata.FunctionalList;
import bjc.funcdata.ListEx;
/**
* Implements a lazy holder that has been bound.
*
* @author Ben Culkin
* @param <OldType>
* The old type of the value.
* @param <BoundContainedType>
* The type of the new bound value.
*/
public class BoundLazy<OldType, BoundContainedType>
implements Holder<BoundContainedType> {
/* The old value. */
private final Supplier<Holder<OldType>> oldSupplier;
/* The function to use to transform the old value into a new value. */
private final Function<OldType, Holder<BoundContainedType>> binder;
/* The bound value being held. */
private Holder<BoundContainedType> boundHolder;
/* Whether the bound value has been actualized or not. */
private boolean holderBound;
/* Transformations currently pending on the bound value. */
private final ListEx<UnaryOperator<BoundContainedType>> actions
= new FunctionalList<>();
/**
* Create a new bound lazy value.
*
* @param supp
* The supplier of the old value.
*
* @param binder
* The function to use to bind the old value to the new one.
*/
public BoundLazy(final Supplier<Holder<OldType>> supp,
final Function<OldType, Holder<BoundContainedType>> binder) {
oldSupplier = supp;
this.binder = binder;
}
@Override
public <BoundType> Holder<BoundType>
bind(final Function<BoundContainedType, Holder<BoundType>> bindr) {
if (bindr == null)
throw new NullPointerException("Binder must not be null");
/* Prepare a list of pending actions. */
final ListEx<UnaryOperator<BoundContainedType>> pendingActions
= new FunctionalList<>();
for (UnaryOperator<BoundContainedType> pendAct : actions) {
pendingActions.add(pendAct);
}
/* Create the new supplier of a value. */
final Supplier<Holder<BoundContainedType>> typeSupplier = () -> {
Holder<BoundContainedType> oldHolder = boundHolder;
/* Bind the value if it hasn't been bound before. */
if (!holderBound) {
oldHolder = oldSupplier.get().unwrap(binder);
}
/* Apply all the pending actions. */
return pendingActions.reduceAux(oldHolder,
(action, state) -> state.transform(action), value -> value);
};
return new BoundLazy<>(typeSupplier, bindr);
}
@Override
public <NewType> Function<BoundContainedType, Holder<NewType>>
lift(final Function<BoundContainedType, NewType> func) {
if (func == null)
throw new NullPointerException("Function to lift must not be null");
return val -> {
return new Lazy<>(func.apply(val));
};
}
@Override
public <MappedType> Holder<MappedType>
map(final Function<BoundContainedType, MappedType> mapper) {
if (mapper == null)
throw new NullPointerException("Mapper must not be null");
/* Prepare a list of pending actions. */
final ListEx<UnaryOperator<BoundContainedType>> pendingActions
= new FunctionalList<>();
for (UnaryOperator<BoundContainedType> pendAct : actions) {
pendingActions.add(pendAct);
}
/* Prepare the new supplier. */
final Supplier<MappedType> typeSupplier = () -> {
Holder<BoundContainedType> oldHolder = boundHolder;
/* Bound the value if it hasn't been bound. */
if (!holderBound) {
oldHolder = oldSupplier.get().unwrap(binder);
}
/* Apply pending actions. */
return pendingActions.reduceAux(oldHolder.getValue(),
(action, state) -> action.apply(state), value -> mapper.apply(value));
};
return new Lazy<>(typeSupplier);
}
@Override
public String toString() {
if (holderBound)
return boundHolder.toString();
return "(unmaterialized)";
}
@Override
public Holder<BoundContainedType>
transform(final UnaryOperator<BoundContainedType> transformer) {
if (transformer == null)
throw new NullPointerException("Transformer must not be null");
actions.add(transformer);
return this;
}
@Override
public <UnwrappedType> UnwrappedType
unwrap(final Function<BoundContainedType, UnwrappedType> unwrapper) {
if (unwrapper == null)
throw new NullPointerException("Unwrapper must not be null");
if (!holderBound) {
boundHolder = oldSupplier.get().unwrap(binder::apply);
}
return boundHolder.unwrap(unwrapper);
}
}
|