1 /* 2 * Copyright 2022 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.annotation.Nullable; 21 import android.app.appsearch.annotation.CanIgnoreReturnValue; 22 import android.app.appsearch.util.BundleUtil; 23 import android.os.Bundle; 24 25 import com.android.internal.util.Preconditions; 26 27 import java.util.Objects; 28 29 /** The result class of the {@link AppSearchSession#searchSuggestion}. */ 30 public final class SearchSuggestionResult { 31 32 private static final String SUGGESTED_RESULT_FIELD = "suggestedResult"; 33 private final Bundle mBundle; 34 @Nullable private Integer mHashCode; 35 SearchSuggestionResult(@onNull Bundle bundle)36 SearchSuggestionResult(@NonNull Bundle bundle) { 37 mBundle = Objects.requireNonNull(bundle); 38 } 39 40 /** 41 * Returns the {@link Bundle} populated by this builder. 42 * 43 * @hide 44 */ 45 @NonNull getBundle()46 public Bundle getBundle() { 47 return mBundle; 48 } 49 50 /** 51 * Returns the suggested result that could be used as query expression in the {@link 52 * AppSearchSession#search}. 53 * 54 * <p>The suggested result will never be empty. 55 * 56 * <p>The suggested result only contains lowercase or special characters. 57 */ 58 @NonNull getSuggestedResult()59 public String getSuggestedResult() { 60 return Objects.requireNonNull(mBundle.getString(SUGGESTED_RESULT_FIELD)); 61 } 62 63 @Override equals(@ullable Object other)64 public boolean equals(@Nullable Object other) { 65 if (this == other) { 66 return true; 67 } 68 if (!(other instanceof SearchSuggestionResult)) { 69 return false; 70 } 71 SearchSuggestionResult otherResult = (SearchSuggestionResult) other; 72 return BundleUtil.deepEquals(this.mBundle, otherResult.mBundle); 73 } 74 75 @Override hashCode()76 public int hashCode() { 77 if (mHashCode == null) { 78 mHashCode = BundleUtil.deepHashCode(mBundle); 79 } 80 return mHashCode; 81 } 82 83 /** The Builder class of {@link SearchSuggestionResult}. */ 84 public static final class Builder { 85 private String mSuggestedResult = ""; 86 87 /** 88 * Sets the suggested result that could be used as query expression in the {@link 89 * AppSearchSession#search}. 90 * 91 * <p>The suggested result should only contain lowercase or special characters. 92 */ 93 @CanIgnoreReturnValue 94 @NonNull setSuggestedResult(@onNull String suggestedResult)95 public Builder setSuggestedResult(@NonNull String suggestedResult) { 96 Objects.requireNonNull(suggestedResult); 97 Preconditions.checkStringNotEmpty(suggestedResult); 98 mSuggestedResult = suggestedResult; 99 return this; 100 } 101 102 /** Build a {@link SearchSuggestionResult} object */ 103 @NonNull build()104 public SearchSuggestionResult build() { 105 Bundle bundle = new Bundle(); 106 bundle.putString(SUGGESTED_RESULT_FIELD, mSuggestedResult); 107 return new SearchSuggestionResult(bundle); 108 } 109 } 110 } 111