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.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 28 import com.android.appsearch.flags.Flags; 29 import com.android.internal.util.Preconditions; 30 31 import java.util.Objects; 32 33 /** The result class of the {@link AppSearchSession#searchSuggestion}. */ 34 @SafeParcelable.Class(creator = "SearchSuggestionResultCreator") 35 @SuppressWarnings("HiddenSuperclass") 36 public final class SearchSuggestionResult extends AbstractSafeParcelable { 37 38 @FlaggedApi(Flags.FLAG_ENABLE_SAFE_PARCELABLE_2) 39 @NonNull 40 public static final Parcelable.Creator<SearchSuggestionResult> CREATOR = 41 new SearchSuggestionResultCreator(); 42 43 @Field(id = 1, getter = "getSuggestedResult") 44 private final String mSuggestedResult; 45 46 @Nullable private Integer mHashCode; 47 48 @Constructor SearchSuggestionResult(@aramid = 1) String suggestedResult)49 SearchSuggestionResult(@Param(id = 1) String suggestedResult) { 50 mSuggestedResult = Objects.requireNonNull(suggestedResult); 51 } 52 53 /** 54 * Returns the suggested result that could be used as query expression in the {@link 55 * AppSearchSession#search}. 56 * 57 * <p>The suggested result will never be empty. 58 * 59 * <p>The suggested result only contains lowercase or special characters. 60 */ 61 @NonNull getSuggestedResult()62 public String getSuggestedResult() { 63 return mSuggestedResult; 64 } 65 66 @Override equals(@ullable Object other)67 public boolean equals(@Nullable Object other) { 68 if (this == other) { 69 return true; 70 } 71 if (!(other instanceof SearchSuggestionResult)) { 72 return false; 73 } 74 SearchSuggestionResult otherResult = (SearchSuggestionResult) other; 75 return mSuggestedResult.equals(otherResult.mSuggestedResult); 76 } 77 78 @Override hashCode()79 public int hashCode() { 80 if (mHashCode == null) { 81 mHashCode = mSuggestedResult.hashCode(); 82 } 83 return mHashCode; 84 } 85 86 /** The Builder class of {@link SearchSuggestionResult}. */ 87 public static final class Builder { 88 private String mSuggestedResult = ""; 89 90 /** 91 * Sets the suggested result that could be used as query expression in the {@link 92 * AppSearchSession#search}. 93 * 94 * <p>The suggested result should only contain lowercase or special characters. 95 */ 96 @CanIgnoreReturnValue 97 @NonNull setSuggestedResult(@onNull String suggestedResult)98 public Builder setSuggestedResult(@NonNull String suggestedResult) { 99 Objects.requireNonNull(suggestedResult); 100 Preconditions.checkStringNotEmpty(suggestedResult); 101 mSuggestedResult = suggestedResult; 102 return this; 103 } 104 105 /** Build a {@link SearchSuggestionResult} object */ 106 @NonNull build()107 public SearchSuggestionResult build() { 108 return new SearchSuggestionResult(mSuggestedResult); 109 } 110 } 111 112 @FlaggedApi(Flags.FLAG_ENABLE_SAFE_PARCELABLE_2) 113 @Override writeToParcel(@onNull Parcel dest, int flags)114 public void writeToParcel(@NonNull Parcel dest, int flags) { 115 SearchSuggestionResultCreator.writeToParcel(this, dest, flags); 116 } 117 } 118