1 /*
2  * Copyright (C) 2023 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.health.connect;
18 
19 import android.annotation.NonNull;
20 import android.health.connect.datatypes.DataOrigin;
21 import android.os.Parcel;
22 import android.util.ArraySet;
23 
24 import java.time.ZoneOffset;
25 import java.util.List;
26 import java.util.Objects;
27 import java.util.Set;
28 
29 /**
30  * A class to represent the results of {@link HealthConnectManager} aggregate APIs
31  *
32  * @hide
33  */
34 public final class AggregateResult<T> {
35     private final T mResult;
36     private ZoneOffset mZoneOffset;
37     private Set<DataOrigin> mDataOrigins;
38 
39     @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression
AggregateResult(T result)40     public AggregateResult(T result) {
41         mResult = result;
42     }
43 
putToParcel(@onNull Parcel parcel)44     public void putToParcel(@NonNull Parcel parcel) {
45         if (mResult instanceof Long) {
46             parcel.writeLong((Long) mResult);
47         } else if (mResult instanceof Double) {
48             parcel.writeDouble((Double) mResult);
49         }
50     }
51 
52     /**
53      * @return {@link ZoneOffset} for the underlying record, null if aggregation was derived from
54      *     multiple records
55      */
getZoneOffset()56     public ZoneOffset getZoneOffset() {
57         return mZoneOffset;
58     }
59 
60     /** Sets the {@link ZoneOffset} for the underlying record. */
setZoneOffset(ZoneOffset zoneOffset)61     public AggregateResult<T> setZoneOffset(ZoneOffset zoneOffset) {
62         mZoneOffset = zoneOffset;
63         return this;
64     }
65 
66     /** Returns set of {@link DataOrigin} that contributed to the aggregation result */
67     @NonNull
getDataOrigins()68     public Set<DataOrigin> getDataOrigins() {
69         return mDataOrigins;
70     }
71 
72     /** Sets a Set of {@link DataOrigin} that contributed to the aggregation result. */
setDataOrigins(@onNull List<String> packageNameList)73     public AggregateResult<T> setDataOrigins(@NonNull List<String> packageNameList) {
74         Objects.requireNonNull(packageNameList);
75 
76         mDataOrigins = new ArraySet<>();
77         for (String packageName : packageNameList) {
78             mDataOrigins.add(new DataOrigin.Builder().setPackageName(packageName).build());
79         }
80         return this;
81     }
82 
83     /**
84      * @return an Object representing the result of an aggregation.
85      */
86     @NonNull
getResult()87     T getResult() {
88         return mResult;
89     }
90 }
91