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.NonNull; 20 import android.app.appsearch.annotation.CanIgnoreReturnValue; 21 import android.util.ArraySet; 22 23 import java.util.Arrays; 24 import java.util.Collection; 25 import java.util.Collections; 26 import java.util.Objects; 27 import java.util.Set; 28 29 /** 30 * Encapsulates a request to remove documents by namespace and IDs from the {@link AppSearchSession} 31 * database. 32 * 33 * @see AppSearchSession#remove 34 */ 35 public final class RemoveByDocumentIdRequest { 36 private final String mNamespace; 37 private final Set<String> mIds; 38 RemoveByDocumentIdRequest(String namespace, Set<String> ids)39 RemoveByDocumentIdRequest(String namespace, Set<String> ids) { 40 mNamespace = namespace; 41 mIds = ids; 42 } 43 44 /** Returns the namespace to remove documents from. */ 45 @NonNull getNamespace()46 public String getNamespace() { 47 return mNamespace; 48 } 49 50 /** Returns the set of document IDs attached to the request. */ 51 @NonNull getIds()52 public Set<String> getIds() { 53 return Collections.unmodifiableSet(mIds); 54 } 55 56 /** Builder for {@link RemoveByDocumentIdRequest} objects. */ 57 public static final class Builder { 58 private final String mNamespace; 59 private ArraySet<String> mIds = new ArraySet<>(); 60 private boolean mBuilt = false; 61 62 /** Creates a {@link RemoveByDocumentIdRequest.Builder} instance. */ Builder(@onNull String namespace)63 public Builder(@NonNull String namespace) { 64 mNamespace = Objects.requireNonNull(namespace); 65 } 66 67 /** Adds one or more document IDs to the request. */ 68 @CanIgnoreReturnValue 69 @NonNull addIds(@onNull String... ids)70 public Builder addIds(@NonNull String... ids) { 71 Objects.requireNonNull(ids); 72 resetIfBuilt(); 73 return addIds(Arrays.asList(ids)); 74 } 75 76 /** Adds a collection of IDs to the request. */ 77 @CanIgnoreReturnValue 78 @NonNull addIds(@onNull Collection<String> ids)79 public Builder addIds(@NonNull Collection<String> ids) { 80 Objects.requireNonNull(ids); 81 resetIfBuilt(); 82 mIds.addAll(ids); 83 return this; 84 } 85 86 /** Builds a new {@link RemoveByDocumentIdRequest}. */ 87 @NonNull build()88 public RemoveByDocumentIdRequest build() { 89 mBuilt = true; 90 return new RemoveByDocumentIdRequest(mNamespace, mIds); 91 } 92 resetIfBuilt()93 private void resetIfBuilt() { 94 if (mBuilt) { 95 mIds = new ArraySet<>(mIds); 96 mBuilt = false; 97 } 98 } 99 } 100 } 101