1 /*
2  * Copyright (C) 2024 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.internal.datatypes;
18 
19 
20 import static java.util.Objects.hash;
21 import static java.util.Objects.requireNonNull;
22 
23 import android.annotation.NonNull;
24 import android.annotation.Nullable;
25 import android.health.connect.datatypes.MedicalResource;
26 import android.health.connect.datatypes.MedicalResource.MedicalResourceType;
27 
28 import java.util.Objects;
29 import java.util.UUID;
30 
31 /**
32  * Internal representation of {@link MedicalResource}.
33  *
34  * @hide
35  */
36 public final class MedicalResourceInternal {
37     @Nullable private UUID mUuid;
38     @MedicalResourceType private int mType;
39     @NonNull private String mDataSourceId = "";
40     @NonNull private String mData = "";
41     @NonNull private String mFhirResourceType = "";
42     @NonNull private String mFhirResourceId = "";
43 
44     /** Returns the unique identifier of this data. */
45     @Nullable
getUuid()46     public UUID getUuid() {
47         return mUuid;
48     }
49 
50     /** Returns this object with the identifier. */
51     @NonNull
setUuid(@ullable UUID uuid)52     public MedicalResourceInternal setUuid(@Nullable UUID uuid) {
53         requireNonNull(uuid);
54         mUuid = uuid;
55         return this;
56     }
57 
58     /** Returns the medical resource type. */
59     @MedicalResourceType
getType()60     public int getType() {
61         return mType;
62     }
63 
64     /** Returns this object with the type. */
65     @NonNull
setType(@edicalResourceType int type)66     public MedicalResourceInternal setType(@MedicalResourceType int type) {
67         mType = type;
68         return this;
69     }
70 
71     /** Returns The data source ID where the data comes from. */
72     @NonNull
getDataSourceId()73     public String getDataSourceId() {
74         return mDataSourceId;
75     }
76 
77     /** Returns this object with the data source ID. */
78     @NonNull
setDataSourceId(@onNull String dataSourceId)79     public MedicalResourceInternal setDataSourceId(@NonNull String dataSourceId) {
80         requireNonNull(dataSourceId);
81         mDataSourceId = dataSourceId;
82         return this;
83     }
84 
85     /** Returns the FHIR resource data in JSON representation. */
86     @NonNull
getData()87     public String getData() {
88         return mData;
89     }
90 
91     /** Returns this object with the FHIR resource data in JSON representation. */
92     @NonNull
setData(@onNull String data)93     public MedicalResourceInternal setData(@NonNull String data) {
94         requireNonNull(data);
95         mData = data;
96         return this;
97     }
98 
99     /** Returns the FHIR resource type extracted from the FHIR JSON. */
100     @NonNull
getFhirResourceType()101     public String getFhirResourceType() {
102         return mFhirResourceType;
103     }
104 
105     /** Returns this object with the FHIR resource type. */
106     @NonNull
setFhirResourceType(@onNull String fhirResourceType)107     public MedicalResourceInternal setFhirResourceType(@NonNull String fhirResourceType) {
108         requireNonNull(fhirResourceType);
109         mFhirResourceType = fhirResourceType;
110         return this;
111     }
112 
113     /** Returns the FHIR resource id extracted from the FHIR JSON. */
114     @NonNull
getFhirResourceId()115     public String getFhirResourceId() {
116         return mFhirResourceId;
117     }
118 
119     /** Returns this object with the FHIR resource id. */
120     @NonNull
setFhirResourceId(@onNull String fhirResourceId)121     public MedicalResourceInternal setFhirResourceId(@NonNull String fhirResourceId) {
122         requireNonNull(fhirResourceId);
123         mFhirResourceId = fhirResourceId;
124         return this;
125     }
126 
127     /** Converts this object to an external representation. */
128     @NonNull
129     @SuppressWarnings("FlaggedApi") // this class is internal only
toExternalResource()130     public MedicalResource toExternalResource() {
131         return new MedicalResource.Builder(
132                         requireNonNull(getUuid()).toString(),
133                         getType(),
134                         getDataSourceId(),
135                         getData())
136                 .build();
137     }
138 
139     /** Converts to this object from an external representation. */
140     @NonNull
141     @SuppressWarnings("FlaggedApi") // this class is internal only
fromExternalResource(@onNull MedicalResource external)142     public static MedicalResourceInternal fromExternalResource(@NonNull MedicalResource external) {
143         requireNonNull(external);
144         return new MedicalResourceInternal()
145                 .setUuid(UUID.fromString(external.getId()))
146                 .setType(external.getType())
147                 .setDataSourceId(external.getDataSourceId())
148                 .setData(external.getData());
149     }
150 
151     @Override
equals(Object o)152     public boolean equals(Object o) {
153         if (this == o) return true;
154         if (!(o instanceof MedicalResourceInternal that)) return false;
155         return Objects.equals(getUuid(), that.getUuid())
156                 && getType() == that.getType()
157                 && getDataSourceId().equals(that.getDataSourceId())
158                 && getData().equals(that.getData());
159     }
160 
161     @Override
hashCode()162     public int hashCode() {
163         return hash(getUuid(), getType(), getDataSourceId(), getData());
164     }
165 }
166