Home | History | Annotate | Download | only in databinding
      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  */
     16 package android.databinding;
     17 
     18 import android.os.Parcel;
     19 import android.os.Parcelable;
     20 
     21 import java.io.Serializable;
     22 
     23 /**
     24  * An observable class that holds a primitive float.
     25  * <p>
     26  * Observable field classes may be used instead of creating an Observable object:
     27  * <pre><code>public class MyDataObject {
     28  *     public final ObservableFloat temperature = new ObservableFloat();
     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  */
     37 public class ObservableFloat extends BaseObservable implements Parcelable, Serializable {
     38     static final long serialVersionUID = 1L;
     39     private float mValue;
     40 
     41     /**
     42      * Creates an ObservableFloat with the given initial value.
     43      *
     44      * @param value the initial value for the ObservableFloat
     45      */
     46     public ObservableFloat(float value) {
     47         mValue = value;
     48     }
     49 
     50     /**
     51      * Creates an ObservableFloat with the initial value of <code>0f</code>.
     52      */
     53     public ObservableFloat() {
     54     }
     55 
     56     /**
     57      * @return the stored value.
     58      */
     59     public float get() {
     60         return mValue;
     61     }
     62 
     63     /**
     64      * Set the stored value.
     65      */
     66     public void set(float 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.writeFloat(mValue);
     81     }
     82 
     83     public static final Parcelable.Creator<ObservableFloat> CREATOR
     84             = new Parcelable.Creator<ObservableFloat>() {
     85 
     86         @Override
     87         public ObservableFloat createFromParcel(Parcel source) {
     88             return new ObservableFloat(source.readFloat());
     89         }
     90 
     91         @Override
     92         public ObservableFloat[] newArray(int size) {
     93             return new ObservableFloat[size];
     94         }
     95     };
     96 }
     97