| /* |
| * Copyright (C) 2022 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.healthconnect.datatypes; |
| |
| import android.annotation.NonNull; |
| import android.healthconnect.datatypes.units.Volume; |
| |
| import java.time.Instant; |
| import java.time.ZoneOffset; |
| import java.util.Objects; |
| |
| /** Captures the amount of liquids user had in a single drink. */ |
| @Identifier(recordIdentifier = RecordTypeIdentifier.RECORD_TYPE_HYDRATION) |
| public final class HydrationRecord extends IntervalRecord { |
| /** Builder class for {@link HydrationRecord} */ |
| public static final class Builder { |
| private final Metadata mMetadata; |
| private final Instant mStartTime; |
| private final Instant mEndTime; |
| private ZoneOffset mStartZoneOffset; |
| private ZoneOffset mEndZoneOffset; |
| private final Volume mVolume; |
| |
| /** |
| * @param metadata Metadata to be associated with the record. See {@link Metadata}. |
| * @param startTime Start time of this activity |
| * @param endTime End time of this activity |
| * @param volume Volume of the liquids in {@link Volume} unit. Required field. Valid range: |
| * 0-100 liters. |
| */ |
| public Builder( |
| @NonNull Metadata metadata, |
| @NonNull Instant startTime, |
| @NonNull Instant endTime, |
| @NonNull Volume volume) { |
| Objects.requireNonNull(metadata); |
| Objects.requireNonNull(startTime); |
| Objects.requireNonNull(endTime); |
| Objects.requireNonNull(volume); |
| mMetadata = metadata; |
| mStartTime = startTime; |
| mEndTime = endTime; |
| mStartZoneOffset = ZoneOffset.systemDefault().getRules().getOffset(Instant.now()); |
| mEndZoneOffset = ZoneOffset.systemDefault().getRules().getOffset(Instant.now()); |
| mVolume = volume; |
| } |
| |
| /** Sets the zone offset of the user when the activity started */ |
| @NonNull |
| public Builder setStartZoneOffset(@NonNull ZoneOffset startZoneOffset) { |
| Objects.requireNonNull(startZoneOffset); |
| |
| mStartZoneOffset = startZoneOffset; |
| return this; |
| } |
| |
| /** Sets the zone offset of the user when the activity ended */ |
| @NonNull |
| public Builder setEndZoneOffset(@NonNull ZoneOffset endZoneOffset) { |
| Objects.requireNonNull(endZoneOffset); |
| |
| mEndZoneOffset = endZoneOffset; |
| return this; |
| } |
| |
| /** |
| * @return Object of {@link HydrationRecord} |
| */ |
| @NonNull |
| public HydrationRecord build() { |
| return new HydrationRecord( |
| mMetadata, mStartTime, mStartZoneOffset, mEndTime, mEndZoneOffset, mVolume); |
| } |
| } |
| |
| private final Volume mVolume; |
| |
| /** |
| * @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 volume Volume of this activity |
| */ |
| private HydrationRecord( |
| @NonNull Metadata metadata, |
| @NonNull Instant startTime, |
| @NonNull ZoneOffset startZoneOffset, |
| @NonNull Instant endTime, |
| @NonNull ZoneOffset endZoneOffset, |
| @NonNull Volume volume) { |
| super(metadata, startTime, startZoneOffset, endTime, endZoneOffset); |
| Objects.requireNonNull(metadata); |
| Objects.requireNonNull(startTime); |
| Objects.requireNonNull(startZoneOffset); |
| Objects.requireNonNull(startTime); |
| Objects.requireNonNull(endZoneOffset); |
| Objects.requireNonNull(volume); |
| mVolume = volume; |
| } |
| |
| /** |
| * @return hydration volume |
| */ |
| @NonNull |
| public Volume getVolume() { |
| return mVolume; |
| } |
| |
| /** |
| * 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 object argument; {@code false} |
| * otherwise. |
| */ |
| @Override |
| public boolean equals(@NonNull Object object) { |
| if (super.equals(object) && object instanceof HydrationRecord) { |
| HydrationRecord other = (HydrationRecord) object; |
| return this.getVolume().equals(other.getVolume()); |
| } |
| return false; |
| } |
| |
| /** Returns a hash code value for the object. */ |
| @Override |
| public int hashCode() { |
| return Objects.hash(super.hashCode(), this.getVolume()); |
| } |
| } |