下面列出了android.os.Parcelable#Creator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Nullable
public static <T> T unmarshall(@NonNull final byte[] bytes, @NonNull final Parcelable.Creator<T> creator) {
final Parcel parcel = unmarshall(bytes);
final T result = creator.createFromParcel(parcel);
parcel.recycle();
return result;
}
private static <T> T bytes2Parcelable(byte[] bytes, Parcelable.Creator<T> creator) {
if (bytes == null) return null;
Parcel parcel = Parcel.obtain();
parcel.unmarshall(bytes, 0, bytes.length);
parcel.setDataPosition(0);
T result = creator.createFromParcel(parcel);
parcel.recycle();
return result;
}
/**
* Factory method for {@link Parcelable.Creator}.
*
* @param callbacks Creator callbacks implementation.
* @return New creator.
*/
public static <T> Parcelable.Creator<T> newCreator(
ParcelableCompatCreatorCallbacks<T> callbacks) {
if (android.os.Build.VERSION.SDK_INT >= 13) {
ParcelableCompatCreatorHoneycombMR2Stub.instantiate(callbacks);
}
return new CompatCreator<T>(callbacks);
}
public static <T> T deserialize(byte[] bytes, Parcelable.Creator<T> creator) {
Parcel parcel = deserialize(bytes);
return creator.createFromParcel(parcel);
}
@Override
protected Parcelable.Creator<Alarm> getParcelableCreator() {
return Alarm.CREATOR;
}
public static Parcelable.Creator<Posts> getCREATOR () {
return CREATOR;
}
public static <T> T parcelAndCreate(Parcelable parcelable, Parcelable.Creator<T> creator) {
Parcel parcel = Parcel.obtain();
parcelable.writeToParcel(parcel, /* flags= */ 0);
parcel.setDataPosition(0);
return creator.createFromParcel(parcel);
}
public static <T> T unmarshall(byte[] bytes, Parcelable.Creator<T> creator) {
Parcel parcel = unmarshall(bytes);
T result = creator.createFromParcel(parcel);
parcel.recycle();
return result;
}
@Override
protected Parcelable.Creator<Timer> getParcelableCreator() {
return Timer.CREATOR;
}
public static Parcelable.Creator<Topics> getCREATOR () {
return CREATOR;
}
static <T> T bytes2Parcelable(final byte[] bytes,
final Parcelable.Creator<T> creator) {
return ConvertUtils.bytes2Parcelable(bytes, creator);
}
public static <T> T unmarshall(byte[] bytes, Parcelable.Creator<T> creator) {
Parcel parcel = unmarshall(bytes);
return creator.createFromParcel(parcel);
}
public ParcelableAdapter(@Nullable Parcelable.Creator<T> creator) {
this.creator = creator;
}
/**
* Return the parcelable in cache.
*
* @param key The key of cache.
* @param creator The creator.
* @param defaultValue The default value if the cache doesn't exist.
* @param <T> The value type.
* @return the parcelable if cache exists or defaultValue otherwise
*/
public <T> T getParcelable(@NonNull final String key,
@NonNull final Parcelable.Creator<T> creator,
final T defaultValue) {
byte[] bytes = realGetBytes(TYPE_PARCELABLE + key);
if (bytes == null) return defaultValue;
return UtilsBridge.bytes2Parcelable(bytes, creator);
}
/**
* Get a {@link Creator} that can be used for the {@code CREATOR} field of a {@link
* Parcelable}.
*
* @param clazz The class associated with the type the the Creator will create.
* @param <T> The type the Creator will create.
*
* @return A fully implemented Creator for the specified type.
*
* @throws PostmanException if there is no {@link Parceler} associated with the given type.
*/
public static <T> Parcelable.Creator<T> getCreator(Class<T> clazz) throws PostmanException {
@SuppressWarnings("unchecked")
Parcelable.Creator<T> creator = (Parcelable.Creator<T>) creatorMap.get(clazz);
if (creator == null) {
creator = newCreator(clazz);
creatorMap.put(clazz, creator);
}
return creator;
}
/**
* 缓存中读取Parcelable
*
* @param key 键
* @param creator 建造器
* @return 存在且没过期返回对应值,否则返回{@code null}
*/
public <T> T getParcelable(@NonNull String key, @NonNull Parcelable.Creator<T> creator) {
return getParcelable(key, creator, null);
}
/**
* 缓存中读取Parcelable
*
* @param key 键
* @param creator 建造器
* @param defaultValue 默认值
* @return 存在且没过期返回对应值,否则返回默认值{@code defaultValue}
*/
public <T> T getParcelable(@NonNull String key, @NonNull Parcelable.Creator<T> creator, T defaultValue) {
byte[] bytes = getBytes(key);
if (bytes == null) return defaultValue;
return CacheHelper.bytes2Parcelable(bytes, creator);
}
/**
* Return the parcelable in cache.
*
* @param key The key of cache.
* @param creator The creator.
* @param defaultValue The default value if the cache doesn't exist.
* @param <T> The value type.
* @return the parcelable if cache exists or defaultValue otherwise
*/
public static <T> T getParcelable(@NonNull final String key,
@NonNull final Parcelable.Creator<T> creator,
final T defaultValue) {
return getParcelable(key, creator, defaultValue, getDefaultCacheDiskUtils());
}
/**
* Return the parcelable in cache.
*
* @param key The key of cache.
* @param creator The creator.
* @param cacheDiskUtils The instance of {@link CacheDiskUtils}.
* @param <T> The value type.
* @return the parcelable if cache exists or null otherwise
*/
public static <T> T getParcelable(@NonNull final String key,
@NonNull final Parcelable.Creator<T> creator,
@NonNull final CacheDiskUtils cacheDiskUtils) {
return cacheDiskUtils.getParcelable(key, creator);
}
protected abstract Parcelable.Creator<?> readParcelableCreator(Parcel from, ClassLoader loader);