summaryrefslogtreecommitdiff
path: root/framework/java/android/health/connect/datatypes/IntervalRecord.java
blob: 5d55c5c3bf62170f74eb0dcdeb85923eb7be2c16 (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
/*
 * 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.datatypes;

import android.annotation.NonNull;
import android.annotation.Nullable;

import java.time.Instant;
import java.time.ZoneOffset;
import java.util.Objects;

/** A record that contains a measurement with a time interval. */
public abstract class IntervalRecord extends Record {
    private final Instant mStartTime;
    private final ZoneOffset mStartZoneOffset;
    private final Instant mEndTime;
    private final ZoneOffset mEndZoneOffset;

    /**
     * @param metadata Metadata to be associated with the record. See {@link Metadata}
     * @param startTime Start time of this activity
     * @param startZoneOffset Zone offset of the user when the activity started
     * @param endTime End time of this activity
     * @param endZoneOffset Zone offset of the user when the activity finished
     * @param skipValidation Boolean flag to skip validation of record values.
     */
    IntervalRecord(
            @NonNull Metadata metadata,
            @NonNull Instant startTime,
            @NonNull ZoneOffset startZoneOffset,
            @NonNull Instant endTime,
            @NonNull ZoneOffset endZoneOffset,
            boolean skipValidation) {
        super(metadata);
        Objects.requireNonNull(startTime);
        Objects.requireNonNull(startZoneOffset);
        Objects.requireNonNull(endTime);
        Objects.requireNonNull(endZoneOffset);
        if (!skipValidation && startTime.isAfter(Instant.now())) {
            throw new IllegalArgumentException(
                    "Record start time must not be in the future, start time: "
                            + startTime
                            + " currentTime: "
                            + Instant.now());
        }
        if (endTime.toEpochMilli() < startTime.toEpochMilli()) {
            throw new IllegalArgumentException(
                    "end time in millis needs to be after start time in millis. startTime instant: "
                            + startTime
                            + " endTime instant: "
                            + endTime);
        }
        mStartTime = startTime;
        mStartZoneOffset = startZoneOffset;
        mEndTime = endTime;
        mEndZoneOffset = endZoneOffset;
    }
    /**
     * @return Start time of the activity
     */
    @NonNull
    public Instant getStartTime() {
        return mStartTime;
    }
    /**
     * @return Start time's zone offset of the activity
     */
    @NonNull
    public ZoneOffset getStartZoneOffset() {
        return mStartZoneOffset;
    }
    /**
     * @return End time of the activity
     */
    @NonNull
    public Instant getEndTime() {
        return mEndTime;
    }
    /**
     * @return End time's zone offset of the activity
     */
    @NonNull
    public ZoneOffset getEndZoneOffset() {
        return mEndZoneOffset;
    }
    /**
     * Indicates whether some other object is "equal to" this one.
     *
     * @param object the reference object with which to compare.
     * @return {@code true} if this object is the same as the obj
     */
    @Override
    public boolean equals(@Nullable Object object) {
        if (super.equals(object)) {
            IntervalRecord other = (IntervalRecord) object;
            return getStartTime().toEpochMilli() == other.getStartTime().toEpochMilli()
                    && getEndTime().toEpochMilli() == other.getEndTime().toEpochMilli()
                    && getStartZoneOffset().equals(other.getStartZoneOffset())
                    && getEndZoneOffset().equals(other.getEndZoneOffset());
        }
        return false;
    }
    /**
     * Returns a hash code value for the object.
     *
     * @return a hash code value for this object.
     */
    @Override
    public int hashCode() {
        return Objects.hash(
                super.hashCode(),
                getStartTime(),
                getStartZoneOffset(),
                getEndTime(),
                getEndZoneOffset());
    }
}