/
StreamedSnapshot.java
116 lines (100 loc) · 3.57 KB
/
StreamedSnapshot.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
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.models.streaming;
import java.util.Objects;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.base.entity.id.DefaultEntityId;
import org.eclipse.ditto.model.base.entity.id.EntityId;
import org.eclipse.ditto.model.base.json.Jsonifiable;
/**
* Serializable message for streamed snapshots.
*/
public final class StreamedSnapshot implements StreamingMessage, Jsonifiable<JsonObject> {
private final EntityId entityId;
private final JsonObject snapshot;
private StreamedSnapshot(final EntityId entityId, final JsonObject snapshot) {
this.entityId = entityId;
this.snapshot = snapshot;
}
/**
* Create a streamed snapshot from entity ID and snapshot as JSON object.
*
* @param entityId the entity ID of the snapshot.
* @param snapshot the snapshot as JSON object.
* @return the streamed snapshot object.
*/
public static StreamedSnapshot of(final EntityId entityId, final JsonObject snapshot) {
return new StreamedSnapshot(entityId, snapshot);
}
/**
* Deserialize a streamed snapshot from JSON.
*
* @param jsonObject the JSON representation of the streamed snapshot.
* @return the streamed snapshot object.
*/
public static StreamedSnapshot fromJson(final JsonObject jsonObject) {
final EntityId entityId = DefaultEntityId.of(jsonObject.getValueOrThrow(JsonFields.ENTITY_ID));
final JsonObject snapshot = jsonObject.getValueOrThrow(JsonFields.SNAPSHOT);
return new StreamedSnapshot(entityId, snapshot);
}
/**
* Retrieve the entity ID of the streamed snapshot.
*
* @return the entity ID.
*/
public EntityId getEntityId() {
return entityId;
}
/**
* Retrieve the snapshot as JSON object.
*
* @return the snapshot as JSON object.
*/
public JsonObject getSnapshot() {
return snapshot;
}
@Override
public JsonObject toJson() {
return JsonObject.newBuilder()
.set(JsonFields.ENTITY_ID, entityId.toString())
.set(JsonFields.SNAPSHOT, snapshot)
.build();
}
@Override
public boolean equals(final Object other) {
if (other instanceof StreamedSnapshot) {
final StreamedSnapshot that = (StreamedSnapshot) other;
return Objects.equals(entityId, that.entityId) &&
Objects.equals(snapshot, that.snapshot);
} else {
return false;
}
}
@Override
public int hashCode() {
return Objects.hash(entityId, snapshot);
}
@Override
public String toString() {
return getClass().getSimpleName() +
"[entityId=" + entityId +
",snapshot=" + snapshot +
"]";
}
private static final class JsonFields {
private static final JsonFieldDefinition<String> ENTITY_ID = JsonFactory.newStringFieldDefinition("id");
private static final JsonFieldDefinition<JsonObject> SNAPSHOT = JsonFactory.newJsonObjectFieldDefinition("s");
}
}