summaryrefslogtreecommitdiff
path: root/framework/java/android/health/connect/AggregateRecordsResponse.java
blob: 83bbc18416290f86ccf8e7e45c71db3489a4a5f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.health.connect;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.health.connect.datatypes.AggregationType;
import android.health.connect.datatypes.DataOrigin;
import android.health.connect.internal.datatypes.utils.AggregationTypeIdMapper;
import android.util.ArrayMap;

import java.time.ZoneOffset;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/** A class representing response for {@link HealthConnectManager#aggregate} */
public final class AggregateRecordsResponse<T> {
    private final Map<AggregationType<T>, AggregateResult<T>> mAggregateResults;

    /**
     * We only support querying and fetching same type of aggregations, so we can cast blindly
     *
     * @hide
     */
    @SuppressWarnings("unchecked")
    public AggregateRecordsResponse(@NonNull Map<Integer, AggregateResult<?>> aggregateResults) {
        Objects.requireNonNull(aggregateResults);

        mAggregateResults = new ArrayMap<>(aggregateResults.size());
        aggregateResults.forEach(
                (key, value) ->
                        mAggregateResults.put(
                                (AggregationType<T>)
                                        AggregationTypeIdMapper.getInstance()
                                                .getAggregationTypeFor(key),
                                (AggregateResult<T>) value));
    }

    /** @hide */
    public static <U> ZoneOffset getZoneOffsetInternal(
            @NonNull AggregationType<U> aggregationType,
            Map<AggregationType<U>, AggregateResult<U>> mAggregateResults) {
        Objects.requireNonNull(aggregationType);

        AggregateResult<U> result = mAggregateResults.get(aggregationType);

        if (result == null) {
            return null;
        }

        return result.getZoneOffset();
    }

    /** @hide */
    public static <U> Set<DataOrigin> getDataOriginsInternal(
            @NonNull AggregationType<U> aggregationType,
            Map<AggregationType<U>, AggregateResult<U>> mAggregateResults) {
        Objects.requireNonNull(aggregationType);

        AggregateResult<U> result = mAggregateResults.get(aggregationType);

        if (result == null) {
            return Collections.emptySet();
        }

        return result.getDataOrigins();
    }

    /** @hide */
    public static <U> U getInternal(
            @NonNull AggregationType<U> aggregationType,
            Map<AggregationType<U>, AggregateResult<U>> mAggregateResults) {
        Objects.requireNonNull(aggregationType);

        AggregateResult<U> result = mAggregateResults.get(aggregationType);

        if (result == null) {
            return null;
        }

        return result.getResult();
    }

    /**
     * @return a map of {@link AggregationType} -> {@link AggregateResult}
     * @hide
     */
    @NonNull
    public Map<Integer, AggregateResult<?>> getAggregateResults() {
        Map<Integer, AggregateResult<?>> aggregateResultMap = new ArrayMap<>();
        mAggregateResults.forEach(
                (key, value) -> {
                    aggregateResultMap.put(
                            AggregationTypeIdMapper.getInstance().getIdFor(key), value);
                });
        return aggregateResultMap;
    }

    /**
     * @return an aggregation result for {@code aggregationType}. *
     * @param aggregationType {@link AggregationType} for which to get the result
     */
    @Nullable
    public T get(@NonNull AggregationType<T> aggregationType) {
        return getInternal(aggregationType, mAggregateResults);
    }

    /**
     * @return {@link ZoneOffset} for the underlying aggregation record, null if the corresponding
     *     aggregation doesn't exist and or if multiple records were present.
     */
    @Nullable
    public ZoneOffset getZoneOffset(@NonNull AggregationType<T> aggregationType) {
        return getZoneOffsetInternal(aggregationType, mAggregateResults);
    }

    /**
     * Returns a set of {@link DataOrigin}s for the underlying aggregation record, empty set if the
     * corresponding aggregation doesn't exist and or if multiple records were present.
     */
    @NonNull
    public Set<DataOrigin> getDataOrigins(@NonNull AggregationType<T> aggregationType) {
        return getDataOriginsInternal(aggregationType, mAggregateResults);
    }
}