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 */
16package android.databinding;
17
18import android.os.Parcel;
19import android.os.Parcelable;
20
21import java.io.Serializable;
22
23/**
24 * An observable class that holds a primitive int.
25 * <p>
26 * Observable field classes may be used instead of creating an Observable object:
27 * <pre><code>public class MyDataObject {
28 *     public final ObservableField<String> name = new ObservableField<String>();
29 *     public final ObservableInt age = new ObservableInt();
30 * }</code></pre>
31 * Fields of this type should be declared final because bindings only detect changes in the
32 * field's value, not of the field itself.
33 * <p>
34 * This class is parcelable and serializable but callbacks are ignored when the object is
35 * parcelled / serialized. Unless you add custom callbacks, this will not be an issue because
36 * data binding framework always re-registers callbacks when the view is bound.
37 */
38public class ObservableInt extends BaseObservable implements Parcelable, Serializable {
39    static final long serialVersionUID = 1L;
40    private int mValue;
41
42    /**
43     * Creates an ObservableInt with the given initial value.
44     *
45     * @param value the initial value for the ObservableInt
46     */
47    public ObservableInt(int value) {
48        mValue = value;
49    }
50
51    /**
52     * Creates an ObservableInt with the initial value of <code>0</code>.
53     */
54    public ObservableInt() {
55    }
56
57    /**
58     * @return the stored value.
59     */
60    public int get() {
61        return mValue;
62    }
63
64    /**
65     * Set the stored value.
66     */
67    public void set(int value) {
68        if (value != mValue) {
69            mValue = value;
70            notifyChange();
71        }
72    }
73
74    @Override
75    public int describeContents() {
76        return 0;
77    }
78
79    @Override
80    public void writeToParcel(Parcel dest, int flags) {
81        dest.writeInt(mValue);
82    }
83
84    public static final Parcelable.Creator<ObservableInt> CREATOR
85            = new Parcelable.Creator<ObservableInt>() {
86
87        @Override
88        public ObservableInt createFromParcel(Parcel source) {
89            return new ObservableInt(source.readInt());
90        }
91
92        @Override
93        public ObservableInt[] newArray(int size) {
94            return new ObservableInt[size];
95        }
96    };
97}
98