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