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;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 
22 import java.util.Objects;
23 
24 /** Specifies the contributing source/application of any {@link Record} */
25 public final class DataOrigin {
26     /**
27      * @see DataOrigin
28      */
29     public static final class Builder {
30         @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression
31         private String mPackageName;
32 
33         /**
34          * Sets the package name of the contributing package. Auto-populated by the platform at
35          * record insertion time.
36          */
37         @NonNull
setPackageName(@onNull String packageName)38         public Builder setPackageName(@NonNull String packageName) {
39             Objects.requireNonNull(packageName);
40 
41             mPackageName = packageName;
42             return this;
43         }
44 
45         /**
46          * @return {@link DataOrigin}'s object
47          */
48         @NonNull
build()49         public DataOrigin build() {
50             return new DataOrigin(mPackageName);
51         }
52     }
53 
54     private final String mPackageName;
55 
DataOrigin(String packageName)56     private DataOrigin(String packageName) {
57         mPackageName = packageName;
58     }
59 
60     /**
61      * @return the corresponding package name.
62      */
63     @NonNull
getPackageName()64     public String getPackageName() {
65         return mPackageName;
66     }
67 
68     /**
69      * Indicates whether some other object is "equal to" this one.
70      *
71      * @param object the reference object with which to compare.
72      * @return {@code true} if this object is the same as the obj
73      */
74     @Override
equals(@ullable Object object)75     public boolean equals(@Nullable Object object) {
76         if (this == object) return true;
77         if (object instanceof DataOrigin) {
78             DataOrigin other = (DataOrigin) object;
79             return Objects.equals(this.getPackageName(), other.getPackageName());
80         }
81         return false;
82     }
83 
84     /**
85      * Returns a hash code value for the object.
86      *
87      * @return a hash code value for this object.
88      */
89     @Override
hashCode()90     public int hashCode() {
91         return Objects.hash(this.getPackageName());
92     }
93 }
94