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 java.io.Serializable;
19d37b47419005587c52ca4c099432327b7f06478dYigit Boyar
20d37b47419005587c52ca4c099432327b7f06478dYigit Boyar/**
21d37b47419005587c52ca4c099432327b7f06478dYigit Boyar * An object wrapper to make it observable.
22c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * <p>
23c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * Observable field classes may be used instead of creating an Observable object:
24c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * <pre><code>public class MyDataObject {
255884ca73e8737846bee442a8032de649615f20cbGeorge Mount *     public final ObservableField&lt;String&gt; name = new ObservableField&lt;String&gt;();
26c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *     public final ObservableInt age = new ObservableInt();
27c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * }</code></pre>
28c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * Fields of this type should be declared final because bindings only detect changes in the
29c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * field's value, not of the field itself.
30d37b47419005587c52ca4c099432327b7f06478dYigit Boyar *
31d37b47419005587c52ca4c099432327b7f06478dYigit Boyar * @param <T> The type parameter for the actual object.
32d37b47419005587c52ca4c099432327b7f06478dYigit Boyar * @see android.databinding.ObservableParcelable
33d37b47419005587c52ca4c099432327b7f06478dYigit Boyar */
34d37b47419005587c52ca4c099432327b7f06478dYigit Boyarpublic class ObservableField<T> extends BaseObservable implements Serializable {
35d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    static final long serialVersionUID = 1L;
367920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount    private T mValue;
377920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount
38d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    /**
39d37b47419005587c52ca4c099432327b7f06478dYigit Boyar     * Wraps the given object and creates an observable object
40d37b47419005587c52ca4c099432327b7f06478dYigit Boyar     *
41d37b47419005587c52ca4c099432327b7f06478dYigit Boyar     * @param value The value to be wrapped as an observable.
42d37b47419005587c52ca4c099432327b7f06478dYigit Boyar     */
43d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    public ObservableField(T value) {
44d37b47419005587c52ca4c099432327b7f06478dYigit Boyar        mValue = value;
45d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    }
46d37b47419005587c52ca4c099432327b7f06478dYigit Boyar
47d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    /**
48d37b47419005587c52ca4c099432327b7f06478dYigit Boyar     * Creates an empty observable object
49d37b47419005587c52ca4c099432327b7f06478dYigit Boyar     */
50d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    public ObservableField() {
51d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    }
52d37b47419005587c52ca4c099432327b7f06478dYigit Boyar
53c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount    /**
54c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount     * @return the stored value.
55c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount     */
567920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount    public T get() {
577920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount        return mValue;
587920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount    }
597920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount
60c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount    /**
61c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount     * Set the stored value.
62c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount     */
637920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount    public void set(T value) {
644774d9e71a36c8381f56a0120984916df9fc724bGeorge Mount        if (value != mValue) {
654774d9e71a36c8381f56a0120984916df9fc724bGeorge Mount            mValue = value;
664774d9e71a36c8381f56a0120984916df9fc724bGeorge Mount            notifyChange();
674774d9e71a36c8381f56a0120984916df9fc724bGeorge Mount        }
687920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount    }
697920e17f7b501d5792e7e3250e9dbb69eca86adeGeorge Mount}
70