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;
18 
19 import static com.android.healthfitness.flags.Flags.FLAG_PERSONAL_HEALTH_RECORD;
20 
21 import static java.util.Objects.hash;
22 import static java.util.Objects.requireNonNull;
23 
24 import android.annotation.FlaggedApi;
25 import android.annotation.NonNull;
26 import android.health.connect.datatypes.MedicalDataSource;
27 import android.os.Parcel;
28 import android.os.Parcelable;
29 
30 /** Class used to create requests for {@link HealthConnectManager#upsertMedicalResources}. */
31 @FlaggedApi(FLAG_PERSONAL_HEALTH_RECORD)
32 public final class UpsertMedicalResourceRequest implements Parcelable {
33     private final long mDataSourceId;
34     @NonNull private final String mData;
35 
36     @NonNull
37     public static final Creator<UpsertMedicalResourceRequest> CREATOR =
38             new Creator<>() {
39                 @Override
40                 public UpsertMedicalResourceRequest createFromParcel(Parcel in) {
41                     return new UpsertMedicalResourceRequest(in);
42                 }
43 
44                 @Override
45                 public UpsertMedicalResourceRequest[] newArray(int size) {
46                     return new UpsertMedicalResourceRequest[size];
47                 }
48             };
49 
50     /**
51      * @param dataSourceId The id associated with the existing {@link MedicalDataSource}.
52      * @param data The FHIR resource data in JSON representation.
53      */
UpsertMedicalResourceRequest(long dataSourceId, @NonNull String data)54     private UpsertMedicalResourceRequest(long dataSourceId, @NonNull String data) {
55         requireNonNull(data);
56 
57         mDataSourceId = dataSourceId;
58         mData = data;
59     }
60 
UpsertMedicalResourceRequest(@onNull Parcel in)61     private UpsertMedicalResourceRequest(@NonNull Parcel in) {
62         requireNonNull(in);
63         mDataSourceId = in.readLong();
64         mData = requireNonNull(in.readString());
65     }
66 
67     /**
68      * Returns the id of the existing {@link MedicalDataSource}, to represent where the data is
69      * coming from.
70      */
71     @NonNull
getDataSourceId()72     public long getDataSourceId() {
73         return mDataSourceId;
74     }
75 
76     /** Returns the FHIR resource data in JSON representation. */
77     @NonNull
getData()78     public String getData() {
79         return mData;
80     }
81 
82     @Override
describeContents()83     public int describeContents() {
84         return 0;
85     }
86 
87     /** Populates a {@link Parcel} with the self information. */
88     @Override
writeToParcel(@onNull Parcel dest, int flags)89     public void writeToParcel(@NonNull Parcel dest, int flags) {
90         requireNonNull(dest);
91         dest.writeLong(mDataSourceId);
92         dest.writeString(mData);
93     }
94 
95     /** Returns a hash code value for the object. */
96     @Override
hashCode()97     public int hashCode() {
98         return hash(getDataSourceId(), getData());
99     }
100 
101     /** Returns whether an object is equal to the current one. */
102     @Override
equals(Object o)103     public boolean equals(Object o) {
104         if (this == o) return true;
105         if (!(o instanceof UpsertMedicalResourceRequest that)) return false;
106         return getDataSourceId() == that.getDataSourceId() && getData().equals(that.getData());
107     }
108 
109     /** Builder class for {@link UpsertMedicalResourceRequest}. */
110     public static final class Builder {
111         private long mDataSourceId;
112         private String mData;
113 
114         /**
115          * @param dataSourceId The id associated with the existing {@link MedicalDataSource}.
116          * @param data The FHIR resource data in JSON representation.
117          */
Builder(long dataSourceId, @NonNull String data)118         public Builder(long dataSourceId, @NonNull String data) {
119             requireNonNull(data);
120 
121             mDataSourceId = dataSourceId;
122             mData = data;
123         }
124 
Builder(@onNull Builder original)125         public Builder(@NonNull Builder original) {
126             requireNonNull(original);
127             mDataSourceId = original.mDataSourceId;
128             mData = original.mData;
129         }
130 
Builder(@onNull UpsertMedicalResourceRequest original)131         public Builder(@NonNull UpsertMedicalResourceRequest original) {
132             requireNonNull(original);
133             mDataSourceId = original.getDataSourceId();
134             mData = original.getData();
135         }
136 
137         /**
138          * @param dataSourceId The id associated with the existing {@link MedicalDataSource}.
139          */
140         @NonNull
setDataSourceId(long dataSourceId)141         public Builder setDataSourceId(long dataSourceId) {
142             mDataSourceId = dataSourceId;
143             return this;
144         }
145 
146         /**
147          * @param data represents the FHIR resource data in JSON format.
148          */
149         @NonNull
setData(@onNull String data)150         public Builder setData(@NonNull String data) {
151             requireNonNull(data);
152             mData = data;
153             return this;
154         }
155 
156         /** Returns the Object of {@link UpsertMedicalResourceRequest}. */
157         @NonNull
build()158         public UpsertMedicalResourceRequest build() {
159             return new UpsertMedicalResourceRequest(mDataSourceId, mData);
160         }
161     }
162 }
163