-
Notifications
You must be signed in to change notification settings - Fork 108
/
ModelMetadata.java
147 lines (131 loc) · 4.53 KB
/
ModelMetadata.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amplifyframework.datastore.appsync;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.ObjectsCompat;
import com.amplifyframework.core.model.Model;
import com.amplifyframework.core.model.annotations.ModelConfig;
import com.amplifyframework.core.model.annotations.ModelField;
import com.amplifyframework.core.model.temporal.Temporal;
import java.util.Objects;
/**
* A system model to house versioning/sync metadata associated with a model instance.
* This is _also_ used as a model of a portion of an AppSync response, that includes
* metadata about a particular model. The metadata fields are unique to the AppSync protocol,
* and these fields exist in GraphQL responses from AppSync.
*/
@ModelConfig
@SuppressWarnings({"MemberName", "ParameterName"})
public final class ModelMetadata implements Model {
private final @ModelField(targetType = "ID", isRequired = true) String id;
private final @ModelField(targetType = "Boolean") Boolean _deleted;
private final @ModelField(targetType = "Int") Integer _version;
private final @ModelField(targetType = "AWSTimestamp") Temporal.Timestamp _lastChangedAt;
/**
* Constructor for this metadata model.
* @param id The identifier of the object this is holding the metadata for (also this object's own identifier)
* @param deleted Whether this object was deleted since the last sync time specified
* @param version What version this object was last seen at
* @param lastChangedAt When was this object last changed
*/
public ModelMetadata(
@NonNull String id,
@Nullable Boolean deleted,
@Nullable Integer version,
@Nullable Temporal.Timestamp lastChangedAt) {
this.id = Objects.requireNonNull(id);
this._deleted = deleted;
this._version = version;
this._lastChangedAt = lastChangedAt;
}
/**
* Gets identifier.
* @return identifier
*/
@NonNull
public String resolveIdentifier() {
return id;
}
/**
* Gets the deleted status.
* @return True if deleted, False otherwise
*/
@Nullable
public Boolean isDeleted() {
return _deleted;
}
/**
* Gets the version.
* @return version
*/
@Nullable
public Integer getVersion() {
return _version;
}
/**
* Gets the type of the model.
* @return Type of the Model.
*/
@Nullable
public String getTypename() {
return id.split("\\|")[0];
}
/**
* Gets last changed at time.
* @return last changed at time
*/
@Nullable
public Temporal.Timestamp getLastChangedAt() {
return _lastChangedAt;
}
@Override
public boolean equals(Object thatObject) {
if (this == thatObject) {
return true;
}
if (thatObject == null || getClass() != thatObject.getClass()) {
return false;
}
ModelMetadata metadata = (ModelMetadata) thatObject;
if (!ObjectsCompat.equals(id, metadata.id)) {
return false;
}
if (!ObjectsCompat.equals(_deleted, metadata._deleted)) {
return false;
}
if (!ObjectsCompat.equals(_version, metadata._version)) {
return false;
}
return ObjectsCompat.equals(_lastChangedAt, metadata._lastChangedAt);
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + (_deleted != null ? _deleted.hashCode() : 0);
result = 31 * result + (_version != null ? _version.hashCode() : 0);
result = 31 * result + (_lastChangedAt != null ? _lastChangedAt.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "ModelMetadata{" +
"id='" + id + '\'' +
", _deleted=" + _deleted +
", _version=" + _version +
", _lastChangedAt=" + _lastChangedAt +
'}';
}
}