-
Notifications
You must be signed in to change notification settings - Fork 214
/
StreamAck.java
215 lines (191 loc) · 5.98 KB
/
StreamAck.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (c) 2017 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.internal.utils.pekko.streaming;
import java.util.Objects;
import java.util.stream.Stream;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.base.model.json.Jsonifiable;
/**
* Ack message that is used for acking elements while streaming.
*/
@Immutable
public final class StreamAck implements Jsonifiable<JsonValue> {
/**
* The element id.
*/
private final String elementId;
/**
* The status of the ack.
*/
private final Status status;
/**
* Create an ack.
*
* @param elementId the element for which the ack is sent.
* @param status The status of the StreamAck.
*/
private StreamAck(final String elementId, final Status status) {
this.elementId = elementId;
this.status = status;
}
/**
* Get the element id.
*
* @return The element id.
*/
String getElementId() {
return elementId;
}
/**
* Get the status.
*
* @return The status.
*/
Status getStatus() {
return status;
}
/**
* Create an successful ack.
*
* @param elementId the element for which the ack is sent.
* @return The StreamAck with status {@link Status#SUCCESS}.
*/
public static StreamAck success(final String elementId) {
return new StreamAck(elementId, Status.SUCCESS);
}
/**
* Create an failure ack.
*
* @param elementId the element for which the ack is sent.}.
* @return The StreamAck with status {@link Status#FAILURE}.
*/
public static StreamAck failure(final String elementId) {
return new StreamAck(elementId, Status.FAILURE);
}
/**
* Create an ack.
*
* @param elementId the element for which the ack is sent.
* @param status The status of the StreamAck.
* @return The StreamAck.
*/
private static StreamAck of(final String elementId, final Status status) {
return new StreamAck(elementId, status);
}
/**
* Status of ACKs.
*/
public enum Status {
/**
* Success status.
*/
SUCCESS,
/**
* Failure status.
*/
FAILURE,
/**
* Status that can be used if status of ack is unknown.
*/
UNKNOWN;
/**
* Create the status from its String representation.
*
* @param statusString The Status as String.
* @return The Status instance or {@link #UNKNOWN} if unknown.
*/
static Status fromString(final String statusString) {
return Stream.of(values())
.filter(status -> status.name().equals(statusString))
.findFirst()
.orElse(Status.UNKNOWN);
}
}
/**
* {@inheritDoc}
*/
@Override
public JsonValue toJson() {
return JsonFactory.newObjectBuilder()
.set(JsonFields.ELEMENT_ID, elementId)
.set(JsonFields.STATUS, status.name())
.build();
}
/**
* Creates a new {@code StreamAck} from a JSON object.
*
* @param jsonObject the JSON object of which a new StreamAck is to be created.
* @return the StreamAck which was created from the given JSON object.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws IllegalArgumentException if {@code jsonObject} is empty.
* @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} is not valid JSON.
* @throws org.eclipse.ditto.json.JsonMissingFieldException if the passed in {@code jsonObject} was not in the
* expected 'StreamAck' format.
*/
public static StreamAck fromJson(final JsonObject jsonObject) {
final String extractedElementId = jsonObject.getValueOrThrow(StreamAck.JsonFields.ELEMENT_ID);
final String extractedStatus = jsonObject.getValueOrThrow(JsonFields.STATUS);
final Status status = Status.fromString(extractedStatus);
return of(extractedElementId, status);
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final StreamAck streamAck = (StreamAck) o;
return Objects.equals(elementId, streamAck.elementId) &&
status == streamAck.status;
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return Objects.hash(elementId, status);
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "StreamAck{" +
"elementId='" + elementId + '\'' +
", status=" + status +
'}';
}
/**
* An enumeration of the known {@link org.eclipse.ditto.json.JsonField}s of a StreamAck.
*/
@Immutable
private static final class JsonFields {
/**
* JSON field containing the StreamAcks's element Id.
*/
private static final JsonFieldDefinition<String> ELEMENT_ID = JsonFactory.newStringFieldDefinition
("elementId");
/**
* JSON field containing the StreamAck's type.
*/
private static final JsonFieldDefinition<String> STATUS = JsonFactory.newStringFieldDefinition("type");
private JsonFields() {
throw new AssertionError();
}
}
}