dagger.Lazy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dagger Show documentation
Show all versions of dagger Show documentation
A fast dependency injector for Android and Java.
/*
* Copyright (C) 2012 The Dagger Authors.
*
* 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 dagger;
/**
* A handle to a lazily-computed value. Each {@code Lazy} computes its value on
* the first call to {@link #get()} and remembers that same value for all
* subsequent calls to {@code get()}.
*
* All implementations are expected to be thread-safe and compute their value at most once.
*
*
Example
* The differences between direct injection, provider
* injection and lazy injection are best demonstrated
* with an example. Start with a module that computes a different integer for
* each use:
* {@literal @Module}
* final class CounterModule {
* int next = 100;
*
* {@literal @Provides} Integer provideInteger() {
* System.out.println("computing...");
* return next++;
* }
* }
*
*
* Direct Injection
* This class injects that integer and prints it 3 times:
* final class DirectCounter {
* {@literal @Inject} Integer value;
*
* void print() {
* System.out.println("printing...");
* System.out.println(value);
* System.out.println(value);
* System.out.println(value);
* }
* }
*
* Injecting a {@code DirectCounter} and invoking {@code print()} reveals that
* the value is computed before it is required:
* computing...
* printing...
* 100
* 100
* 100
*
*
* Provider Injection
* This class injects a {@linkplain javax.inject.Provider provider} for the
* integer. It calls {@code Provider.get()} 3 times and prints each result:
*
* final class ProviderCounter {
* {@literal @Inject Provider provider;}
*
* void print() {
* System.out.println("printing...");
* System.out.println(provider.get());
* System.out.println(provider.get());
* System.out.println(provider.get());
* }
* }
*
* Injecting a {@code ProviderCounter} and invoking {@code print()} shows that
* a new value is computed each time {@code Provider.get()} is used:
* printing...
* computing...
* 100
* computing...
* 101
* computing...
* 102
*
*
* Lazy Injection
* This class injects a {@code Lazy} for the integer. Like the provider above,
* it calls {@code Lazy.get()} 3 times and prints each result:
* final class LazyCounter {
* {@literal @Inject Lazy lazy;}
*
* void print() {
* System.out.println("printing...");
* System.out.println(lazy.get());
* System.out.println(lazy.get());
* System.out.println(lazy.get());
* }
* }
*
* Injecting a {@code LazyCounter} and invoking {@code print()} shows that a new
* value is computed immediately before it is needed. The same value is returned
* for all subsequent uses:
* printing...
* computing...
* 100
* 100
* 100
*
*
* Lazy != Singleton
* Note that each injected {@code Lazy} is independent, and remembers its value
* in isolation of other {@code Lazy} instances. In this example, two {@code
* LazyCounter} objects are created and {@code print()} is called on each:
*
* final class LazyCounters {
* {@literal @Inject} LazyCounter counter1;
* {@literal @Inject} LazyCounter counter2;
*
* void print() {
* counter1.print();
* counter2.print();
* }
* }
*
* The output demonstrates that each {@code Lazy} works independently:
*
* printing...
* computing...
* 100
* 100
* 100
* printing...
* computing...
* 101
* 101
* 101
*
* Use {@link javax.inject.Singleton @Singleton} to share one instance among all
* clients, and {@code Lazy} for lazy computation in a single client.
*/
public interface Lazy {
/**
* Return the underlying value, computing the value if necessary. All calls to
* the same {@code Lazy} instance will return the same result.
*/
T get();
}