• 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 android.util.Pair;
21 import com.google.android.enterprise.connectedapps.internal.Bundler;
22 import com.google.android.enterprise.connectedapps.internal.BundlerType;
23 
24 /** Wrapper for reading & writing {@link Pair} instances from and to {@link Parcel} instances. */
25 public class ParcelablePair<F, S> implements Parcelable {
26 
27   private static final int NULL = -1;
28   private static final int NOT_NULL = 1;
29 
30   private final Bundler bundler;
31   private final BundlerType type;
32   private final Pair<F, S> pair;
33 
34   /**
35    * Create a wrapper for a given pair.
36    *
37    * <p>The passed in {@link Bundler} must be capable of bundling {@code E} and {@code F}.
38    */
of(Bundler bundler, BundlerType type, Pair<F, S> pair)39   public static <F, S> ParcelablePair<F, S> of(Bundler bundler, BundlerType type, Pair<F, S> pair) {
40     return new ParcelablePair<>(bundler, type, pair);
41   }
42 
get()43   public Pair<F, S> get() {
44     return pair;
45   }
46 
ParcelablePair(Bundler bundler, BundlerType type, Pair<F, S> pair)47   private ParcelablePair(Bundler bundler, BundlerType type, Pair<F, S> pair) {
48     if (bundler == null || type == null) {
49       throw new NullPointerException();
50     }
51     this.bundler = bundler;
52     this.type = type;
53     this.pair = pair;
54   }
55 
ParcelablePair(Parcel in)56   private ParcelablePair(Parcel in) {
57     bundler = in.readParcelable(Bundler.class.getClassLoader());
58     int present = in.readInt();
59 
60     if (present == NULL) {
61       type = null;
62       pair = null;
63       return;
64     }
65 
66     type = (BundlerType) in.readParcelable(Bundler.class.getClassLoader());
67     BundlerType fType = type.typeArguments().get(0);
68     BundlerType sType = type.typeArguments().get(1);
69 
70     @SuppressWarnings("unchecked")
71     F first = (F) bundler.readFromParcel(in, fType);
72     @SuppressWarnings("unchecked")
73     S second = (S) bundler.readFromParcel(in, sType);
74 
75     pair = new Pair<>(first, second);
76   }
77 
78   @Override
writeToParcel(Parcel dest, int flags)79   public void writeToParcel(Parcel dest, int flags) {
80     dest.writeParcelable(bundler, flags);
81 
82     if (pair == null) {
83       dest.writeInt(NULL);
84       return;
85     }
86 
87     dest.writeInt(NOT_NULL);
88     dest.writeParcelable(type, flags);
89 
90     BundlerType fType = type.typeArguments().get(0);
91     BundlerType sType = type.typeArguments().get(1);
92 
93     bundler.writeToParcel(dest, pair.first, fType, flags);
94     bundler.writeToParcel(dest, pair.second, sType, flags);
95   }
96 
97   @Override
describeContents()98   public int describeContents() {
99     return 0;
100   }
101 
102   @SuppressWarnings("rawtypes")
103   public static final Creator<ParcelablePair> CREATOR =
104       new Creator<ParcelablePair>() {
105         @Override
106         public ParcelablePair createFromParcel(Parcel in) {
107           return new ParcelablePair(in);
108         }
109 
110         @Override
111         public ParcelablePair[] newArray(int size) {
112           return new ParcelablePair[size];
113         }
114       };
115 }
116