1 /*
2  * Copyright (C) 2015 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 package android.databinding;
17 
18 import java.util.Map;
19 
20 /**
21  * A {@link Map} that notifies when items change. This kind of Map may be data bound
22  * and have the UI update as the map changes.
23  * <p>
24  * Implementers must call {@link OnMapChangedCallback#onMapChanged(ObservableMap, Object)} whenever
25  * an item is added, changed, or removed.
26  * <p>
27  * ObservableArrayMap is a convenient implementation of ObservableMap.
28  * MapChangeRegistry may help other implementations manage the callbacks.
29  * @see Observable
30  * @see ObservableList
31  */
32 public interface ObservableMap<K, V> extends Map<K, V> {
33 
34     /**
35      * Adds a callback to listen for changes to the ObservableMap.
36      * @param callback The callback to start listening for events.
37      */
addOnMapChangedCallback( OnMapChangedCallback<? extends ObservableMap<K, V>, K, V> callback)38     void addOnMapChangedCallback(
39             OnMapChangedCallback<? extends ObservableMap<K, V>, K, V> callback);
40 
41     /**
42      * Removes a previously added callback.
43      * @param callback The callback that no longer needs to be notified of map changes.
44      */
removeOnMapChangedCallback( OnMapChangedCallback<? extends ObservableMap<K, V>, K, V> callback)45     void removeOnMapChangedCallback(
46             OnMapChangedCallback<? extends ObservableMap<K, V>, K, V> callback);
47 
48     /**
49      * A callback receiving notifications when an ObservableMap changes.
50      */
51     abstract class OnMapChangedCallback<T extends ObservableMap<K, V>, K, V> {
52 
53         /**
54          * Called whenever an ObservableMap changes, including values inserted, deleted,
55          * and changed.
56          * @param sender The changing map.
57          * @param key The key of the value inserted, removed, or changed.
58          */
onMapChanged(T sender, K key)59         public abstract void onMapChanged(T sender, K key);
60     }
61 }
62