package io.particle.android.sdk.utils;


import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

import com.google.common.collect.Lists;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.ParametersAreNonnullByDefault;


@ParametersAreNonnullByDefault
public class Parcelables {

    public static boolean readBoolean(Parcel parcel) {
        return (parcel.readInt() != 0);
    }


    public static void writeBoolean(Parcel parcel, boolean value) {
        parcel.writeInt(value ? 1 : 0);
    }


    public static List<String> readStringList(Parcel parcel) {
        List<String> sourceList = Lists.newArrayList();
        parcel.readStringList(sourceList);
        return sourceList;
    }


    public static Map<String, String> readStringMap(Parcel parcel) {
        Map<String, String> map = new HashMap<>();
        Bundle bundle = parcel.readBundle(Parcelables.class.getClassLoader());
        for (String key : bundle.keySet()) {
            map.put(key, bundle.getString(key));
        }
        return map;
    }

    public static void writeStringMap(Parcel parcel, Map<String, String> stringMap) {
        Bundle b = new Bundle();
        for (Map.Entry<String, String> entry : stringMap.entrySet()) {
            b.putString(entry.getKey(), entry.getValue());
        }
        parcel.writeBundle(b);
    }

    public static <T extends Parcelable> Map<String, T> readParcelableMap(Parcel parcel) {
        Map<String, T> map = new HashMap<>();
        Bundle bundle = parcel.readBundle(Parcelables.class.getClassLoader());
        for (String key : bundle.keySet()) {
            T parcelable = bundle.getParcelable(key);
            map.put(key, parcelable);
        }
        return map;
    }

    public static <T extends Parcelable> void writeParcelableMap(Parcel parcel, Map<String, T> map) {
        Bundle b = new Bundle();
        for (Map.Entry<String, T> entry : map.entrySet()) {
            b.putParcelable(entry.getKey(), entry.getValue());
        }
        parcel.writeBundle(b);
    }


    public static <T extends Serializable> Map<String, T> readSerializableMap(Parcel parcel) {
        Map<String, T> map = new HashMap<>();
        Bundle bundle = parcel.readBundle(Parcelables.class.getClassLoader());
        for (String key : bundle.keySet()) {
            @SuppressWarnings("unchecked")
            T serializable = (T) bundle.getSerializable(key);
            map.put(key, serializable);
        }
        return map;
    }

    public static <T extends Serializable> void writeSerializableMap(Parcel parcel, Map<String, T> map) {
        Bundle b = new Bundle();
        for (Map.Entry<String, T> entry : map.entrySet()) {
            b.putSerializable(entry.getKey(), entry.getValue());
        }
        parcel.writeBundle(b);
    }

}

@@ -156,16 +156,57 @@
 t.Lists;
+%0Aimport android.support.v4.util.ArrayMap;
 %0A%0Aimport
@@ -232,34 +232,8 @@
 le;%0A
-import java.util.HashMap;%0A
 impo
@@ -905,36 +905,37 @@
 ring%3E map = new 
-Hash
+Array
 Map%3C%3E();%0A       
@@ -1550,36 +1550,37 @@
 g, T%3E map = new 
-Hash
+Array
 Map%3C%3E();%0A       
@@ -2123,33 +2123,32 @@
 ndle(b);%0A    %7D%0A%0A
-%0A
     public stati
@@ -2263,12 +2263,13 @@
 new 
-Hash
+Array
 Map%3C

Use ArrayMaps instead of HashMap, for memory and CPU savings