• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.NonNull;
20 
21 import java.util.LinkedHashSet;
22 import java.util.Set;
23 
24 /**
25  * Base class that provides the implementation for the callback mechanism of the
26  * {@link DataProducer} API.
27  *
28  * @param <T> The type of data this producer returns through {@link #getData()}.
29  */
30 public abstract class BaseDataProducer<T> implements DataProducer<T> {
31     private final Set<Runnable> mCallbacks = new LinkedHashSet<>();
32 
33     @Override
addDataChangedCallback(@onNull Runnable callback)34     public final void addDataChangedCallback(@NonNull Runnable callback) {
35         mCallbacks.add(callback);
36     }
37 
38     @Override
removeDataChangedCallback(@onNull Runnable callback)39     public final void removeDataChangedCallback(@NonNull Runnable callback) {
40         mCallbacks.remove(callback);
41     }
42 
43     /**
44      * Called to notify all registered callbacks that the data provided by {@link #getData()} has
45      * changed.
46      */
notifyDataChanged()47     protected void notifyDataChanged() {
48         for (Runnable callback : mCallbacks) {
49             callback.run();
50         }
51     }
52 }
53