25
25
26
26
/**
27
27
* Simple value type to delay the creation of an object using a {@link Supplier} returning the produced object for
28
- * subsequent lookups. Note, that no concurrency control is applied during the lookup of {@link #get()}, which means in
29
- * concurrent access scenarios, the provided {@link Supplier} can be called multiple times.
28
+ * subsequent lookups.
29
+ * <p>
30
+ * Note, that no concurrency control is applied during the lookup of {@link #get()}, which means in concurrent access
31
+ * scenarios, the provided {@link Supplier} can be called multiple times.
30
32
*
31
33
* @author Oliver Gierke
32
34
* @author Mark Paluch
@@ -45,17 +47,12 @@ public class Lazy<T> implements Supplier<T> {
45
47
private @ Nullable T value ;
46
48
private volatile boolean resolved ;
47
49
48
- /**
49
- * Creates a new {@link Lazy} instance for the given supplier.
50
- *
51
- * @param supplier
52
- */
53
50
private Lazy (Supplier <? extends T > supplier ) {
54
51
this (supplier , null , false );
55
52
}
56
53
57
54
/**
58
- * Creates a new {@link Lazy} for the given {@link Supplier}, value and whether it has been resolved or not.
55
+ * Creates a new {@code Lazy} for the given {@link Supplier}, value and whether it has been resolved or not.
59
56
*
60
57
* @param supplier must not be {@literal null}.
61
58
* @param value can be {@literal null}.
@@ -69,22 +66,22 @@ private Lazy(Supplier<? extends T> supplier, @Nullable T value, boolean resolved
69
66
}
70
67
71
68
/**
72
- * Creates a new {@link Lazy} to produce an object lazily.
69
+ * Creates a new {@code Lazy} to produce an object lazily.
73
70
*
74
71
* @param <T> the type of which to produce an object of eventually.
75
72
* @param supplier the {@link Supplier} to create the object lazily.
76
- * @return
73
+ * @return a {@code Lazy} wrapping the given {@link Supplier}.
77
74
*/
78
75
public static <T > Lazy <T > of (Supplier <? extends T > supplier ) {
79
76
return new Lazy <>(supplier );
80
77
}
81
78
82
79
/**
83
- * Creates a new {@link Lazy} to return the given value.
80
+ * Creates a new {@code Lazy} to return the given value.
84
81
*
85
82
* @param <T> the type of the value to return eventually.
86
83
* @param value the value to return.
87
- * @return
84
+ * @return a {@code Lazy} wrapping {@code value}.
88
85
*/
89
86
public static <T > Lazy <T > of (T value ) {
90
87
@@ -94,9 +91,9 @@ public static <T> Lazy<T> of(T value) {
94
91
}
95
92
96
93
/**
97
- * Creates a pre-resolved empty {@link Lazy}.
94
+ * Creates a pre-resolved empty {@code Lazy}.
98
95
*
99
- * @return
96
+ * @return an empty {@code Lazy}.
100
97
* @since 2.1
101
98
*/
102
99
@ SuppressWarnings ("unchecked" )
@@ -105,11 +102,12 @@ public static <T> Lazy<T> empty() {
105
102
}
106
103
107
104
/**
108
- * Returns the value created by the configured {@link Supplier}. Will return the calculated instance for subsequent
109
- * lookups.
105
+ * Returns the value created by the configured {@link Supplier}. Will return the same instance for subsequent lookups.
110
106
*
111
- * @return
107
+ * @return the value created by the configured {@link Supplier}. Will return the same instance for subsequent lookups.
108
+ * @throws IllegalStateException if the resolved value is {@literal null}.
112
109
*/
110
+ @ Override
113
111
public T get () {
114
112
115
113
T value = getNullable ();
@@ -121,63 +119,84 @@ public T get() {
121
119
return value ;
122
120
}
123
121
122
+ /**
123
+ * Returns the value of the lazy evaluation.
124
+ *
125
+ * @return the value of the lazy evaluation, can be {@literal null}.
126
+ * @since 2.2
127
+ */
128
+ @ Nullable
129
+ public T getNullable () {
130
+
131
+ if (resolved ) {
132
+ return value ;
133
+ }
134
+
135
+ this .value = supplier .get ();
136
+ this .resolved = true ;
137
+
138
+ return value ;
139
+ }
140
+
124
141
/**
125
142
* Returns the {@link Optional} value created by the configured {@link Supplier}, allowing the absence of values in
126
143
* contrast to {@link #get()}. Will return the calculated instance for subsequent lookups.
127
144
*
128
- * @return
145
+ * @return an {@link Optional} value created by the configured {@link Supplier} or an empty {@link Optional} if the
146
+ * resolved value is {@literal null}.
129
147
*/
130
148
public Optional <T > getOptional () {
131
149
return Optional .ofNullable (getNullable ());
132
150
}
133
151
134
152
/**
135
- * Returns a new Lazy that will consume the given supplier in case the current one does not yield in a result.
153
+ * Returns a new {@code Lazy} that will return the given value in case the current one does not yield in a result.
136
154
*
137
- * @param supplier must not be {@literal null}.
138
- * @return
155
+ * @param other must not be {@literal null}.
156
+ * @return a new {@code Lazy} that will yield its value if present, otherwise {@code other}.
139
157
*/
140
- public Lazy <T > or (Supplier <? extends T > supplier ) {
158
+ public Lazy <T > or (T other ) {
141
159
142
- Assert .notNull (supplier , "Supplier must not be null" );
160
+ Assert .notNull (other , "Other value must not be null" );
143
161
144
- return Lazy .of (() -> orElseGet ( supplier ));
162
+ return Lazy .of (() -> orElse ( other ));
145
163
}
146
164
147
165
/**
148
- * Returns a new Lazy that will return the given value in case the current one does not yield in a result.
166
+ * Returns a new {@code Lazy} that will consume the given supplier in case the current one does not yield in a result.
149
167
*
150
- * @param value must not be {@literal null}.
151
- * @return
168
+ * @param supplier the supplying function that produces a value to be returned, must not be {@literal null}.
169
+ * @return a new {@code Lazy} that will yield its value if present, otherwise the result produced by the supplying
170
+ * function.
152
171
*/
153
- public Lazy <T > or (T value ) {
172
+ public Lazy <T > or (Supplier <? extends T > supplier ) {
154
173
155
- Assert .notNull (value , "Value must not be null" );
174
+ Assert .notNull (supplier , "Supplier must not be null" );
156
175
157
- return Lazy .of (() -> orElse ( value ));
176
+ return Lazy .of (() -> orElseGet ( supplier ));
158
177
}
159
178
160
179
/**
161
180
* Returns the value of the lazy computation or the given default value in case the computation yields
162
181
* {@literal null}.
163
182
*
164
- * @param value
165
- * @return
183
+ * @param other the value to be returned, if no value is present. May be {@literal null}.
184
+ * @return the value, if present, otherwise {@code other}.
166
185
*/
167
186
@ Nullable
168
- public T orElse (@ Nullable T value ) {
187
+ public T orElse (@ Nullable T other ) {
169
188
170
189
T nullable = getNullable ();
171
190
172
- return nullable == null ? value : nullable ;
191
+ return nullable == null ? other : nullable ;
173
192
}
174
193
175
194
/**
176
195
* Returns the value of the lazy computation or the value produced by the given {@link Supplier} in case the original
177
196
* value is {@literal null}.
178
197
*
179
- * @param supplier must not be {@literal null}.
180
- * @return
198
+ * @param supplier the supplying function that produces a value to be returned, must not be {@literal null}.
199
+ * @return the value, if present, otherwise the result produced by the supplying function.
181
200
*/
182
201
@ Nullable
183
202
public T orElseGet (Supplier <? extends T > supplier ) {
@@ -190,10 +209,11 @@ public T orElseGet(Supplier<? extends T> supplier) {
190
209
}
191
210
192
211
/**
193
- * Creates a new {@link Lazy} with the given {@link Function} lazily applied to the current one.
212
+ * Creates a new {@code Lazy} with the given {@link Function} lazily applied to the current one.
194
213
*
195
214
* @param function must not be {@literal null}.
196
- * @return
215
+ * @return an {@code Lazy} describing the result of applying a mapping function to the value of this {@code Lazy} or
216
+ * throwing {@link IllegalStateException} if the {@code Lazy} is empty.
197
217
*/
198
218
public <S > Lazy <S > map (Function <? super T , ? extends S > function ) {
199
219
@@ -203,10 +223,11 @@ public <S> Lazy<S> map(Function<? super T, ? extends S> function) {
203
223
}
204
224
205
225
/**
206
- * Creates a new {@link Lazy} with the given {@link Function} lazily applied to the current one.
226
+ * Creates a new {@code Lazy} with the given {@link Function} lazily applied to the current one.
207
227
*
208
228
* @param function must not be {@literal null}.
209
- * @return
229
+ * @return the result of applying an {@code Lazy}-bearing mapping function to the value of this {@code Lazy} or a
230
+ * {@code Lazy} throwing {@link IllegalStateException} if the {@code Lazy} is empty.
210
231
*/
211
232
public <S > Lazy <S > flatMap (Function <? super T , Lazy <? extends S >> function ) {
212
233
@@ -215,50 +236,6 @@ public <S> Lazy<S> flatMap(Function<? super T, Lazy<? extends S>> function) {
215
236
return Lazy .of (() -> function .apply (get ()).get ());
216
237
}
217
238
218
- /**
219
- * Returns the {@link String} representation of the already resolved value or the one provided through the given
220
- * {@link Supplier} if the value has not been resolved yet.
221
- *
222
- * @param fallback must not be {@literal null}.
223
- * @return will never be {@literal null}.
224
- * @since 3.0.1
225
- */
226
- public String toString (Supplier <String > fallback ) {
227
-
228
- Assert .notNull (fallback , "Fallback must not be null!" );
229
-
230
- return resolved ? toString () : fallback .get ();
231
- }
232
-
233
- /**
234
- * Returns the value of the lazy evaluation.
235
- *
236
- * @return
237
- * @since 2.2
238
- */
239
- @ Nullable
240
- public T getNullable () {
241
-
242
- if (resolved ) {
243
- return value ;
244
- }
245
-
246
- this .value = supplier .get ();
247
- this .resolved = true ;
248
-
249
- return value ;
250
- }
251
-
252
- @ Override
253
- public String toString () {
254
-
255
- if (!resolved ) {
256
- return UNRESOLVED ;
257
- }
258
-
259
- return value == null ? "null" : value .toString ();
260
- }
261
-
262
239
@ Override
263
240
public boolean equals (@ Nullable Object o ) {
264
241
@@ -291,4 +268,29 @@ public int hashCode() {
291
268
292
269
return result ;
293
270
}
271
+
272
+ @ Override
273
+ public String toString () {
274
+
275
+ if (!resolved ) {
276
+ return UNRESOLVED ;
277
+ }
278
+
279
+ return value == null ? "null" : value .toString ();
280
+ }
281
+
282
+ /**
283
+ * Returns the {@link String} representation of the already resolved value or the one provided through the given
284
+ * {@link Supplier} if the value has not been resolved yet.
285
+ *
286
+ * @param fallback must not be {@literal null}.
287
+ * @return will never be {@literal null}.
288
+ * @since 3.0.1
289
+ */
290
+ public String toString (Supplier <String > fallback ) {
291
+
292
+ Assert .notNull (fallback , "Fallback must not be null!" );
293
+
294
+ return resolved ? toString () : fallback .get ();
295
+ }
294
296
}
0 commit comments