From 28fbf0859c63b2a30555b3295fe738c263c0db6b Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 10 Jul 2015 21:45:41 -0400 Subject: lazy: Fix LazyBinder de-duplication. --- .../java/com/tavianator/sangria/lazy/LazyBinder.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'sangria-lazy/src/main/java/com/tavianator/sangria') 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(); + } } } -- cgit v1.2.3 From 8c7d863828e0bf4b9045075ff3ccd386993dfbb5 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 10 Jul 2015 22:01:30 -0400 Subject: lazy: Fix Lazy when a provider returns null. --- .../java/com/tavianator/sangria/lazy/Lazy.java | 11 +++++---- .../java/com/tavianator/sangria/lazy/LazyTest.java | 27 +++++++++++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) (limited to 'sangria-lazy/src/main/java/com/tavianator/sangria') 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 { + private static final Object SENTINEL = new Object(); + private final Provider provider; - private volatile T instance = null; + private volatile Object instance = SENTINEL; @Inject Lazy(Provider provider) { @@ -67,15 +69,16 @@ public final class Lazy { /** * @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/test/java/com/tavianator/sangria/lazy/LazyTest.java b/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazyTest.java index ee6200a..a451a96 100644 --- a/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazyTest.java +++ b/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazyTest.java @@ -36,6 +36,7 @@ import com.google.inject.Provider; import com.google.inject.spi.DefaultBindingTargetVisitor; import com.google.inject.spi.Element; import com.google.inject.spi.Elements; +import com.google.inject.util.Providers; import org.junit.Test; import static com.tavianator.sangria.test.SangriaMatchers.*; @@ -62,15 +63,26 @@ public class LazyTest { @Singleton private static class CountingProvider implements Provider { int count = 0; + private final Provider impl; @Inject CountingProvider() { + this.impl = new Provider() { + @Override + public Abstract get() { + return new Abstract() { }; + } + }; + } + + CountingProvider(Abstract instance) { + this.impl = Providers.of(instance); } @Override public Abstract get() { ++count; - return new Abstract() { }; + return impl.get(); } } @@ -186,6 +198,19 @@ public class LazyTest { assertThat(module, followsBestPractices()); } + @Test + public void testNull() { + Module module = new AbstractModule() { + @Override + protected void configure() { + bind(CountingProvider.class) + .toInstance(new CountingProvider(null)); + } + }; + + test(Guice.createInjector(module), HasLazy.class); + } + @Test(expected = CreationException.class) public void testMissingBinding() { Guice.createInjector(new AbstractModule() { -- cgit v1.2.3