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