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