Skip to content

Instantly share code, notes, and snippets.

@fedefernandez
Created June 20, 2014 10:10
Show Gist options
  • Save fedefernandez/e30fc7989cc404719b3a to your computer and use it in GitHub Desktop.
Save fedefernandez/e30fc7989cc404719b3a to your computer and use it in GitHub Desktop.
Parcelable Utility
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public final class ParcelableUtil {
private ParcelableUtil() {
}
public static void writeString(Parcel dest, String value) {
if (value == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeString(value);
}
}
public static String readString(Parcel in) {
String value = null;
if (in.readByte() == 1) {
value = in.readString();
}
return value;
}
public static void writeFloat(Parcel dest, Float value) {
if (value == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeFloat(value);
}
}
public static Float readFloat(Parcel in) {
Float value = null;
if (in.readByte() == 1) {
value = in.readFloat();
}
return value;
}
public static void writeDate(Parcel dest, Date value) {
if (value == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeLong(value.getTime());
}
}
public static Date readDate(Parcel in) {
Date value = null;
if (in.readByte() == 1) {
value = new Date(in.readLong());
}
return value;
}
public static void writeStringArray(Parcel dest, String[] value) {
if (value == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeStringArray(value);
}
}
public static String[] readStringArray(Parcel in) {
String[] value = null;
if (in.readByte() == 1) {
value = in.createStringArray();
}
return value;
}
public static void writeList(Parcel dest, List<? extends Parcelable> value) {
if (value == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeTypedList(value);
}
}
public static <T> List<T> readList(Parcel in, Class<T> classObject, Parcelable.Creator<T> creator) {
List<T> value = null;
if (in.readByte() == 1) {
value = new ArrayList<T>();
in.readTypedList(value, creator);
}
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment