1 // Copyright 2019 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.base.supplier; 6 7 import org.chromium.base.Callback; 8 9 /** 10 * ObservableSupplier wraps an asynchronously provided object E, notifying observers when the 11 * dependency is available. This allows classes dependent on E to be provided with a 12 * ObservableSupplier during construction and register a Callback<E> to be notified when the needed 13 * dependency is available. 14 * 15 * This class must only be accessed from a single thread. 16 * 17 * For classes owning the ObservableSupplier and providing it as a dependency to others, see 18 * {@link ObservableSupplierImpl}. 19 * 20 * For classes using a ObservableSupplier to receive a dependency: 21 * - To be notified when the object is available, call {@link #addObserver(Callback)} with a 22 * Callback to be notified when the object is available. 23 * - If the object is already available, the Callback will be called immediately. 24 * - The Callback may be called multiple times if the object wrapped by the ObservableSupplier 25 * changes. 26 * 27 * @param <E> The type of the wrapped object. 28 */ 29 public interface ObservableSupplier<E> extends Supplier<E> { 30 /** 31 * @param obs An observer to be notified when the object owned by this supplier is available. 32 * If the object is already available, the callback will be notified at the end of the 33 * current message loop (so long as the object hasn't changed). 34 * @return The current object or null if it hasn't been set yet. 35 */ addObserver(Callback<E> obs)36 E addObserver(Callback<E> obs); 37 38 /** 39 * @param obs The observer to remove. 40 */ removeObserver(Callback<E> obs)41 void removeObserver(Callback<E> obs); 42 } 43