1 /* 2 * Copyright 2021 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.android.enterprise.connectedapps.parcelablewrappers; 17 18 import android.os.Parcel; 19 import android.os.Parcelable; 20 import com.google.android.enterprise.connectedapps.internal.Bundler; 21 import com.google.android.enterprise.connectedapps.internal.BundlerType; 22 import com.google.common.collect.ImmutableMap; 23 24 /** 25 * Wrapper for reading & writing {@link ImmutableMap} instances from and to {@link Parcel} 26 * instances. 27 */ 28 public class ParcelableImmutableMap<E, F> implements Parcelable { 29 30 private static final int NULL_SIZE = -1; 31 private static final int KEY_TYPE_INDEX = 0; 32 private static final int VALUE_TYPE_INDEX = 1; 33 34 private final Bundler bundler; 35 private final BundlerType type; 36 private final ImmutableMap<E, F> map; 37 38 /** 39 * Create a wrapper for a given immutable map. 40 * 41 * <p>The passed in {@link Bundler} must be capable of bundling {@code E} and {@code F}. 42 */ of( Bundler bundler, BundlerType type, ImmutableMap<E, F> map)43 public static <E, F> ParcelableImmutableMap<E, F> of( 44 Bundler bundler, BundlerType type, ImmutableMap<E, F> map) { 45 return new ParcelableImmutableMap<>(bundler, type, map); 46 } 47 get()48 public ImmutableMap<E, F> get() { 49 return map; 50 } 51 ParcelableImmutableMap(Bundler bundler, BundlerType type, ImmutableMap<E, F> map)52 private ParcelableImmutableMap(Bundler bundler, BundlerType type, ImmutableMap<E, F> map) { 53 if (bundler == null || type == null) { 54 throw new NullPointerException(); 55 } 56 this.bundler = bundler; 57 this.type = type; 58 this.map = map; 59 } 60 ParcelableImmutableMap(Parcel in)61 private ParcelableImmutableMap(Parcel in) { 62 bundler = in.readParcelable(Bundler.class.getClassLoader()); 63 int size = in.readInt(); 64 65 if (size == NULL_SIZE) { 66 type = null; 67 map = null; 68 return; 69 } 70 71 ImmutableMap.Builder<E, F> mapBuilder = ImmutableMap.builder(); 72 73 type = (BundlerType) in.readParcelable(Bundler.class.getClassLoader()); 74 if (size > 0) { 75 BundlerType keyType = type.typeArguments().get(KEY_TYPE_INDEX); 76 BundlerType valueType = type.typeArguments().get(VALUE_TYPE_INDEX); 77 for (int i = 0; i < size; i++) { 78 @SuppressWarnings("unchecked") 79 E key = (E) bundler.readFromParcel(in, keyType); 80 @SuppressWarnings("unchecked") 81 F value = (F) bundler.readFromParcel(in, valueType); 82 mapBuilder.put(key, value); 83 } 84 } 85 86 map = mapBuilder.build(); 87 } 88 89 @Override writeToParcel(Parcel dest, int flags)90 public void writeToParcel(Parcel dest, int flags) { 91 dest.writeParcelable(bundler, flags); 92 93 if (map == null) { 94 dest.writeInt(NULL_SIZE); 95 return; 96 } 97 98 dest.writeInt(map.size()); 99 dest.writeParcelable(type, flags); 100 if (!map.isEmpty()) { 101 BundlerType keyType = type.typeArguments().get(0); 102 BundlerType valueType = type.typeArguments().get(1); 103 104 for (ImmutableMap.Entry<E, F> entry : map.entrySet()) { 105 bundler.writeToParcel(dest, entry.getKey(), keyType, flags); 106 bundler.writeToParcel(dest, entry.getValue(), valueType, flags); 107 } 108 } 109 } 110 111 @Override describeContents()112 public int describeContents() { 113 return 0; 114 } 115 116 @SuppressWarnings("rawtypes") 117 public static final Creator<ParcelableImmutableMap> CREATOR = 118 new Creator<ParcelableImmutableMap>() { 119 @Override 120 public ParcelableImmutableMap createFromParcel(Parcel in) { 121 return new ParcelableImmutableMap(in); 122 } 123 124 @Override 125 public ParcelableImmutableMap[] newArray(int size) { 126 return new ParcelableImmutableMap[size]; 127 } 128 }; 129 } 130