public static <T> void writeArrayListToBundle(ArrayList<T> list, Bundle bundle, Class<T> itemClass, String key) { getListBundlerForItemClass(itemClass).writeToBundle(list, bundle, key); }
public static <T> ArrayList<T> readArrayListFromBundle(Bundle bundle, Class<T> itemClass, String key) { return getListBundlerForItemClass(itemClass).readFromBundle(bundle, key); }
@SuppressWarnings("unchecked") public static void readCollectionFromBundle(Collection collection, Bundle bundle, Class itemClass, String key) { collection.addAll(ArrayListBundler.readArrayListFromBundle(bundle, itemClass, key)); } }
@SuppressWarnings("unchecked") public static void writeCollectionToBundle(Collection collection, Bundle bundle, Class itemClass, String key) { ArrayList arrayList = collection instanceof ArrayList ? (ArrayList) collection : new ArrayList(collection); ArrayListBundler.writeArrayListToBundle(arrayList, bundle, itemClass, key); }
@Test public void testEmptyMapDoesNotThrowException() { Map<String, String> map = new HashMap<>(); Bundle bundle = new Bundle(); MapBundler.writeMapToBundle(map, bundle, String.class, String.class, BUNDLE_KEY); map.clear(); MapBundler.readMapFromBundle(map, bundle, String.class, String.class, BUNDLE_KEY); assertTrue("map.isEmpty()", map.isEmpty()); }
/** * Reads an object of the specified class from the given Parcel. * * @param clazz The class of the object to be read. * @param parcel The source Parcel. * @param <T> The type of the Object. * * @return A new instance of the specified type instantiated from the Parcel. * * @throws PostmanException if there is no {@link Parceler} associated with the given type. */ public static <T> T readFromParcel(Class<T> clazz, Parcel parcel) throws PostmanException { Parceler<T> parceler = getParcelerForClass(clazz); return parceler.readFromParcel(parcel); }
/** * Creates of new array of the specified type with the specified size. * * @param clazz The class associated with the type of array to create. * @param size The desired size of the array. * @param <T> The type of the array to create. * * @return A new array of the specified type with the specified size. * * @throws PostmanException if there is no {@link Parceler} associated with the given type. * @see Creator#newArray(int) */ public static <T> T[] newArray(Class<T> clazz, int size) throws PostmanException { Parceler<T> parceler = getParcelerForClass(clazz); return parceler.newArray(size); }
/** * Write the specified Object to a {@link Parcel}. * * @param object The Object to be written to the Parcel. * @param parcel The destination Parcel. * @param <T> The type of the Object. * * @throws PostmanException if there is no {@link Parceler} associated with the given type. */ public static <T> void writeToParcel(T object, Parcel parcel) throws PostmanException { @SuppressWarnings("unchecked") Parceler<T> parceler = getParcelerForClass((Class<T>) object.getClass()); parceler.writeToParcel(object, parcel); }
public static <T> void writeArrayListToBundle(ArrayList<T> list, Bundle bundle, Class<T> itemClass, String key) { getListBundlerForItemClass(itemClass).writeToBundle(list, bundle, key); }
public static <T> ArrayList<T> readArrayListFromBundle(Bundle bundle, Class<T> itemClass, String key) { return getListBundlerForItemClass(itemClass).readFromBundle(bundle, key); }
@SuppressWarnings("unchecked") public static void writeCollectionToBundle(Collection collection, Bundle bundle, Class itemClass, String key) { ArrayList arrayList = collection instanceof ArrayList ? (ArrayList) collection : new ArrayList(collection); ArrayListBundler.writeArrayListToBundle(arrayList, bundle, itemClass, key); }
@SuppressWarnings("unchecked") public static void readCollectionFromBundle(Collection collection, Bundle bundle, Class itemClass, String key) { collection.addAll(ArrayListBundler.readArrayListFromBundle(bundle, itemClass, key)); } }
@Test public void testStringStringMap() { Map<String, String> map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", "value2"); Bundle bundle = new Bundle(); MapBundler.writeMapToBundle(map, bundle, String.class, String.class, BUNDLE_KEY); map.clear(); MapBundler.readMapFromBundle(map, bundle, String.class, String.class, BUNDLE_KEY); assertEquals("value1", map.get("key1")); assertEquals("value2", map.get("key2")); }
/** * Reads an object of the specified class from the given Parcel. * * @param clazz The class of the object to be read. * @param parcel The source Parcel. * @param <T> The type of the Object. * * @return A new instance of the specified type instantiated from the Parcel. * * @throws PostmanException if there is no {@link Parceler} associated with the given type. */ public static <T> T readFromParcel(Class<T> clazz, Parcel parcel) throws PostmanException { Parceler<T> parceler = getParcelerForClass(clazz); return parceler.readFromParcel(parcel); }
/** * Creates of new array of the specified type with the specified size. * * @param clazz The class associated with the type of array to create. * @param size The desired size of the array. * @param <T> The type of the array to create. * * @return A new array of the specified type with the specified size. * * @throws PostmanException if there is no {@link Parceler} associated with the given type. * @see Creator#newArray(int) */ public static <T> T[] newArray(Class<T> clazz, int size) throws PostmanException { Parceler<T> parceler = getParcelerForClass(clazz); return parceler.newArray(size); }
/** * Write the specified Object to a {@link Parcel}. * * @param object The Object to be written to the Parcel. * @param parcel The destination Parcel. * @param <T> The type of the Object. * * @throws PostmanException if there is no {@link Parceler} associated with the given type. */ public static <T> void writeToParcel(T object, Parcel parcel) throws PostmanException { @SuppressWarnings("unchecked") Parceler<T> parceler = getParcelerForClass((Class<T>) object.getClass()); parceler.writeToParcel(object, parcel); }
/** * Write the given map to bundle, using the given {@code bundleKey} to identify it. * * @param map The Map to write. * @param bundle The Bundle to which the Map will be written. * @param keyClass The class representing the type for the map keys. * @param valueClass The class representing the type for the map values. * @param bundleKey The string used to identify the map for later retrieval (see {@link * Bundle#get(String)}). * @param <K> The type for the map keys. * @param <V> The type for the map values. */ public static <K, V> void writeMapToBundle(Map<K, V> map, Bundle bundle, Class<K> keyClass, Class<V> valueClass, String bundleKey) { ArrayList<K> keys = new ArrayList<>(map.size()); ArrayList<V> values = new ArrayList<>(map.size()); for (Map.Entry<K, V> entry : map.entrySet()) { keys.add(entry.getKey()); values.add(entry.getValue()); } ArrayListBundler.writeArrayListToBundle(keys, bundle, keyClass, bundleKey + KEYS_SUFFIX); ArrayListBundler.writeArrayListToBundle(values, bundle, valueClass, bundleKey + VALUES_SUFFIX); }
String bundleKey) { ArrayList<K> keys = ArrayListBundler.readArrayListFromBundle(bundle, keyClass, bundleKey + KEYS_SUFFIX); ArrayList<V> values = ArrayListBundler.readArrayListFromBundle(bundle, valueClass, bundleKey + VALUES_SUFFIX);
@Test public void testIntParcelableMap() { Map<Integer, MyParcelable> map = new HashMap<>(); MyParcelable value1 = new MyParcelable(); MyParcelable value2 = new MyParcelable(); map.put(1, value1); map.put(2, value2); Bundle bundle = new Bundle(); MapBundler.writeMapToBundle(map, bundle, Integer.class, MyParcelable.class, BUNDLE_KEY); map.clear(); MapBundler.readMapFromBundle(map, bundle, Integer.class, MyParcelable.class, BUNDLE_KEY); Assert.assertEquals(value1, map.get(1)); Assert.assertEquals(value2, map.get(2)); }
/** * Write the given map to bundle, using the given {@code bundleKey} to identify it. * * @param map The Map to write. * @param bundle The Bundle to which the Map will be written. * @param keyClass The class representing the type for the map keys. * @param valueClass The class representing the type for the map values. * @param bundleKey The string used to identify the map for later retrieval (see {@link * Bundle#get(String)}). * @param <K> The type for the map keys. * @param <V> The type for the map values. */ public static <K, V> void writeMapToBundle(Map<K, V> map, Bundle bundle, Class<K> keyClass, Class<V> valueClass, String bundleKey) { ArrayList<K> keys = new ArrayList<>(map.size()); ArrayList<V> values = new ArrayList<>(map.size()); for (Map.Entry<K, V> entry : map.entrySet()) { keys.add(entry.getKey()); values.add(entry.getValue()); } ArrayListBundler.writeArrayListToBundle(keys, bundle, keyClass, bundleKey + KEYS_SUFFIX); ArrayListBundler.writeArrayListToBundle(values, bundle, valueClass, bundleKey + VALUES_SUFFIX); }