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.datatypes.units;
18 
19 import android.annotation.NonNull;
20 
21 import java.util.Objects;
22 
23 /** Represents a unit of blood glucose level (glycaemia). Supported units: mmol/L */
24 public final class BloodGlucose implements Comparable<BloodGlucose> {
25     private final double mInMillimolesPerLiter;
26 
BloodGlucose(double value)27     private BloodGlucose(double value) {
28         mInMillimolesPerLiter = value;
29     }
30 
31     /**
32      * Creates a BloodGlucose object with the specified value in mmol/L
33      *
34      * @param value value to be set as millimoles per liter
35      */
36     @NonNull
fromMillimolesPerLiter(double value)37     public static BloodGlucose fromMillimolesPerLiter(double value) {
38         return new BloodGlucose(value);
39     }
40 
41     /** Returns blood glucose in millimoles per liter */
getInMillimolesPerLiter()42     public double getInMillimolesPerLiter() {
43         return mInMillimolesPerLiter;
44     }
45 
46     /**
47      * Compares this object with the specified object for order. Returns a negative integer, zero,
48      * or a positive integer as this object is less than, equal to, or greater than the specified
49      * object.
50      *
51      * @param other the object to be compared.
52      * @return a negative integer, zero, or a positive integer as this object is less than, equal
53      *     to, or greater than the specified object.
54      * @throws NullPointerException if the specified object is null
55      * @throws ClassCastException if the specified object's type prevents it from being compared to
56      *     this object.
57      */
58     @Override
compareTo(@onNull BloodGlucose other)59     public int compareTo(@NonNull BloodGlucose other) {
60         return Double.compare(this.mInMillimolesPerLiter, other.mInMillimolesPerLiter);
61     }
62 
63     /**
64      * Indicates whether some other object is "equal to" this one.
65      *
66      * @param object the reference object with which to compare.
67      * @return {@code true} if this object is the same as the object argument; {@code false}
68      *     otherwise.
69      */
70     @Override
equals(Object object)71     public boolean equals(Object object) {
72         if (this == object) return true;
73         if (object instanceof BloodGlucose) {
74             BloodGlucose other = (BloodGlucose) object;
75             return this.getInMillimolesPerLiter() == other.getInMillimolesPerLiter();
76         }
77         return false;
78     }
79 
80     /** Returns a hash code value for the object. */
81     @Override
hashCode()82     public int hashCode() {
83         return Objects.hash(this.getInMillimolesPerLiter());
84     }
85 
86     /**
87      * @return a string representation of the object.
88      */
89     @Override
toString()90     public String toString() {
91         return mInMillimolesPerLiter + " mmol/L";
92     }
93 }
94