From b5bc672aa1367a34e2939584cdf35964d369cac5 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 14 Sep 2014 20:32:29 -0400 Subject: Bump the version to 1.2-SNAPSHOT. --- pom.xml | 12 ++++++------ sangria-contextual/pom.xml | 2 +- sangria-core/pom.xml | 2 +- sangria-listbinder/pom.xml | 2 +- sangria-log4j/pom.xml | 2 +- sangria-slf4j/pom.xml | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index ff82b02..5279ece 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.tavianator.sangria sangria - 1.1-SNAPSHOT + 1.2-SNAPSHOT pom Sangria @@ -51,31 +51,31 @@ com.tavianator.sangria sangria-core - 1.1-SNAPSHOT + ${project.version} com.tavianator.sangria sangria-contextual - 1.1-SNAPSHOT + ${project.version} com.tavianator.sangria sangria-slf4j - 1.1-SNAPSHOT + ${project.version} com.tavianator.sangria sangria-log4j - 1.1-SNAPSHOT + ${project.version} com.tavianator.sangria sangria-listbinder - 1.1-SNAPSHOT + ${project.version} diff --git a/sangria-contextual/pom.xml b/sangria-contextual/pom.xml index 54d0a36..c939076 100644 --- a/sangria-contextual/pom.xml +++ b/sangria-contextual/pom.xml @@ -7,7 +7,7 @@ com.tavianator.sangria sangria - 1.1-SNAPSHOT + 1.2-SNAPSHOT sangria-contextual diff --git a/sangria-core/pom.xml b/sangria-core/pom.xml index 8c58c49..c06b186 100644 --- a/sangria-core/pom.xml +++ b/sangria-core/pom.xml @@ -7,7 +7,7 @@ com.tavianator.sangria sangria - 1.1-SNAPSHOT + 1.2-SNAPSHOT sangria-core diff --git a/sangria-listbinder/pom.xml b/sangria-listbinder/pom.xml index 5b04e5e..4040c4a 100644 --- a/sangria-listbinder/pom.xml +++ b/sangria-listbinder/pom.xml @@ -7,7 +7,7 @@ com.tavianator.sangria sangria - 1.1-SNAPSHOT + 1.2-SNAPSHOT sangria-listbinder diff --git a/sangria-log4j/pom.xml b/sangria-log4j/pom.xml index 5dae892..f05b454 100644 --- a/sangria-log4j/pom.xml +++ b/sangria-log4j/pom.xml @@ -7,7 +7,7 @@ com.tavianator.sangria sangria - 1.1-SNAPSHOT + 1.2-SNAPSHOT sangria-log4j diff --git a/sangria-slf4j/pom.xml b/sangria-slf4j/pom.xml index a722eb8..c274a47 100644 --- a/sangria-slf4j/pom.xml +++ b/sangria-slf4j/pom.xml @@ -7,7 +7,7 @@ com.tavianator.sangria sangria - 1.1-SNAPSHOT + 1.2-SNAPSHOT sangria-slf4j -- cgit v1.2.3 From bc1de44c602192e59ff66d1a5d1f1e9cdcc4a2bb Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 14 Sep 2014 23:26:43 -0400 Subject: core: Add PotentialAnnotation.from(Key) method. --- .../sangria/core/PotentialAnnotation.java | 70 +++++++++++++++++++++- .../sangria/core/PotentialAnnotationTest.java | 18 ++++-- 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/sangria-core/src/main/java/com/tavianator/sangria/core/PotentialAnnotation.java b/sangria-core/src/main/java/com/tavianator/sangria/core/PotentialAnnotation.java index 302e5e1..7c3680f 100644 --- a/sangria-core/src/main/java/com/tavianator/sangria/core/PotentialAnnotation.java +++ b/sangria-core/src/main/java/com/tavianator/sangria/core/PotentialAnnotation.java @@ -29,7 +29,7 @@ import com.google.inject.spi.Message; * A record of stored annotations, perfect for builders with {@code annotatedWith()} methods. * * @author Tavian Barnes (tavianator@tavianator.com) - * @version 1.1 + * @version 1.2 * @since 1.1 */ public abstract class PotentialAnnotation { @@ -75,6 +75,24 @@ public abstract class PotentialAnnotation { return NONE; } + /** + * @return A {@link PotentialAnnotation} with the annotation from a {@link Key}. + * @since 1.2 + */ + public static PotentialAnnotation from(Key key) { + Annotation instance = key.getAnnotation(); + if (instance != null) { + return none().annotatedWith(instance); + } + + Class type = key.getAnnotationType(); + if (type != null) { + return none().annotatedWith(type); + } + + return none(); + } + private PotentialAnnotation() { } @@ -139,6 +157,12 @@ public abstract class PotentialAnnotation { */ public abstract T accept(Visitor visitor); + @Override + public abstract boolean equals(Object o); + + @Override + public abstract int hashCode(); + @Override public abstract String toString(); @@ -171,6 +195,16 @@ public abstract class PotentialAnnotation { return visitor.visitNoAnnotation(); } + @Override + public boolean equals(Object o) { + return o == this || o instanceof NoAnnotation; + } + + @Override + public int hashCode() { + return NoAnnotation.class.hashCode(); + } + @Override public String toString() { return "[no annotation]"; @@ -202,6 +236,23 @@ public abstract class PotentialAnnotation { return visitor.visitAnnotationType(annotationType); } + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (!(o instanceof AnnotationType)) { + return false; + } + + AnnotationType other = (AnnotationType)o; + return annotationType.equals(other.annotationType); + } + + @Override + public int hashCode() { + return annotationType.hashCode(); + } + @Override public String toString() { return "@" + annotationType.getCanonicalName(); @@ -233,6 +284,23 @@ public abstract class PotentialAnnotation { return visitor.visitAnnotationInstance(annotation); } + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (!(o instanceof AnnotationInstance)) { + return false; + } + + AnnotationInstance other = (AnnotationInstance)o; + return annotation.equals(other.annotation); + } + + @Override + public int hashCode() { + return annotation.hashCode(); + } + @Override public String toString() { return annotation.toString(); diff --git a/sangria-core/src/test/java/com/tavianator/sangria/core/PotentialAnnotationTest.java b/sangria-core/src/test/java/com/tavianator/sangria/core/PotentialAnnotationTest.java index c4ccc36..4c53ef4 100644 --- a/sangria-core/src/test/java/com/tavianator/sangria/core/PotentialAnnotationTest.java +++ b/sangria-core/src/test/java/com/tavianator/sangria/core/PotentialAnnotationTest.java @@ -36,7 +36,7 @@ import static org.junit.Assert.*; * Tests for {@link PotentialAnnotation}s. * * @author Tavian Barnes (tavianator@tavianator.com) - * @version 1.1 + * @version 1.2 * @since 1.1 */ public class PotentialAnnotationTest { @@ -63,8 +63,8 @@ public class PotentialAnnotationTest { @Test(expected = CreationException.class) public void testInvalidAnnotatedWithInstance() { - none.annotatedWith(Names.named("name")) - .annotatedWith(Names.named("name")); + none.annotatedWith(nameAnnotation) + .annotatedWith(nameAnnotation); } @Test @@ -77,6 +77,16 @@ public class PotentialAnnotationTest { equalTo(new Key(nameAnnotation) { })); } + @Test + public void testFromKey() { + assertThat(PotentialAnnotation.from(new Key() { }), + equalTo(none)); + assertThat(PotentialAnnotation.from(new Key(Simple.class) { }), + equalTo(none.annotatedWith(Simple.class))); + assertThat(PotentialAnnotation.from(new Key(nameAnnotation) { }), + equalTo(none.annotatedWith(nameAnnotation))); + } + @Test public void testVisitor() { PotentialAnnotation.Visitor visitor = new PotentialAnnotation.Visitor() { @@ -114,7 +124,7 @@ public class PotentialAnnotationTest { } /** - * Needed to avoid compilation error to to inferred type being anonymous class. + * Needed to avoid compilation error due to inferred type being anonymous class. */ private static Matcher> equalTo(Key key) { return Matchers.equalTo(key); -- cgit v1.2.3 From af52e472b4f0b00b0d00c04b3fa54a79f94b9a49 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 14 Sep 2014 23:56:51 -0400 Subject: lazy: Implement Lazy, very much like Dagger's. --- pom.xml | 7 + sangria-lazy/pom.xml | 58 +++++ .../java/com/tavianator/sangria/lazy/Lazy.java | 81 ++++++ .../com/tavianator/sangria/lazy/LazyBinder.java | 273 +++++++++++++++++++++ .../com/tavianator/sangria/lazy/LazyBinding.java | 34 +++ .../sangria/lazy/LazyBindingVisitor.java | 37 +++ .../com/tavianator/sangria/lazy/package-info.java | 25 ++ .../java/com/tavianator/sangria/lazy/LazyTest.java | 243 ++++++++++++++++++ 8 files changed, 758 insertions(+) create mode 100644 sangria-lazy/pom.xml create mode 100644 sangria-lazy/src/main/java/com/tavianator/sangria/lazy/Lazy.java create mode 100644 sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBinder.java create mode 100644 sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBinding.java create mode 100644 sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBindingVisitor.java create mode 100644 sangria-lazy/src/main/java/com/tavianator/sangria/lazy/package-info.java create mode 100644 sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazyTest.java diff --git a/pom.xml b/pom.xml index 5279ece..794108c 100644 --- a/pom.xml +++ b/pom.xml @@ -78,6 +78,12 @@ ${project.version} + + com.tavianator.sangria + sangria-lazy + ${project.version} + + com.google.inject guice @@ -288,6 +294,7 @@ sangria-slf4j sangria-log4j sangria-listbinder + sangria-lazy diff --git a/sangria-lazy/pom.xml b/sangria-lazy/pom.xml new file mode 100644 index 0000000..49416bb --- /dev/null +++ b/sangria-lazy/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + + com.tavianator.sangria + sangria + 1.2-SNAPSHOT + + + sangria-lazy + jar + Sangria Lazy + Lazily-loaded dependencies + + + + com.tavianator.sangria + sangria-core + + + + com.google.inject + guice + + + + com.google.guava + guava + + + + com.google.code.findbugs + jsr305 + true + + + + junit + junit + test + + + + org.hamcrest + hamcrest-integration + test + + + + org.mockito + mockito-core + test + + + 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 new file mode 100644 index 0000000..adf531c --- /dev/null +++ b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/Lazy.java @@ -0,0 +1,81 @@ +/**************************************************************************** + * Sangria * + * Copyright (C) 2014 Tavian Barnes * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + ****************************************************************************/ + +package com.tavianator.sangria.lazy; + +import javax.inject.Inject; +import javax.inject.Provider; + +/** + * A lazily-loaded dependency. Like a {@link Provider}, calling {@link #get()} will produce an instance of {@code T}. + * Unlike a {@link Provider}, the same instance will be returned for every future call to {@link #get()}. Different + * {@code Lazy} instances are independent and will return different instances from {@link #get()}. + * + *

+ * {@link Lazy} works automatically for unqualified bindings, as long as just-in-time bindings are enabled. For + * qualified bindings, or if explicit bindings are requred, use {@link LazyBinder}: + *

+ * + *
+ * // Either separately...
+ * bind(Dependency.class)
+ *         .annotatedWith(Names.named("name"))
+ *         .to(RealDependency.class);
+ *
+ * LazyBinder.create(binder())
+ *         .bind(Dependency.class)
+ *         .annotatedWith(Names.named("name"));
+ *
+ * // ... or in one go
+ * LazyBinder.create(binder())
+ *         .bind(Dependency.class)
+ *         .annotatedWith(Names.named("name"))
+ *         .to(RealDependency.class);
+ *
+ * ...
+ *
+ * {@literal @}Inject {@literal @}Named("name") Lazy<Dependency> lazy;
+ * 
+ * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +public final class Lazy { + private final Provider provider; + private volatile T instance = null; + + @Inject + Lazy(Provider provider) { + this.provider = provider; + } + + /** + * @return A lazily-produced value of type {@code T}. + */ + public T get() { + // Double-checked locking + if (instance == null) { + synchronized (this) { + if (instance == null) { + instance = provider.get(); + } + } + } + return 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 new file mode 100644 index 0000000..26d3848 --- /dev/null +++ b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBinder.java @@ -0,0 +1,273 @@ +/**************************************************************************** + * Sangria * + * Copyright (C) 2014 Tavian Barnes * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + ****************************************************************************/ + +package com.tavianator.sangria.lazy; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; + +import javax.inject.Provider; + +import com.google.inject.Binder; +import com.google.inject.Key; +import com.google.inject.Scope; +import com.google.inject.TypeLiteral; +import com.google.inject.binder.AnnotatedBindingBuilder; +import com.google.inject.binder.LinkedBindingBuilder; +import com.google.inject.binder.ScopedBindingBuilder; +import com.google.inject.spi.BindingTargetVisitor; +import com.google.inject.spi.ProviderInstanceBinding; +import com.google.inject.spi.ProviderWithExtensionVisitor; +import com.google.inject.util.Types; + +import com.tavianator.sangria.core.PotentialAnnotation; + +/** + * Binder for {@link Lazy} instances. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +public class LazyBinder { + private static final Class[] SKIPPED_SOURCES = { + LazyBinder.class, + BindingAnnotator.class, + LazyBindingBuilder.class, + }; + + private final Binder binder; + + private LazyBinder(Binder binder) { + this.binder = binder; + } + + /** + * Create a {@link LazyBinder}. + * + * @param binder The {@link Binder} to use. + * @return A {@link LazyBinder} instance. + */ + public static LazyBinder create(Binder binder) { + return new LazyBinder(binder.skipSources(SKIPPED_SOURCES)); + } + + @SuppressWarnings("unchecked") + private static TypeLiteral> lazyOf(TypeLiteral type) { + return (TypeLiteral>)TypeLiteral.get(Types.newParameterizedType(Lazy.class, type.getType())); + } + + /** + * See the EDSL examples at {@link Lazy}. + */ + public AnnotatedBindingBuilder bind(Class type) { + return bind(TypeLiteral.get(type)); + } + + /** + * See the EDSL examples at {@link Lazy}. + */ + public AnnotatedBindingBuilder bind(TypeLiteral type) { + AnnotatedBindingBuilder> lazyBinding = binder.bind(lazyOf(type)); + return new LazyBindingBuilder<>(binder, type, lazyBinding, PotentialAnnotation.none()); + } + + /** + * Applies an annotation to an {@link AnnotatedBindingBuilder}. + */ + private static class BindingAnnotator implements PotentialAnnotation.Visitor> { + private final AnnotatedBindingBuilder builder; + + BindingAnnotator(AnnotatedBindingBuilder builder) { + this.builder = builder; + } + + @Override + public LinkedBindingBuilder visitNoAnnotation() { + return builder; + } + + @Override + public LinkedBindingBuilder visitAnnotationType(Class annotationType) { + return builder.annotatedWith(annotationType); + } + + @Override + public LinkedBindingBuilder visitAnnotationInstance(Annotation annotation) { + return builder.annotatedWith(annotation); + } + } + + /** + * See the EDSL examples at {@link Lazy}. + */ + public LinkedBindingBuilder bind(Key key) { + TypeLiteral type = key.getTypeLiteral(); + PotentialAnnotation potentialAnnotation = PotentialAnnotation.from(key); + return potentialAnnotation.accept(new BindingAnnotator<>(bind(type))); + } + + /** + * Actual binder implementation. + */ + private static class LazyBindingBuilder implements AnnotatedBindingBuilder { + private final Binder binder; + private final TypeLiteral type; + private final AnnotatedBindingBuilder> lazyBinding; + private final PotentialAnnotation potentialAnnotation; + + LazyBindingBuilder( + Binder binder, + TypeLiteral type, + AnnotatedBindingBuilder> lazyBinding, + PotentialAnnotation potentialAnnotation) { + this.binder = binder; + this.type = type; + this.lazyBinding = lazyBinding; + this.potentialAnnotation = potentialAnnotation; + } + + @Override + public LinkedBindingBuilder annotatedWith(Class annotationType) { + PotentialAnnotation newAnnotation = potentialAnnotation.annotatedWith(annotationType); + Key key = newAnnotation.getKey(type); + + lazyBinding.annotatedWith(annotationType) + .toProvider(new LazyProvider<>(binder.getProvider(key), key)); + + return new LazyBindingBuilder<>(binder, type, null, newAnnotation); + } + + @Override + public LinkedBindingBuilder annotatedWith(Annotation annotation) { + PotentialAnnotation newAnnotation = potentialAnnotation.annotatedWith(annotation); + Key key = newAnnotation.getKey(type); + + lazyBinding.annotatedWith(annotation) + .toProvider(new LazyProvider<>(binder.getProvider(key), key)); + + return new LazyBindingBuilder<>(binder, type, null, newAnnotation); + } + + /** + * @return A binding builder for the underlying binding. + */ + private LinkedBindingBuilder makeBinder() { + return binder.bind(potentialAnnotation.getKey(type)); + } + + @Override + public ScopedBindingBuilder to(Class implementation) { + return makeBinder().to(implementation); + } + + @Override + public ScopedBindingBuilder to(TypeLiteral implementation) { + return makeBinder().to(implementation); + } + + @Override + public ScopedBindingBuilder to(Key targetKey) { + return makeBinder().to(targetKey); + } + + @Override + public void toInstance(T instance) { + makeBinder().toInstance(instance); + } + + @Override + public ScopedBindingBuilder toProvider(com.google.inject.Provider provider) { + return makeBinder().toProvider(provider); + } + + @Override + public ScopedBindingBuilder toProvider(Provider provider) { + return makeBinder().toProvider(provider); + } + + @Override + public ScopedBindingBuilder toProvider(Class> providerType) { + return makeBinder().toProvider(providerType); + } + + @Override + public ScopedBindingBuilder toProvider(TypeLiteral> providerType) { + return makeBinder().toProvider(providerType); + } + + @Override + public ScopedBindingBuilder toProvider(Key> providerKey) { + return makeBinder().toProvider(providerKey); + } + + @Override + public ScopedBindingBuilder toConstructor(Constructor constructor) { + return makeBinder().toConstructor(constructor); + } + + @Override + public ScopedBindingBuilder toConstructor(Constructor constructor, TypeLiteral type) { + return makeBinder().toConstructor(constructor, type); + } + + @Override + public void in(Class scopeAnnotation) { + makeBinder().in(scopeAnnotation); + } + + @Override + public void in(Scope scope) { + makeBinder().in(scope); + } + + @Override + public void asEagerSingleton() { + makeBinder().asEagerSingleton(); + } + } + + private static class LazyProvider implements LazyBinding, ProviderWithExtensionVisitor> { + private final Provider provider; + private final Key key; + + LazyProvider(Provider provider, Key key) { + this.provider = provider; + this.key = key; + } + + @Override + public Lazy get() { + return new Lazy<>(provider); + } + + @Override + public Key getTargetKey() { + return key; + } + + @SuppressWarnings("unchecked") // B must be Lazy + @Override + public V acceptExtensionVisitor(BindingTargetVisitor visitor, ProviderInstanceBinding binding) { + if (visitor instanceof LazyBindingVisitor) { + return ((LazyBindingVisitor)visitor).visit(this); + } else { + return visitor.visit(binding); + } + } + } +} diff --git a/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBinding.java b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBinding.java new file mode 100644 index 0000000..518ae57 --- /dev/null +++ b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBinding.java @@ -0,0 +1,34 @@ +/**************************************************************************** + * Sangria * + * Copyright (C) 2014 Tavian Barnes * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + ****************************************************************************/ + +package com.tavianator.sangria.lazy; + +import com.google.inject.Key; + +/** + * SPI for {@link LazyBinder} bindings. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +public interface LazyBinding { + /** + * @return The key wrapped by the {@link Lazy Lazy<T>} binding. + */ + Key getTargetKey(); +} diff --git a/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBindingVisitor.java b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBindingVisitor.java new file mode 100644 index 0000000..ef95a0c --- /dev/null +++ b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyBindingVisitor.java @@ -0,0 +1,37 @@ +/**************************************************************************** + * Sangria * + * Copyright (C) 2014 Tavian Barnes * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + ****************************************************************************/ + +package com.tavianator.sangria.lazy; + +import com.google.inject.spi.BindingTargetVisitor; + +/** + * Visitor interface for the lazy binding SPI. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +public interface LazyBindingVisitor extends BindingTargetVisitor { + /** + * Visit a {@link LazyBinding}. + * + * @param binding The binding to visit. + * @return A value of type {@code V}. + */ + V visit(LazyBinding binding); +} diff --git a/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/package-info.java b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/package-info.java new file mode 100644 index 0000000..15b5b1a --- /dev/null +++ b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/package-info.java @@ -0,0 +1,25 @@ +/**************************************************************************** + * Sangria * + * Copyright (C) 2014 Tavian Barnes * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + ****************************************************************************/ + +/** + * Lazy loading. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +package com.tavianator.sangria.lazy; 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 new file mode 100644 index 0000000..ff63c03 --- /dev/null +++ b/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazyTest.java @@ -0,0 +1,243 @@ +/**************************************************************************** + * Sangria * + * Copyright (C) 2014 Tavian Barnes * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + ****************************************************************************/ + +package com.tavianator.sangria.lazy; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.*; +import javax.inject.Inject; +import javax.inject.Qualifier; + +import com.google.inject.AbstractModule; +import com.google.inject.Binding; +import com.google.inject.CreationException; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.Module; +import com.google.inject.spi.DefaultBindingTargetVisitor; +import com.google.inject.spi.Element; +import com.google.inject.spi.Elements; +import org.junit.Test; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +/** + * Tests for {@link Lazy} injection. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +public class LazyTest { + @Retention(RetentionPolicy.RUNTIME) + @Qualifier + private @interface Simple { + } + + private interface Abstract { + } + + private static class Concrete implements Abstract { + static final ThreadLocal INSTANCES = new ThreadLocal() { + @Override + protected Integer initialValue() { + return 0; + } + }; + + @Inject + Concrete() { + INSTANCES.set(INSTANCES.get() + 1); + } + } + + private static class HasConcrete { + @Inject Lazy lazy; + } + + private static class HasQualifiedAbstract { + @Inject @Simple Lazy lazy; + } + + @Test + public void testJustInTime() { + testHasConcrete(Guice.createInjector()); + } + + @Test + public void testExplicitBindings() { + testHasConcrete(Guice.createInjector(new AbstractModule() { + @Override + protected void configure() { + binder().requireExplicitBindings(); + + bind(Concrete.class); + LazyBinder.create(binder()) + .bind(Concrete.class); + } + })); + } + + private void testHasConcrete(Injector injector) { + int before = Concrete.INSTANCES.get(); + + HasConcrete hasConcrete = new HasConcrete(); + injector.injectMembers(hasConcrete); + assertThat(Concrete.INSTANCES.get(), equalTo(before)); + + Concrete instance = hasConcrete.lazy.get(); + assertThat(Concrete.INSTANCES.get(), equalTo(before + 1)); + + Concrete instance2 = hasConcrete.lazy.get(); + assertThat(instance2, sameInstance(instance)); + assertThat(Concrete.INSTANCES.get(), equalTo(before + 1)); + + HasConcrete hasConcrete2 = new HasConcrete(); + injector.injectMembers(hasConcrete2); + assertThat(Concrete.INSTANCES.get(), equalTo(before + 1)); + + Concrete instance3 = hasConcrete2.lazy.get(); + assertThat(instance3, not(sameInstance(instance))); + assertThat(Concrete.INSTANCES.get(), equalTo(before + 2)); + } + + @Test + public void testBindSeparately() { + testQualifiedAbstract(Guice.createInjector(new AbstractModule() { + @Override + protected void configure() { + bind(Abstract.class) + .annotatedWith(Simple.class) + .to(Concrete.class); + + LazyBinder.create(binder()) + .bind(Abstract.class) + .annotatedWith(Simple.class); + } + })); + } + + @Test + public void testBindTogether() { + testQualifiedAbstract(Guice.createInjector(new AbstractModule() { + @Override + protected void configure() { + LazyBinder.create(binder()) + .bind(Abstract.class) + .annotatedWith(Simple.class) + .to(Concrete.class); + } + })); + } + + private void testQualifiedAbstract(Injector injector) { + int before = Concrete.INSTANCES.get(); + + HasQualifiedAbstract hasQualifiedAbstract = new HasQualifiedAbstract(); + injector.injectMembers(hasQualifiedAbstract); + assertThat(Concrete.INSTANCES.get(), equalTo(before)); + + Abstract instance = hasQualifiedAbstract.lazy.get(); + assertThat(Concrete.INSTANCES.get(), equalTo(before + 1)); + + Abstract instance2 = hasQualifiedAbstract.lazy.get(); + assertThat(instance2, sameInstance(instance2)); + assertThat(Concrete.INSTANCES.get(), equalTo(before + 1)); + + HasQualifiedAbstract hasQualifiedAbstract2 = new HasQualifiedAbstract(); + injector.injectMembers(hasQualifiedAbstract2); + assertThat(Concrete.INSTANCES.get(), equalTo(before + 1)); + + Abstract instance3 = hasQualifiedAbstract2.lazy.get(); + assertThat(instance3, not(sameInstance(instance))); + assertThat(Concrete.INSTANCES.get(), equalTo(before + 2)); + } + + @Test(expected = CreationException.class) + public void testMissingBinding() { + Guice.createInjector(new AbstractModule() { + @Override + protected void configure() { + LazyBinder.create(binder()) + .bind(Abstract.class); + } + }); + } + + @Test(expected = CreationException.class) + public void testMissingQualifiedBinding() { + Guice.createInjector(new AbstractModule() { + @Override + protected void configure() { + LazyBinder.create(binder()) + .bind(Abstract.class) + .annotatedWith(Simple.class); + } + }); + } + + private static class TestVisitor extends DefaultBindingTargetVisitor implements LazyBindingVisitor { + @Override + public Boolean visit(LazyBinding binding) { + assertThat(binding.getTargetKey().equals(new Key(Simple.class) { }), is(true)); + return true; + } + + @Override + protected Boolean visitOther(Binding binding) { + return false; + } + } + + private boolean visit(Binding binding) { + return binding.acceptTargetVisitor(new TestVisitor()); + } + + @Test + public void testExtensionSpi() { + Module module = new AbstractModule() { + @Override + protected void configure() { + // TODO: Expose the SPI for unqualified bindings + LazyBinder.create(binder()) + .bind(Abstract.class) + .annotatedWith(Simple.class) + .to(Concrete.class); + } + }; + + List elements = Elements.getElements(module); + + int passed = 0; + for (Element element : elements) { + if (element instanceof Binding) { + if (visit((Binding)element)) { + ++passed; + } + } + } + assertThat(passed, equalTo(1)); + + Injector injector = Guice.createInjector(Elements.getModule(elements)); + assertThat(visit(injector.getBinding(new Key>(Simple.class) { })), is(true)); + assertThat(visit(injector.getBinding(new Key(Simple.class) { })), is(false)); + assertThat(visit(injector.getBinding(new Key() { })), is(false)); + } +} -- cgit v1.2.3 From 2517e3a84c69862453111ef4efdad314074cb32d Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 15 Sep 2014 20:29:47 -0400 Subject: lazy: Add LazySingleton scope. --- .../com/tavianator/sangria/lazy/LazyScopes.java | 59 +++++++++++++++ .../com/tavianator/sangria/lazy/LazySingleton.java | 41 +++++++++++ .../tavianator/sangria/lazy/SangriaLazyModule.java | 44 ++++++++++++ .../tavianator/sangria/lazy/LazySingletonTest.java | 83 ++++++++++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyScopes.java create mode 100644 sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazySingleton.java create mode 100644 sangria-lazy/src/main/java/com/tavianator/sangria/lazy/SangriaLazyModule.java create mode 100644 sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazySingletonTest.java diff --git a/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyScopes.java b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyScopes.java new file mode 100644 index 0000000..beb1294 --- /dev/null +++ b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazyScopes.java @@ -0,0 +1,59 @@ +/**************************************************************************** + * Sangria * + * Copyright (C) 2014 Tavian Barnes * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + ****************************************************************************/ + +package com.tavianator.sangria.lazy; + +import com.google.inject.Key; +import com.google.inject.Provider; +import com.google.inject.Scope; +import com.google.inject.Scopes; + +/** + * Lazy scope implementations. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +public class LazyScopes { + /** + * Lazy version of {@link Scopes#SINGLETON}. + * + * @see LazySingleton + */ + public static final Scope LAZY_SINGLETON = new Scope() { + public Provider scope(final Key key, final Provider creator) { + final Provider singleton = Scopes.SINGLETON.scope(key, creator); + + return new Provider() { + public T get() { + return singleton.get(); + } + + @Override + public String toString() { + return String.format("%s[%s]", creator, LAZY_SINGLETON); + } + }; + } + + @Override + public String toString() { + return "LazyScopes.LAZY_SINGLETON"; + } + }; +} diff --git a/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazySingleton.java b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazySingleton.java new file mode 100644 index 0000000..b8f0482 --- /dev/null +++ b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/LazySingleton.java @@ -0,0 +1,41 @@ +/**************************************************************************** + * Sangria * + * Copyright (C) 2014 Tavian Barnes * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + ****************************************************************************/ + +package com.tavianator.sangria.lazy; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; +import javax.inject.Scope; +import javax.inject.Singleton; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.*; + +/** + * Like {@link Singleton}, but always lazy. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +@Scope +@Documented +@Target({ TYPE, METHOD }) +@Retention(RUNTIME) +public @interface LazySingleton { +} diff --git a/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/SangriaLazyModule.java b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/SangriaLazyModule.java new file mode 100644 index 0000000..ff4a02a --- /dev/null +++ b/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/SangriaLazyModule.java @@ -0,0 +1,44 @@ +/**************************************************************************** + * Sangria * + * Copyright (C) 2014 Tavian Barnes * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + ****************************************************************************/ + +package com.tavianator.sangria.lazy; + +import com.google.inject.AbstractModule; + +/** + * Module for lazy initialization features. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +public class SangriaLazyModule extends AbstractModule { + @Override + protected void configure() { + bindScope(LazySingleton.class, LazyScopes.LAZY_SINGLETON); + } + + @Override + public boolean equals(Object o) { + return o instanceof SangriaLazyModule; + } + + @Override + public int hashCode() { + return SangriaLazyModule.class.hashCode(); + } +} diff --git a/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazySingletonTest.java b/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazySingletonTest.java new file mode 100644 index 0000000..69fabce --- /dev/null +++ b/sangria-lazy/src/test/java/com/tavianator/sangria/lazy/LazySingletonTest.java @@ -0,0 +1,83 @@ +/**************************************************************************** + * Sangria * + * Copyright (C) 2014 Tavian Barnes * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + ****************************************************************************/ + +package com.tavianator.sangria.lazy; + +import javax.inject.Inject; +import javax.inject.Provider; + +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Module; +import com.google.inject.Stage; +import org.junit.Test; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +/** + * Tests for the {@link LazySingleton} scope. + * + * @author Tavian Barnes (tavianator@tavianator.com) + * @version 1.2 + * @since 1.2 + */ +public class LazySingletonTest { + @LazySingleton + private static class Scoped { + static final ThreadLocal INSTANCES = new ThreadLocal() { + @Override + protected Integer initialValue() { + return 0; + } + }; + + @Inject + Scoped() { + INSTANCES.set(INSTANCES.get() + 1); + } + } + + @Test + public void testDevelopment() { + test(Stage.DEVELOPMENT, new SangriaLazyModule()); + } + + @Test + public void testProduction() { + test(Stage.PRODUCTION, new SangriaLazyModule()); + } + + @Test + public void testDuplicateModule() { + test(Stage.PRODUCTION, new SangriaLazyModule(), new SangriaLazyModule()); + } + + private void test(Stage stage, Module... modules) { + int before = Scoped.INSTANCES.get(); + + Injector injector = Guice.createInjector(stage, modules); + Provider provider = injector.getProvider(Scoped.class); + assertThat(Scoped.INSTANCES.get(), equalTo(before)); + + Scoped instance = provider.get(); + assertThat(Scoped.INSTANCES.get(), equalTo(before + 1)); + + assertThat(provider.get(), sameInstance(instance)); + assertThat(Scoped.INSTANCES.get(), equalTo(before + 1)); + } +} -- cgit v1.2.3 From 75db2e06b7a6b1f15bad5986b33bc3aa1bb31f26 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 15 Sep 2014 20:37:29 -0400 Subject: lazy: Better maven module description. --- sangria-lazy/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sangria-lazy/pom.xml b/sangria-lazy/pom.xml index 49416bb..4840af3 100644 --- a/sangria-lazy/pom.xml +++ b/sangria-lazy/pom.xml @@ -13,7 +13,7 @@ sangria-lazy jar Sangria Lazy - Lazily-loaded dependencies + Lazy loading -- cgit v1.2.3