1 /* 2 * Copyright (C) 2015 Google, Inc. 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 package dagger.producers.internal; 17 18 import com.google.common.util.concurrent.ListenableFuture; 19 import dagger.producers.Producer; 20 import dagger.producers.monitoring.ProducerMonitor; 21 import dagger.producers.monitoring.ProducerToken; 22 import dagger.producers.monitoring.ProductionComponentMonitor; 23 import dagger.producers.monitoring.internal.Monitors; 24 25 import static com.google.common.base.Preconditions.checkNotNull; 26 27 import javax.annotation.Nullable; 28 import javax.inject.Provider; 29 30 /** 31 * An abstract {@link Producer} implementation that memoizes the result of its compute method. 32 * 33 * @author Jesse Beder 34 * @since 2.0 35 */ 36 public abstract class AbstractProducer<T> implements Producer<T> { 37 private final Provider<ProductionComponentMonitor> monitorProvider; 38 @Nullable private final ProducerToken token; 39 private volatile ListenableFuture<T> instance = null; 40 41 protected AbstractProducer() { 42 this(Monitors.noOpProductionComponentMonitorProvider(), null); 43 } 44 45 protected AbstractProducer( 46 Provider<ProductionComponentMonitor> monitorProvider, @Nullable ProducerToken token) { 47 this.monitorProvider = checkNotNull(monitorProvider); 48 this.token = token; 49 } 50 51 /** Computes this producer's future, which is then cached in {@link #get}. */ 52 protected abstract ListenableFuture<T> compute(ProducerMonitor monitor); 53 54 @Override 55 public final ListenableFuture<T> get() { 56 // double-check idiom from EJ2: Item 71 57 ListenableFuture<T> result = instance; 58 if (result == null) { 59 synchronized (this) { 60 result = instance; 61 if (result == null) { 62 ProducerMonitor monitor = monitorProvider.get().producerMonitorFor(token); 63 instance = result = compute(monitor); 64 if (result == null) { 65 throw new NullPointerException("compute returned null"); 66 } 67 monitor.addCallbackTo(result); 68 } 69 } 70 } 71 return result; 72 } 73 } 74