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.migration;
18 
19 import static java.util.Objects.requireNonNull;
20 
21 import android.annotation.NonNull;
22 import android.annotation.SystemApi;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 /**
27  * A class to represent a migration entity.
28  *
29  * @hide
30  */
31 @SystemApi
32 public final class MigrationEntity implements Parcelable {
33 
34     @NonNull
35     public static final Creator<MigrationEntity> CREATOR =
36             new Creator<>() {
37                 @Override
38                 public MigrationEntity createFromParcel(Parcel in) {
39                     return new MigrationEntity(in);
40                 }
41 
42                 @Override
43                 public MigrationEntity[] newArray(int size) {
44                     return new MigrationEntity[size];
45                 }
46             };
47 
48     private final String mEntityId;
49     private final MigrationPayload mPayload;
50 
51     /**
52      * @param entityId a stable identifier of the entity, must be unique within the entire
53      *     migration. Duplicated entities are ignored.
54      * @param payload a payload of the entity to migrate.
55      */
MigrationEntity(@onNull String entityId, @NonNull MigrationPayload payload)56     public MigrationEntity(@NonNull String entityId, @NonNull MigrationPayload payload) {
57         requireNonNull(entityId);
58         requireNonNull(payload);
59 
60         mEntityId = entityId;
61         mPayload = payload;
62     }
63 
MigrationEntity(@onNull Parcel in)64     private MigrationEntity(@NonNull Parcel in) {
65         mEntityId = in.readString();
66 
67         mPayload =
68                 in.readParcelable(MigrationPayload.class.getClassLoader(), MigrationPayload.class);
69     }
70 
71     /**
72      * Returns a stable identifier of the entity, unique within the entire migration. Duplicated
73      * entities are ignored.
74      */
75     @NonNull
getEntityId()76     public String getEntityId() {
77         return mEntityId;
78     }
79 
80     /** Returns a payload of the entity to migrate. */
81     @NonNull
getPayload()82     public MigrationPayload getPayload() {
83         return mPayload;
84     }
85 
86     @Override
describeContents()87     public int describeContents() {
88         return 0;
89     }
90 
91     @Override
writeToParcel(@onNull Parcel dest, int flags)92     public void writeToParcel(@NonNull Parcel dest, int flags) {
93         dest.writeString(mEntityId);
94         dest.writeParcelable(mPayload, 0);
95     }
96 }
97