17920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount/*
27920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount * Copyright (C) 2015 The Android Open Source Project
37920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount *
47920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount * Licensed under the Apache License, Version 2.0 (the "License");
57920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount * you may not use this file except in compliance with the License.
67920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount * You may obtain a copy of the License at
77920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount *
87920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount *      http://www.apache.org/licenses/LICENSE-2.0
97920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount *
107920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount * Unless required by applicable law or agreed to in writing, software
117920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount * distributed under the License is distributed on an "AS IS" BASIS,
127920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount * See the License for the specific language governing permissions and
147920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount * limitations under the License.
157920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount */
16fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountpackage android.databinding;
177920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount
18d37b47419005587c52ca4c099432327b7f06478dYigit Boyarimport android.os.Parcel;
19d37b47419005587c52ca4c099432327b7f06478dYigit Boyarimport android.os.Parcelable;
20d37b47419005587c52ca4c099432327b7f06478dYigit Boyar
21d37b47419005587c52ca4c099432327b7f06478dYigit Boyarimport java.io.Serializable;
22d37b47419005587c52ca4c099432327b7f06478dYigit Boyar
23d37b47419005587c52ca4c099432327b7f06478dYigit Boyar/**
24d37b47419005587c52ca4c099432327b7f06478dYigit Boyar * An observable class that holds a primitive double.
25d37b47419005587c52ca4c099432327b7f06478dYigit Boyar * <p>
26c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * Observable field classes may be used instead of creating an Observable object:
27c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * <pre><code>public class MyDataObject {
28c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *     public final ObservableDouble temperature = new ObservableDouble();
29c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * }</code></pre>
30c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * Fields of this type should be declared final because bindings only detect changes in the
31c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * field's value, not of the field itself.
32c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * <p>
33d37b47419005587c52ca4c099432327b7f06478dYigit Boyar * This class is parcelable and serializable but callbacks are ignored when the object is
34d37b47419005587c52ca4c099432327b7f06478dYigit Boyar * parcelled / serialized. Unless you add custom callbacks, this will not be an issue because
35d37b47419005587c52ca4c099432327b7f06478dYigit Boyar * data binding framework always re-registers callbacks when the view is bound.
36d37b47419005587c52ca4c099432327b7f06478dYigit Boyar */
37d37b47419005587c52ca4c099432327b7f06478dYigit Boyarpublic class ObservableDouble extends BaseObservable implements Parcelable, Serializable {
38d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    static final long serialVersionUID = 1L;
397920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount    private double mValue;
407920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount
41d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    /**
42d37b47419005587c52ca4c099432327b7f06478dYigit Boyar     * Creates an ObservableDouble with the given initial value.
43d37b47419005587c52ca4c099432327b7f06478dYigit Boyar     *
44d37b47419005587c52ca4c099432327b7f06478dYigit Boyar     * @param value the initial value for the ObservableDouble
45d37b47419005587c52ca4c099432327b7f06478dYigit Boyar     */
46d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    public ObservableDouble(double value) {
47d37b47419005587c52ca4c099432327b7f06478dYigit Boyar        mValue = value;
48d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    }
49d37b47419005587c52ca4c099432327b7f06478dYigit Boyar
50d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    /**
51d37b47419005587c52ca4c099432327b7f06478dYigit Boyar     * Creates an ObservableDouble with the initial value of <code>0</code>.
52d37b47419005587c52ca4c099432327b7f06478dYigit Boyar     */
53d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    public ObservableDouble() {
54d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    }
55d37b47419005587c52ca4c099432327b7f06478dYigit Boyar
56c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount    /**
57c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount     * @return the stored value.
58c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount     */
597920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount    public double get() {
607920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount        return mValue;
617920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount    }
627920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount
63c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount    /**
64c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount     * Set the stored value.
65c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount     */
667920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount    public void set(double value) {
674774d9e71a36c8381f56a0120984916df9fc724bGeorge Mount        if (value != mValue) {
684774d9e71a36c8381f56a0120984916df9fc724bGeorge Mount            mValue = value;
694774d9e71a36c8381f56a0120984916df9fc724bGeorge Mount            notifyChange();
704774d9e71a36c8381f56a0120984916df9fc724bGeorge Mount        }
717920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount    }
72d37b47419005587c52ca4c099432327b7f06478dYigit Boyar
73d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    @Override
74d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    public int describeContents() {
75d37b47419005587c52ca4c099432327b7f06478dYigit Boyar        return 0;
76d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    }
77d37b47419005587c52ca4c099432327b7f06478dYigit Boyar
78d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    @Override
79d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    public void writeToParcel(Parcel dest, int flags) {
80d37b47419005587c52ca4c099432327b7f06478dYigit Boyar        dest.writeDouble(mValue);
81d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    }
82d37b47419005587c52ca4c099432327b7f06478dYigit Boyar
83d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    public static final Parcelable.Creator<ObservableDouble> CREATOR
84d37b47419005587c52ca4c099432327b7f06478dYigit Boyar            = new Parcelable.Creator<ObservableDouble>() {
85d37b47419005587c52ca4c099432327b7f06478dYigit Boyar
86d37b47419005587c52ca4c099432327b7f06478dYigit Boyar        @Override
87d37b47419005587c52ca4c099432327b7f06478dYigit Boyar        public ObservableDouble createFromParcel(Parcel source) {
88d37b47419005587c52ca4c099432327b7f06478dYigit Boyar            return new ObservableDouble(source.readDouble());
89d37b47419005587c52ca4c099432327b7f06478dYigit Boyar        }
90d37b47419005587c52ca4c099432327b7f06478dYigit Boyar
91d37b47419005587c52ca4c099432327b7f06478dYigit Boyar        @Override
92d37b47419005587c52ca4c099432327b7f06478dYigit Boyar        public ObservableDouble[] newArray(int size) {
93d37b47419005587c52ca4c099432327b7f06478dYigit Boyar            return new ObservableDouble[size];
94d37b47419005587c52ca4c099432327b7f06478dYigit Boyar        }
95d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    };
967920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount}
97