summaryrefslogtreecommitdiffstats
path: root/sangria-lazy/src/main/java/com/tavianator/sangria
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2015-07-10 22:03:43 -0400
committerTavian Barnes <tavianator@tavianator.com>2015-07-10 22:03:43 -0400
commitbfbf7f869a6e557906f3f4269b52fff57a9c8e4a (patch)
tree4c8c374d9abb26fea0d9c65a39a8e098c6f260d3 /sangria-lazy/src/main/java/com/tavianator/sangria
parentebbda6f93d4439698727cbf463b20e74a64a03f9 (diff)
parent8c7d863828e0bf4b9045075ff3ccd386993dfbb5 (diff)
downloadsangria-bfbf7f869a6e557906f3f4269b52fff57a9c8e4a.tar.xz
Merge branch 'lazy-fixes'
Diffstat (limited to 'sangria-lazy/src/main/java/com/tavianator/sangria')
-rw-r--r--sangria-lazy/src/main/java/com/tavianator/sangria/lazy/Lazy.java11
-rw-r--r--sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBinder.java17
2 files changed, 24 insertions, 4 deletions
diff --git a/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/Lazy.java b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/Lazy.java
index adf531c..ee42a2f 100644
--- a/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/Lazy.java
+++ b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/Lazy.java
@@ -56,8 +56,10 @@ import javax.inject.Provider;
* @since 1.2
*/
public final class Lazy<T> {
+ private static final Object SENTINEL = new Object();
+
private final Provider<T> provider;
- private volatile T instance = null;
+ private volatile Object instance = SENTINEL;
@Inject
Lazy(Provider<T> provider) {
@@ -67,15 +69,16 @@ public final class Lazy<T> {
/**
* @return A lazily-produced value of type {@code T}.
*/
+ @SuppressWarnings("unchecked")
public T get() {
// Double-checked locking
- if (instance == null) {
+ if (instance == SENTINEL) {
synchronized (this) {
- if (instance == null) {
+ if (instance == SENTINEL) {
instance = provider.get();
}
}
}
- return instance;
+ return (T) instance;
}
}
diff --git a/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBinder.java b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBinder.java
index 26d3848..baad5da 100644
--- a/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBinder.java
+++ b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBinder.java
@@ -269,5 +269,22 @@ public class LazyBinder {
return visitor.visit(binding);
}
}
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ } else if (!(obj instanceof LazyProvider)) {
+ return false;
+ }
+
+ LazyProvider<?> other = (LazyProvider<?>) obj;
+ return key.equals(other.key);
+ }
+
+ @Override
+ public int hashCode() {
+ return key.hashCode();
+ }
}
}