2525
2626/**
2727 * 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.
3032 *
3133 * @author Oliver Gierke
3234 * @author Mark Paluch
@@ -45,17 +47,12 @@ public class Lazy<T> implements Supplier<T> {
4547 private @ Nullable T value ;
4648 private volatile boolean resolved ;
4749
48- /**
49- * Creates a new {@link Lazy} instance for the given supplier.
50- *
51- * @param supplier
52- */
5350 private Lazy (Supplier <? extends T > supplier ) {
5451 this (supplier , null , false );
5552 }
5653
5754 /**
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.
5956 *
6057 * @param supplier must not be {@literal null}.
6158 * @param value can be {@literal null}.
@@ -69,22 +66,22 @@ private Lazy(Supplier<? extends T> supplier, @Nullable T value, boolean resolved
6966 }
7067
7168 /**
72- * Creates a new {@link Lazy} to produce an object lazily.
69+ * Creates a new {@code Lazy} to produce an object lazily.
7370 *
7471 * @param <T> the type of which to produce an object of eventually.
7572 * @param supplier the {@link Supplier} to create the object lazily.
76- * @return
73+ * @return a {@code Lazy} wrapping the given {@link Supplier}.
7774 */
7875 public static <T > Lazy <T > of (Supplier <? extends T > supplier ) {
7976 return new Lazy <>(supplier );
8077 }
8178
8279 /**
83- * Creates a new {@link Lazy} to return the given value.
80+ * Creates a new {@code Lazy} to return the given value.
8481 *
8582 * @param <T> the type of the value to return eventually.
8683 * @param value the value to return.
87- * @return
84+ * @return a {@code Lazy} wrapping {@code value}.
8885 */
8986 public static <T > Lazy <T > of (T value ) {
9087
@@ -94,9 +91,9 @@ public static <T> Lazy<T> of(T value) {
9491 }
9592
9693 /**
97- * Creates a pre-resolved empty {@link Lazy}.
94+ * Creates a pre-resolved empty {@code Lazy}.
9895 *
99- * @return
96+ * @return an empty {@code Lazy}.
10097 * @since 2.1
10198 */
10299 @ SuppressWarnings ("unchecked" )
@@ -105,11 +102,12 @@ public static <T> Lazy<T> empty() {
105102 }
106103
107104 /**
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.
110106 *
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}.
112109 */
110+ @ Override
113111 public T get () {
114112
115113 T value = getNullable ();
@@ -121,63 +119,84 @@ public T get() {
121119 return value ;
122120 }
123121
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+
124141 /**
125142 * Returns the {@link Optional} value created by the configured {@link Supplier}, allowing the absence of values in
126143 * contrast to {@link #get()}. Will return the calculated instance for subsequent lookups.
127144 *
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}.
129147 */
130148 public Optional <T > getOptional () {
131149 return Optional .ofNullable (getNullable ());
132150 }
133151
134152 /**
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.
136154 *
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}.
139157 */
140- public Lazy <T > or (Supplier <? extends T > supplier ) {
158+ public Lazy <T > or (T other ) {
141159
142- Assert .notNull (supplier , "Supplier must not be null" );
160+ Assert .notNull (other , "Other value must not be null" );
143161
144- return Lazy .of (() -> orElseGet ( supplier ));
162+ return Lazy .of (() -> orElse ( other ));
145163 }
146164
147165 /**
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.
149167 *
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.
152171 */
153- public Lazy <T > or (T value ) {
172+ public Lazy <T > or (Supplier <? extends T > supplier ) {
154173
155- Assert .notNull (value , "Value must not be null" );
174+ Assert .notNull (supplier , "Supplier must not be null" );
156175
157- return Lazy .of (() -> orElse ( value ));
176+ return Lazy .of (() -> orElseGet ( supplier ));
158177 }
159178
160179 /**
161180 * Returns the value of the lazy computation or the given default value in case the computation yields
162181 * {@literal null}.
163182 *
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}.
166185 */
167186 @ Nullable
168- public T orElse (@ Nullable T value ) {
187+ public T orElse (@ Nullable T other ) {
169188
170189 T nullable = getNullable ();
171190
172- return nullable == null ? value : nullable ;
191+ return nullable == null ? other : nullable ;
173192 }
174193
175194 /**
176195 * Returns the value of the lazy computation or the value produced by the given {@link Supplier} in case the original
177196 * value is {@literal null}.
178197 *
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.
181200 */
182201 @ Nullable
183202 public T orElseGet (Supplier <? extends T > supplier ) {
@@ -190,10 +209,11 @@ public T orElseGet(Supplier<? extends T> supplier) {
190209 }
191210
192211 /**
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.
194213 *
195214 * @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.
197217 */
198218 public <S > Lazy <S > map (Function <? super T , ? extends S > function ) {
199219
@@ -203,10 +223,11 @@ public <S> Lazy<S> map(Function<? super T, ? extends S> function) {
203223 }
204224
205225 /**
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.
207227 *
208228 * @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.
210231 */
211232 public <S > Lazy <S > flatMap (Function <? super T , Lazy <? extends S >> function ) {
212233
@@ -215,50 +236,6 @@ public <S> Lazy<S> flatMap(Function<? super T, Lazy<? extends S>> function) {
215236 return Lazy .of (() -> function .apply (get ()).get ());
216237 }
217238
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-
262239 @ Override
263240 public boolean equals (@ Nullable Object o ) {
264241
@@ -291,4 +268,29 @@ public int hashCode() {
291268
292269 return result ;
293270 }
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+ }
294296}
0 commit comments