Skip to content

Commit b417268

Browse files
committed
Improve Lazy.toString().
Added dedicated Lazy.toString() rendering the resolved value's ….toString() method but resorts to a constant [Unresolved] if it's not already resolved. An additional ….toString(Supplier<String>) allows to customize the fallback message if needed. Fixes #2751.
1 parent c25861b commit b417268

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/main/java/org/springframework/data/util/Lazy.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
public class Lazy<T> implements Supplier<T> {
3838

3939
private static final Lazy<?> EMPTY = new Lazy<>(() -> null, null, true);
40+
static final String UNRESOLVED = "[Unresolved]";
4041

4142
private final Supplier<? extends T> supplier;
4243

@@ -213,6 +214,21 @@ public <S> Lazy<S> flatMap(Function<? super T, Lazy<? extends S>> function) {
213214
return Lazy.of(() -> function.apply(get()).get());
214215
}
215216

217+
/**
218+
* Returns the {@link String} representation of the already resolved value or the one provided through the given
219+
* {@link Supplier} if the value has not been resolved yet.
220+
*
221+
* @param fallback must not be {@literal null}.
222+
* @return will never be {@literal null}.
223+
* @since 3.0.1
224+
*/
225+
public String toString(Supplier<String> fallback) {
226+
227+
Assert.notNull(fallback, "Fallback must not be null!");
228+
229+
return resolved ? toString() : fallback.get();
230+
}
231+
216232
/**
217233
* Returns the value of the lazy evaluation.
218234
*
@@ -232,6 +248,16 @@ public T getNullable() {
232248
return value;
233249
}
234250

251+
@Override
252+
public String toString() {
253+
254+
if (!resolved) {
255+
return UNRESOLVED;
256+
}
257+
258+
return value == null ? "null" : value.toString();
259+
}
260+
235261
@Override
236262
public boolean equals(@Nullable Object o) {
237263

src/test/java/org/springframework/data/util/LazyUnitTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,35 @@ void returnsElseValue() {
116116
assertThat(empty.or(reference).get()).isEqualTo(reference);
117117
assertThat(empty.or(() -> reference).get()).isEqualTo(reference);
118118
}
119+
120+
@Test // #2751
121+
void doesNotResolveValueForToString() {
122+
123+
Supplier<Object> mock = mock(Supplier.class);
124+
125+
var lazy = Lazy.of(mock);
126+
127+
assertThat(lazy.toString()).isEqualTo(Lazy.UNRESOLVED);
128+
verify(mock, never()).get();
129+
130+
lazy.getNullable();
131+
132+
assertThat(lazy.toString()).isNotEqualTo(Lazy.UNRESOLVED);
133+
}
134+
135+
@Test // #2751
136+
void usesFallbackForToStringOnUnresolvedInstance() {
137+
138+
Supplier<Object> mock = mock(Supplier.class);
139+
140+
var lazy = Lazy.of(mock);
141+
var fallback = "fallback";
142+
143+
assertThat(lazy.toString(() -> fallback)).isEqualTo(fallback);
144+
verify(mock, never()).get();
145+
146+
lazy.getNullable();
147+
148+
assertThat(lazy.toString(() -> fallback)).isNotEqualTo(fallback);
149+
}
119150
}

0 commit comments

Comments
 (0)