blob: 43b48a1d2483b3cada9fef8d505ed78253e886fe [file] [log] [blame]
David Dong230063b2022-01-13 17:48:33 -08001/*
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
17package androidx.appsearch.builtintypes;
18
19import androidx.annotation.NonNull;
20import 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 */
26abstract 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}