1 /*
2  * Copyright (C) 2014 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 android.databinding;
18 
19 /**
20  * A convenience class that implements {@link android.databinding.Observable} interface and provides
21  * {@link #notifyPropertyChanged(int)} and {@link #notifyChange} methods.
22  */
23 public class BaseObservable implements Observable {
24     private transient PropertyChangeRegistry mCallbacks;
25 
BaseObservable()26     public BaseObservable() {
27     }
28 
29     @Override
addOnPropertyChangedCallback(OnPropertyChangedCallback callback)30     public synchronized void addOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
31         if (mCallbacks == null) {
32             mCallbacks = new PropertyChangeRegistry();
33         }
34         mCallbacks.add(callback);
35     }
36 
37     @Override
removeOnPropertyChangedCallback(OnPropertyChangedCallback callback)38     public synchronized void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
39         if (mCallbacks != null) {
40             mCallbacks.remove(callback);
41         }
42     }
43 
44     /**
45      * Notifies listeners that all properties of this instance have changed.
46      */
notifyChange()47     public synchronized void notifyChange() {
48         if (mCallbacks != null) {
49             mCallbacks.notifyCallbacks(this, 0, null);
50         }
51     }
52 
53     /**
54      * Notifies listeners that a specific property has changed. The getter for the property
55      * that changes should be marked with {@link Bindable} to generate a field in
56      * <code>BR</code> to be used as <code>fieldId</code>.
57      *
58      * @param fieldId The generated BR id for the Bindable field.
59      */
notifyPropertyChanged(int fieldId)60     public void notifyPropertyChanged(int fieldId) {
61         if (mCallbacks != null) {
62             mCallbacks.notifyCallbacks(this, fieldId, null);
63         }
64     }
65 }
66