-
Notifications
You must be signed in to change notification settings - Fork 682
/
Copy pathLazy.java
294 lines (236 loc) · 7.18 KB
/
Lazy.java
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* Copyright 2016-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.util;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* Simple value type to delay the creation of an object using a {@link Supplier} returning the produced object for
* subsequent lookups. Note, that no concurrency control is applied during the lookup of {@link #get()}, which means in
* concurrent access scenarios, the provided {@link Supplier} can be called multiple times.
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Henning Rohlfs
* @author Johannes Englmeier
* @author Greg Turnquist
* @since 2.0
*/
public class Lazy<T> implements Supplier<T> {
private static final Lazy<?> EMPTY = new Lazy<>(() -> null, null, true);
static final String UNRESOLVED = "[Unresolved]";
private final Supplier<? extends T> supplier;
private @Nullable T value;
private volatile boolean resolved;
/**
* Creates a new {@link Lazy} instance for the given supplier.
*
* @param supplier
*/
private Lazy(Supplier<? extends T> supplier) {
this(supplier, null, false);
}
/**
* Creates a new {@link Lazy} for the given {@link Supplier}, value and whether it has been resolved or not.
*
* @param supplier must not be {@literal null}.
* @param value can be {@literal null}.
* @param resolved whether the value handed into the constructor represents a resolved value.
*/
private Lazy(Supplier<? extends T> supplier, @Nullable T value, boolean resolved) {
this.supplier = supplier;
this.value = value;
this.resolved = resolved;
}
/**
* Creates a new {@link Lazy} to produce an object lazily.
*
* @param <T> the type of which to produce an object of eventually.
* @param supplier the {@link Supplier} to create the object lazily.
* @return
*/
public static <T> Lazy<T> of(Supplier<? extends T> supplier) {
return new Lazy<>(supplier);
}
/**
* Creates a new {@link Lazy} to return the given value.
*
* @param <T> the type of the value to return eventually.
* @param value the value to return.
* @return
*/
public static <T> Lazy<T> of(T value) {
Assert.notNull(value, "Value must not be null");
return new Lazy<>(() -> value);
}
/**
* Creates a pre-resolved empty {@link Lazy}.
*
* @return
* @since 2.1
*/
@SuppressWarnings("unchecked")
public static <T> Lazy<T> empty() {
return (Lazy<T>) EMPTY;
}
/**
* Returns the value created by the configured {@link Supplier}. Will return the calculated instance for subsequent
* lookups.
*
* @return
*/
public T get() {
T value = getNullable();
if (value == null) {
throw new IllegalStateException("Expected lazy evaluation to yield a non-null value but got null");
}
return value;
}
/**
* Returns the {@link Optional} value created by the configured {@link Supplier}, allowing the absence of values in
* contrast to {@link #get()}. Will return the calculated instance for subsequent lookups.
*
* @return
*/
public Optional<T> getOptional() {
return Optional.ofNullable(getNullable());
}
/**
* Returns a new Lazy that will consume the given supplier in case the current one does not yield in a result.
*
* @param supplier must not be {@literal null}.
* @return
*/
public Lazy<T> or(Supplier<? extends T> supplier) {
Assert.notNull(supplier, "Supplier must not be null");
return Lazy.of(() -> orElseGet(supplier));
}
/**
* Returns a new Lazy that will return the given value in case the current one does not yield in a result.
*
* @param value must not be {@literal null}.
* @return
*/
public Lazy<T> or(T value) {
Assert.notNull(value, "Value must not be null");
return Lazy.of(() -> orElse(value));
}
/**
* Returns the value of the lazy computation or the given default value in case the computation yields
* {@literal null}.
*
* @param value
* @return
*/
@Nullable
public T orElse(@Nullable T value) {
T nullable = getNullable();
return nullable == null ? value : nullable;
}
/**
* Returns the value of the lazy computation or the value produced by the given {@link Supplier} in case the original
* value is {@literal null}.
*
* @param supplier must not be {@literal null}.
* @return
*/
@Nullable
public T orElseGet(Supplier<? extends T> supplier) {
Assert.notNull(supplier, "Default value supplier must not be null");
T value = getNullable();
return value == null ? supplier.get() : value;
}
/**
* Creates a new {@link Lazy} with the given {@link Function} lazily applied to the current one.
*
* @param function must not be {@literal null}.
* @return
*/
public <S> Lazy<S> map(Function<? super T, ? extends S> function) {
Assert.notNull(function, "Function must not be null");
return Lazy.of(() -> function.apply(get()));
}
/**
* Creates a new {@link Lazy} with the given {@link Function} lazily applied to the current one.
*
* @param function must not be {@literal null}.
* @return
*/
public <S> Lazy<S> flatMap(Function<? super T, Lazy<? extends S>> function) {
Assert.notNull(function, "Function must not be null");
return Lazy.of(() -> function.apply(get()).get());
}
/**
* Returns the {@link String} representation of the already resolved value or the one provided through the given
* {@link Supplier} if the value has not been resolved yet.
*
* @param fallback must not be {@literal null}.
* @return will never be {@literal null}.
* @since 3.0.1
*/
public String toString(Supplier<String> fallback) {
Assert.notNull(fallback, "Fallback must not be null!");
return resolved ? toString() : fallback.get();
}
/**
* Returns the value of the lazy evaluation.
*
* @return
* @since 2.2
*/
@Nullable
public T getNullable() {
if (resolved) {
return value;
}
this.value = supplier.get();
this.resolved = true;
return value;
}
@Override
public String toString() {
if (!resolved) {
return UNRESOLVED;
}
return value == null ? "null" : value.toString();
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Lazy<?> lazy)) {
return false;
}
if (resolved != lazy.resolved) {
return false;
}
if (!ObjectUtils.nullSafeEquals(supplier, lazy.supplier)) {
return false;
}
return ObjectUtils.nullSafeEquals(value, lazy.value);
}
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(supplier);
result = 31 * result + ObjectUtils.nullSafeHashCode(value);
result = 31 * result + (resolved ? 1 : 0);
return result;
}
}