Skip to content

Commit 47b78b4

Browse files
committed
Make Entry class Parcelable.
As an Entry object may contain arbitrary data (in the form of a single object), I took the route of checking if that object is also parcelable and parceling it if so. If the object is not parcelable, then an Exception is thrown.
1 parent 7b27f47 commit 47b78b4

1 file changed

Lines changed: 44 additions & 1 deletion

File tree

  • MPChartLib/src/com/github/mikephil/charting/data

MPChartLib/src/com/github/mikephil/charting/data/Entry.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11

22
package com.github.mikephil.charting.data;
33

4+
import android.os.Parcel;
5+
import android.os.ParcelFormatException;
6+
import android.os.Parcelable;
7+
48
/**
59
* Class representing one entry in the chart. Might contain multiple values.
610
* Might only contain a single value depending on the used constructor.
711
*
812
* @author Philipp Jahoda
913
*/
10-
public class Entry {
14+
public class Entry implements Parcelable {
1115

1216
/** the actual value */
1317
private float mVal = 0f;
@@ -142,4 +146,43 @@ public boolean equalTo(Entry e) {
142146
public String toString() {
143147
return "Entry, xIndex: " + mXIndex + " val (sum): " + getVal();
144148
}
149+
150+
@Override
151+
public int describeContents() {
152+
return 0;
153+
}
154+
155+
@Override
156+
public void writeToParcel(Parcel dest, int flags) {
157+
dest.writeFloat(this.mVal);
158+
dest.writeInt(this.mXIndex);
159+
if (mData != null) {
160+
if (mData instanceof Parcelable) {
161+
dest.writeInt(1);
162+
dest.writeParcelable((Parcelable) this.mData, flags);
163+
} else {
164+
throw new ParcelFormatException("Cannot parcel an Entry with non-parcelable data");
165+
}
166+
} else {
167+
dest.writeInt(0);
168+
}
169+
}
170+
171+
protected Entry(Parcel in) {
172+
this.mVal = in.readFloat();
173+
this.mXIndex = in.readInt();
174+
if (in.readInt() == 1) {
175+
this.mData = in.readParcelable(Object.class.getClassLoader());
176+
}
177+
}
178+
179+
public static final Parcelable.Creator<Entry> CREATOR = new Parcelable.Creator<Entry>() {
180+
public Entry createFromParcel(Parcel source) {
181+
return new Entry(source);
182+
}
183+
184+
public Entry[] newArray(int size) {
185+
return new Entry[size];
186+
}
187+
};
145188
}

0 commit comments

Comments
 (0)