1 /* 2 * Copyright (C) 2016 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dagger.internal; 18 19 import static dagger.internal.Preconditions.checkNotNull; 20 import static dagger.internal.Providers.asDaggerProvider; 21 22 import dagger.Lazy; 23 24 /** 25 * A {@link Provider} of {@link Lazy} instances that each delegate to a given {@link Provider}. 26 */ 27 public final class ProviderOfLazy<T> implements Provider<Lazy<T>> { 28 29 private final Provider<T> provider; 30 ProviderOfLazy(Provider<T> provider)31 private ProviderOfLazy(Provider<T> provider) { 32 assert provider != null; 33 this.provider = provider; 34 } 35 36 /** 37 * Returns a new instance of {@link Lazy Lazy<T>}, which calls {@link Provider#get()} at 38 * most once on the {@link Provider} held by this object. 39 */ 40 @Override get()41 public Lazy<T> get() { 42 return DoubleCheck.lazy(provider); 43 } 44 45 /** 46 * Creates a new {@link Provider Provider<Lazy<T>>} that decorates the given 47 * {@link Provider}. 48 * 49 * @see #get() 50 */ create(Provider<T> provider)51 public static <T> Provider<Lazy<T>> create(Provider<T> provider) { 52 return new ProviderOfLazy<T>(checkNotNull(provider)); 53 } 54 55 /** 56 * Legacy javax version of the method to support libraries compiled with an older version of 57 * Dagger. Do not use directly. 58 */ 59 @Deprecated create(javax.inject.Provider<T> provider)60 public static <T> Provider<Lazy<T>> create(javax.inject.Provider<T> provider) { 61 return create(asDaggerProvider(provider)); 62 } 63 } 64