/* * Copyright (C) 2012 Google, Inc. * Copyright (C) 2012 Square, Inc. * * 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 {@code get()} and remembers that same value for all * subsequent calls to {@code get()}. * *
{@code null} is not a supported value. Implementations of {@code Lazy} * are expected to throw {@link NullPointerException} if the computed value is * {@code null}. * *
* {@literal @Module}
* final class CounterModule {
* int next = 100;
*
* {@literal @Provides} Integer provideInteger() {
* System.out.println("computing...");
* return next++;
* }
* }
*
*
*
* 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
*
*
*
* 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
*
*
*
* 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
*
*
*
* 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