1 /* 2 * Copyright (C) 2021 The Android Open Source Project 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 androidx.window.util; 18 19 import androidx.annotation.GuardedBy; 20 import androidx.annotation.NonNull; 21 22 import java.util.HashSet; 23 import java.util.LinkedHashSet; 24 import java.util.Optional; 25 import java.util.Set; 26 import java.util.function.Consumer; 27 28 /** 29 * Base class that provides the implementation for the callback mechanism of the 30 * {@link DataProducer} API. This class is thread safe for adding, removing, and notifying 31 * consumers. 32 * 33 * @param <T> The type of data this producer returns through {@link DataProducer#getData}. 34 */ 35 public abstract class BaseDataProducer<T> implements DataProducer<T>, 36 AcceptOnceConsumer.AcceptOnceProducerCallback<T> { 37 38 private final Object mLock = new Object(); 39 @GuardedBy("mLock") 40 private final Set<Consumer<T>> mCallbacks = new LinkedHashSet<>(); 41 @GuardedBy("mLock") 42 private final Set<Consumer<T>> mCallbacksToRemove = new HashSet<>(); 43 44 /** 45 * Adds a callback to the set of callbacks listening for data. Data is delivered through 46 * {@link BaseDataProducer#notifyDataChanged(Object)}. This method is thread safe. Callers 47 * should ensure that callbacks are thread safe. 48 * @param callback that will receive data from the producer. 49 */ 50 @Override addDataChangedCallback(@onNull Consumer<T> callback)51 public final void addDataChangedCallback(@NonNull Consumer<T> callback) { 52 synchronized (mLock) { 53 mCallbacks.add(callback); 54 Optional<T> currentData = getCurrentData(); 55 currentData.ifPresent(callback); 56 onListenersChanged(mCallbacks); 57 } 58 } 59 60 /** 61 * Removes a callback to the set of callbacks listening for data. This method is thread safe 62 * for adding. 63 * @param callback that was registered in 64 * {@link BaseDataProducer#addDataChangedCallback(Consumer)}. 65 */ 66 @Override removeDataChangedCallback(@onNull Consumer<T> callback)67 public final void removeDataChangedCallback(@NonNull Consumer<T> callback) { 68 synchronized (mLock) { 69 mCallbacks.remove(callback); 70 onListenersChanged(mCallbacks); 71 } 72 } 73 onListenersChanged(Set<Consumer<T>> callbacks)74 protected void onListenersChanged(Set<Consumer<T>> callbacks) {} 75 76 /** 77 * @return the current data if available and {@code Optional.empty()} otherwise. 78 */ 79 @NonNull getCurrentData()80 public abstract Optional<T> getCurrentData(); 81 82 /** 83 * Called to notify all registered consumers that the data provided 84 * by {@link DataProducer#getData} has changed. Calls to this are thread save but callbacks need 85 * to ensure thread safety. 86 */ notifyDataChanged(T value)87 protected void notifyDataChanged(T value) { 88 synchronized (mLock) { 89 for (Consumer<T> callback : mCallbacks) { 90 callback.accept(value); 91 } 92 removeFinishedCallbacksLocked(); 93 } 94 } 95 96 /** 97 * Removes any callbacks that notified us through {@link #onConsumerReadyToBeRemoved(Consumer)} 98 * that they are ready to be removed. 99 */ 100 @GuardedBy("mLock") removeFinishedCallbacksLocked()101 private void removeFinishedCallbacksLocked() { 102 for (Consumer<T> callback: mCallbacksToRemove) { 103 mCallbacks.remove(callback); 104 } 105 mCallbacksToRemove.clear(); 106 } 107 108 @Override onConsumerReadyToBeRemoved(Consumer<T> callback)109 public void onConsumerReadyToBeRemoved(Consumer<T> callback) { 110 synchronized (mLock) { 111 mCallbacksToRemove.add(callback); 112 } 113 } 114 }