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 long. 25 * <p> 26 * Observable field classes may be used instead of creating an Observable object: 27 * <pre><code>public class MyDataObject { 28 * public final ObservableLong friendCount = new ObservableLong(); 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 ObservableLong extends BaseObservable implements Parcelable, Serializable { 38 static final long serialVersionUID = 1L; 39 private long mValue; 40 41 /** 42 * Creates an ObservableLong with the given initial value. 43 * 44 * @param value the initial value for the ObservableLong 45 */ ObservableLong(long value)46 public ObservableLong(long value) { 47 mValue = value; 48 } 49 50 /** 51 * Creates an ObservableLong with the initial value of <code>0L</code>. 52 */ ObservableLong()53 public ObservableLong() { 54 } 55 56 /** 57 * @return the stored value. 58 */ get()59 public long get() { 60 return mValue; 61 } 62 63 /** 64 * Set the stored value. 65 */ set(long value)66 public void set(long value) { 67 if (value != mValue) { 68 mValue = value; 69 notifyChange(); 70 } 71 } 72 73 @Override describeContents()74 public int describeContents() { 75 return 0; 76 } 77 78 @Override writeToParcel(Parcel dest, int flags)79 public void writeToParcel(Parcel dest, int flags) { 80 dest.writeLong(mValue); 81 } 82 83 public static final Parcelable.Creator<ObservableLong> CREATOR 84 = new Parcelable.Creator<ObservableLong>() { 85 86 @Override 87 public ObservableLong createFromParcel(Parcel source) { 88 return new ObservableLong(source.readLong()); 89 } 90 91 @Override 92 public ObservableLong[] newArray(int size) { 93 return new ObservableLong[size]; 94 } 95 }; 96 } 97