Skip to content

Commit 73ed742

Browse files
klueverGuice Team
authored and
Guice Team
committed
Encourage a lambda over Provider.of(T).
PiperOrigin-RevId: 751215993
1 parent 33232b4 commit 73ed742

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

core/src/com/google/inject/util/Providers.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,22 @@
3939
@CheckReturnValue
4040
public final class Providers {
4141

42-
private Providers() {}
43-
4442
/**
4543
* Returns a provider which always provides {@code instance}. This should not be necessary to use
4644
* in your application, but is helpful for several types of unit tests.
4745
*
46+
* <p><b>Java 8+ users:</b> prefer {@code () -> instance}. However, note the following
47+
* differences:
48+
*
49+
* <ul>
50+
* <li>Lambdas will delay evaluation of the instance. {@code () -> computeValue()} will be
51+
* computed when {@code provider.get()} is called, but {@code Providers.of(computeValue())}
52+
* will be computed immediately.
53+
* <li>Lambdas do not implement {@code equals()} or {@code hashCode()}, so avoid putting them in
54+
* a collection.
55+
* <li>Lambdas do not implement a useful {@code toString()}.
56+
* </ul>
57+
*
4858
* @param instance the instance that should always be provided. This is also permitted to be null,
4959
* to enable aggressive testing, although in real life a Guice-supplied Provider will never
5060
* return null.
@@ -173,4 +183,6 @@ public Set<Dependency<?>> getDependencies() {
173183
return dependencies;
174184
}
175185
}
186+
187+
private Providers() {}
176188
}

0 commit comments

Comments
 (0)