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