1 /* 2 * Copyright 2020 The Android Open Source Project 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 * http://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 17 package android.app.appsearch; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.app.appsearch.annotation.CanIgnoreReturnValue; 23 import android.app.appsearch.safeparcel.AbstractSafeParcelable; 24 import android.app.appsearch.safeparcel.SafeParcelable; 25 import android.os.Parcel; 26 import android.os.Parcelable; 27 import android.util.ArraySet; 28 29 import com.android.appsearch.flags.Flags; 30 31 import java.util.ArrayList; 32 import java.util.Arrays; 33 import java.util.Collection; 34 import java.util.Collections; 35 import java.util.List; 36 import java.util.Objects; 37 import java.util.Set; 38 39 /** 40 * Encapsulates a request to remove documents by namespace and IDs from the {@link AppSearchSession} 41 * database. 42 * 43 * @see AppSearchSession#remove 44 */ 45 @SafeParcelable.Class(creator = "RemoveByDocumentIdRequestCreator") 46 @SuppressWarnings("HiddenSuperclass") 47 public final class RemoveByDocumentIdRequest extends AbstractSafeParcelable { 48 /** Creator class for {@link android.app.appsearch.RemoveByDocumentIdRequest}. */ 49 @FlaggedApi(Flags.FLAG_ENABLE_SAFE_PARCELABLE_2) 50 @NonNull 51 public static final Parcelable.Creator<RemoveByDocumentIdRequest> CREATOR = 52 new RemoveByDocumentIdRequestCreator(); 53 54 @NonNull 55 @Field(id = 1, getter = "getNamespace") 56 private final String mNamespace; 57 58 @NonNull 59 @Field(id = 2) 60 final List<String> mIds; 61 62 @Nullable private Set<String> mIdsCached; 63 64 /** 65 * Removes documents by ID. 66 * 67 * @param namespace Namespace of the document to remove. 68 * @param ids The IDs of the documents to delete 69 */ 70 @Constructor RemoveByDocumentIdRequest( @aramid = 1) @onNull String namespace, @Param(id = 2) @NonNull List<String> ids)71 RemoveByDocumentIdRequest( 72 @Param(id = 1) @NonNull String namespace, @Param(id = 2) @NonNull List<String> ids) { 73 mNamespace = Objects.requireNonNull(namespace); 74 mIds = Objects.requireNonNull(ids); 75 } 76 77 /** Returns the namespace to remove documents from. */ 78 @NonNull getNamespace()79 public String getNamespace() { 80 return mNamespace; 81 } 82 83 /** Returns the set of document IDs attached to the request. */ 84 @NonNull getIds()85 public Set<String> getIds() { 86 if (mIdsCached == null) { 87 mIdsCached = Collections.unmodifiableSet(new ArraySet<>(mIds)); 88 } 89 return mIdsCached; 90 } 91 92 @FlaggedApi(Flags.FLAG_ENABLE_SAFE_PARCELABLE_2) 93 @Override writeToParcel(@onNull Parcel dest, int flags)94 public void writeToParcel(@NonNull Parcel dest, int flags) { 95 RemoveByDocumentIdRequestCreator.writeToParcel(this, dest, flags); 96 } 97 98 /** Builder for {@link RemoveByDocumentIdRequest} objects. */ 99 public static final class Builder { 100 private final String mNamespace; 101 private ArraySet<String> mIds = new ArraySet<>(); 102 private boolean mBuilt = false; 103 104 /** Creates a {@link RemoveByDocumentIdRequest.Builder} instance. */ Builder(@onNull String namespace)105 public Builder(@NonNull String namespace) { 106 mNamespace = Objects.requireNonNull(namespace); 107 } 108 109 /** Adds one or more document IDs to the request. */ 110 @CanIgnoreReturnValue 111 @NonNull addIds(@onNull String... ids)112 public Builder addIds(@NonNull String... ids) { 113 Objects.requireNonNull(ids); 114 resetIfBuilt(); 115 return addIds(Arrays.asList(ids)); 116 } 117 118 /** Adds a collection of IDs to the request. */ 119 @CanIgnoreReturnValue 120 @NonNull addIds(@onNull Collection<String> ids)121 public Builder addIds(@NonNull Collection<String> ids) { 122 Objects.requireNonNull(ids); 123 resetIfBuilt(); 124 mIds.addAll(ids); 125 return this; 126 } 127 128 /** Builds a new {@link RemoveByDocumentIdRequest}. */ 129 @NonNull build()130 public RemoveByDocumentIdRequest build() { 131 mBuilt = true; 132 return new RemoveByDocumentIdRequest(mNamespace, new ArrayList<>(mIds)); 133 } 134 resetIfBuilt()135 private void resetIfBuilt() { 136 if (mBuilt) { 137 mIds = new ArraySet<>(mIds); 138 mBuilt = false; 139 } 140 } 141 } 142 } 143