• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 java.util.HashMap;
23 import java.util.Map;
24 
25 /** Wrapper for reading & writing {@link Map} instances from and to {@link Parcel} instances. */
26 public class ParcelableMap<E, F> implements Parcelable {
27 
28   private static final int NULL_SIZE = -1;
29 
30   private final Bundler bundler;
31   private final BundlerType type;
32   private final Map<E, F> map;
33 
34   /**
35    * Create a wrapper for a given map.
36    *
37    * <p>The passed in {@link Bundler} must be capable of bundling {@code E} and {@code F}.
38    */
of(Bundler bundler, BundlerType type, Map<E, F> map)39   public static <E, F> ParcelableMap<E, F> of(Bundler bundler, BundlerType type, Map<E, F> map) {
40     return new ParcelableMap<>(bundler, type, map);
41   }
42 
get()43   public Map<E, F> get() {
44     return map;
45   }
46 
ParcelableMap(Bundler bundler, BundlerType type, Map<E, F> map)47   private ParcelableMap(Bundler bundler, BundlerType type, Map<E, F> map) {
48     if (bundler == null || type == null) {
49       throw new NullPointerException();
50     }
51     this.bundler = bundler;
52     this.type = type;
53     this.map = map;
54   }
55 
ParcelableMap(Parcel in)56   private ParcelableMap(Parcel in) {
57     bundler = in.readParcelable(Bundler.class.getClassLoader());
58     int size = in.readInt();
59 
60     if (size == NULL_SIZE) {
61       type = null;
62       map = null;
63       return;
64     }
65 
66     map = new HashMap<>();
67     type = (BundlerType) in.readParcelable(Bundler.class.getClassLoader());
68     if (size > 0) {
69       BundlerType keyType = type.typeArguments().get(0);
70       BundlerType valueType = type.typeArguments().get(1);
71       for (int i = 0; i < size; i++) {
72         @SuppressWarnings("unchecked")
73         E key = (E) bundler.readFromParcel(in, keyType);
74         @SuppressWarnings("unchecked")
75         F value = (F) bundler.readFromParcel(in, valueType);
76         map.put(key, value);
77       }
78     }
79   }
80 
81   @Override
writeToParcel(Parcel dest, int flags)82   public void writeToParcel(Parcel dest, int flags) {
83     dest.writeParcelable(bundler, flags);
84 
85     if (map == null) {
86       dest.writeInt(NULL_SIZE);
87       return;
88     }
89 
90     dest.writeInt(map.size());
91     dest.writeParcelable(type, flags);
92     if (!map.isEmpty()) {
93       BundlerType keyType = type.typeArguments().get(0);
94       BundlerType valueType = type.typeArguments().get(1);
95 
96       for (Map.Entry<E, F> entry : map.entrySet()) {
97         bundler.writeToParcel(dest, entry.getKey(), keyType, flags);
98         bundler.writeToParcel(dest, entry.getValue(), valueType, flags);
99       }
100     }
101   }
102 
103   @Override
describeContents()104   public int describeContents() {
105     return 0;
106   }
107 
108   @SuppressWarnings("rawtypes")
109   public static final Creator<ParcelableMap> CREATOR =
110       new Creator<ParcelableMap>() {
111         @Override
112         public ParcelableMap createFromParcel(Parcel in) {
113           return new ParcelableMap(in);
114         }
115 
116         @Override
117         public ParcelableMap[] newArray(int size) {
118           return new ParcelableMap[size];
119         }
120       };
121 }
122