package waymaker.gen; // Copyright © 2015 Michael Allan. Licence MIT. import android.os.*; /** Utilities for working with parcels. * * @see android.os.Parcel */ public @ThreadSafe final class ParcelX { private ParcelX() {} /** Writes a boolean value to a parcel. */ public static void writeBoolean( final boolean value, final Parcel out ) { out.writeByte( value? (byte)1: (byte)0 ); } /** Reads a boolean value from a parcel. */ public static boolean readBoolean( final Parcel in ) { return in.readByte() != 0; } /** Writes a parcelable to a parcel without specifying any flags. */ public static void writeParcelable( final Parcelable value, final Parcel out ) { out.writeParcelable( value, /*flags*/0 ); } /** Reads a parcelable from a parcel using ParcelX’s own class loader. * * @param The type of parcelable. */ public static T readParcelable( final Parcel in ) { return in.readParcelable( classLoader ); /* cannot simply pass null here, and use the default class loader, else "Class not found using the boot class loader" */ } /** Writes the string to the parcel with efficient handling for a frequent default instance. The * string is considered at default if s == sDefault. */ public static void writeString( final String s, final Parcel out, final String sDefault ) { // 1. Is default? // - - - - - - - - final boolean isDefault = s == sDefault; // == for speed, not equals ParcelX.writeBoolean( isDefault, out ); // 2. Waynode // - - - - - - if( !isDefault ) out.writeString( s ); } /** Reads a string from the parcel with efficient handling for a frequent default instance. */ public static String readString( final Parcel in, final String sDefault ) { // 1. // - - - if( ParcelX.readBoolean( in )) return sDefault; // 2. // - - - return in.readString(); } //// P r i v a t e ///////////////////////////////////////////////////////////////////////////////////// private static final ClassLoader classLoader = ParcelX.class.getClassLoader(); }