com.workday.postman.adapter.AbstractMapParcelableAdapter Maven / Gradle / Ivy
/*
* Copyright 2015 Workday, Inc.
*
* This software is available under the MIT license.
* Please see the LICENSE.txt file in this project.
*/
package com.workday.postman.adapter;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.Locale;
import java.util.Map;
/**
* The base class for all {@link ParcelableAdapter}s that wrap Maps. Subclasses need only delcare
* the concrete Map type they wrap (for parameter {@link T}), declare a matching constructor, and
* declare a public constant {@code CREATOR} of type {@link com.workday.postman.adapter
* .AbstractMapParcelableAdapter.Creator}. This base class will handle wrapping the Map and writing
* it to the Parcel.
*
* @param The concrete Map type this ParcelableAdapter wraps.
*
* @author nathan.taylor
* @since 2015-08-28.
*/
public class AbstractMapParcelableAdapter implements ParcelableAdapter {
/**
* The base implementation of {@link android.os.Parcelable.Creator} for Map ParcelableAdapters.
* This base class will handle unwrapping the Map written to the Parcel.
*
* @param The concrete Map type the ParcelableAdapter wraps.
* @param The concrete ParcelableAdapter this Creator will create.
*/
public abstract static class Creator>
implements Parcelable.Creator {
@Override
public final A createFromParcel(Parcel source) {
final Parcelable[] keys =
source.readParcelableArray(ParcelableAdapter.class.getClassLoader());
final Parcelable[] values =
source.readParcelableArray(ParcelableAdapter.class.getClassLoader());
if (keys.length != values.length) {
final String message = String.format(Locale.US,
"Length of keys array (%d) does not match "
+ "length of values array (%d)",
keys.length,
values.length);
throw new IllegalStateException(message);
}
final M map = newMapInstance();
@SuppressWarnings("unchecked")
final Map