1 /*
2  * Copyright 2025 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.stats;
18 
19 import android.annotation.SuppressLint;
20 
21 import androidx.annotation.RestrictTo;
22 import androidx.appsearch.annotation.CanIgnoreReturnValue;
23 import androidx.core.util.Preconditions;
24 
25 import org.jspecify.annotations.NonNull;
26 
27 /**
28  * Encapsulates base statistics information for AppSearch results.
29  *
30  * This class provides a convenient way to store and retrieve key statistics,
31  * such as the result code and a bitmask of enabled features.
32  * @exportToFramework:hide
33  */
34 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
35 public class BaseStats {
36     private static final int LAUNCH_VM = 0;
37 
38     private final long mEnabledFeatures;
39 
BaseStats(@onNull Builder<?> builder)40     protected BaseStats(@NonNull Builder<?> builder) {
41         Preconditions.checkNotNull(builder);
42         mEnabledFeatures = builder.mEnabledFeatures;
43     }
44 
45     /** Returns the bitmask representing the enabled features. */
getEnabledFeatures()46     public long getEnabledFeatures() {
47         return mEnabledFeatures;
48     }
49 
50     /**
51      * Builder for {@link BaseStats}.
52      *
53      * @param <BuilderType> Type of subclass who extends this.
54      */
55     // This builder is specifically designed to be extended by stats classes.
56     @SuppressLint("StaticFinalBuilder")
57     @SuppressWarnings("rawtypes")
58     public static class Builder<BuilderType extends Builder> {
59         private final BuilderType mBuilderTypeInstance;
60         long mEnabledFeatures;
61 
62         /** Creates a new {@link BaseStats.Builder}. */
63         @SuppressWarnings("unchecked")
Builder()64         public Builder() {
65             mBuilderTypeInstance = (BuilderType) this;
66         }
67 
68         /** Sets bitmask for all enabled features . */
69         @CanIgnoreReturnValue
setLaunchVMEnabled(boolean enabled)70         public @NonNull BuilderType setLaunchVMEnabled(boolean enabled) {
71             modifyEnabledFeature(LAUNCH_VM, enabled);
72             return mBuilderTypeInstance;
73         }
74 
75         /** Builds the {@link BaseStats} instance. */
build()76         public @NonNull BaseStats build() {
77             return new BaseStats(this);
78         }
79 
modifyEnabledFeature(int feature, boolean enabled)80         private void modifyEnabledFeature(int feature, boolean enabled) {
81             if (enabled) {
82                 mEnabledFeatures |= (1L << feature);
83             } else {
84                 mEnabledFeatures &= ~(1L << feature);
85             }
86         }
87     }
88 }
89