David Dong | 230063b | 2022-01-13 17:48:33 -0800 | [diff] [blame] | 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 androidx.appsearch.builtintypes; |
| 18 | |
| 19 | import androidx.annotation.NonNull; |
| 20 | import androidx.core.util.Preconditions; |
| 21 | |
| 22 | /** |
| 23 | * Default Builder for an AppSearch built in type. This builder includes all the default |
| 24 | * AppSearch properties. |
| 25 | */ |
| 26 | abstract class BaseBuiltinTypeBuilder<T extends BaseBuiltinTypeBuilder<T>> { |
| 27 | protected final String mNamespace; |
| 28 | protected final String mId; |
| 29 | protected int mDocumentScore; |
| 30 | protected long mCreationTimestampMillis; |
| 31 | protected long mDocumentTtlMillis; |
| 32 | |
| 33 | protected BaseBuiltinTypeBuilder(@NonNull String namespace, @NonNull String id) { |
| 34 | mNamespace = Preconditions.checkNotNull(namespace); |
| 35 | mId = Preconditions.checkNotNull(id); |
| 36 | |
| 37 | // Default for unset creationTimestampMillis. AppSearch will internally convert this |
| 38 | // to current time when creating the GenericDocument. |
| 39 | mCreationTimestampMillis = -1; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Sets the user-provided opaque document score of the current AppSearch document, which can |
| 44 | * be used for ranking using |
| 45 | * {@link androidx.appsearch.app.SearchSpec.RankingStrategy#RANKING_STRATEGY_DOCUMENT_SCORE}. |
| 46 | * |
| 47 | * <p>See {@link androidx.appsearch.annotation.Document.Score} for more information on score. |
| 48 | */ |
| 49 | @NonNull |
| 50 | @SuppressWarnings("unchecked") |
| 51 | public T setDocumentScore(int documentScore) { |
| 52 | mDocumentScore = documentScore; |
| 53 | return (T) this; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Sets the creation timestamp for the current AppSearch entity, in milliseconds using the |
| 58 | * {@link System#currentTimeMillis()} time base. |
| 59 | * |
| 60 | * <p>This timestamp refers to the creation time of the AppSearch entity, not when the |
| 61 | * document is written into AppSearch. |
| 62 | * |
| 63 | * <p>If not set, then the current timestamp will be used. |
| 64 | * |
| 65 | * <p>See {@link androidx.appsearch.annotation.Document.CreationTimestampMillis} for more |
| 66 | * information on creation timestamp. |
| 67 | */ |
| 68 | @NonNull |
| 69 | @SuppressWarnings("unchecked") |
| 70 | public T setCreationTimestampMillis(long creationTimestampMillis) { |
| 71 | mCreationTimestampMillis = creationTimestampMillis; |
| 72 | return (T) this; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Sets the time-to-live (TTL) for the current AppSearch document as a duration in milliseconds. |
| 77 | * |
| 78 | * <p>The document will be automatically deleted when the TTL expires. |
| 79 | * |
| 80 | * <p>If not set, then the document will never expire. |
| 81 | * |
| 82 | * <p>See {@link androidx.appsearch.annotation.Document.TtlMillis} for more information on |
| 83 | * TTL. |
| 84 | */ |
| 85 | @NonNull |
| 86 | @SuppressWarnings("unchecked") |
| 87 | public T setDocumentTtlMillis(long documentTtlMillis) { |
| 88 | mDocumentTtlMillis = documentTtlMillis; |
| 89 | return (T) this; |
| 90 | } |
| 91 | } |