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. --- .../java/com/tavianator/sangria/lazy/Lazy.java | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 sangria-lazy/src/main/java/com/tavianator/sangria/lazy/Lazy.java (limited to 'sangria-lazy/src/main/java/com/tavianator/sangria/lazy/Lazy.java') 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; + } +} -- cgit v1.2.3