• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.server.healthconnect.storage.datatypehelpers.aggregation;
18 
19 import android.annotation.IntDef;
20 
21 import java.util.List;
22 
23 /**
24  * Class which represents timestamp of the data to aggregate.
25  *
26  * @hide
27  */
28 public class AggregationTimestamp implements Comparable<AggregationTimestamp> {
29 
30     @IntDef({GROUP_BORDER, INTERVAL_START, INTERVAL_END})
31     public @interface TimestampType {}
32 
33     public static final int GROUP_BORDER = 0;
34     public static final int INTERVAL_START = 1;
35     public static final int INTERVAL_END = 2;
36 
37     private static final List<String> TYPE_PRINT_NAMES = List.of("GROUP_BORDER", "START", "END");
38 
39     @TimestampType private final int mType;
40     private final long mTime;
41     private AggregationRecordData mParentRecord;
42 
AggregationTimestamp(int type, long time)43     public AggregationTimestamp(int type, long time) {
44         mTime = time;
45         mType = type;
46     }
47 
getType()48     int getType() {
49         return mType;
50     }
51 
getTime()52     long getTime() {
53         return mTime;
54     }
55 
getParentData()56     AggregationRecordData getParentData() {
57         return mParentRecord;
58     }
59 
setParentData(AggregationRecordData parentRecord)60     AggregationTimestamp setParentData(AggregationRecordData parentRecord) {
61         mParentRecord = parentRecord;
62         return this;
63     }
64 
65     @Override
compareTo(AggregationTimestamp o)66     public int compareTo(AggregationTimestamp o) {
67         if (this.equals(o)) {
68             return 0;
69         }
70 
71         if (getTime() == o.getTime()) {
72             if (getType() != o.getType()) {
73                 return getType() - o.getType();
74             }
75 
76             return getParentData().compareTo(o.getParentData());
77         } else if (getTime() < o.getTime()) {
78             return -1;
79         } else {
80             return 1;
81         }
82     }
83 
84     @Override
toString()85     public String toString() {
86         return "Timestamp{type="
87                 + TYPE_PRINT_NAMES.get(mType)
88                 + ", time="
89                 + mTime
90                 + ", parentData="
91                 + (mParentRecord == null ? "null" : mParentRecord)
92                 + "}";
93     }
94 }
95