Skip to content

Commit 9621342

Browse files
Merge pull request ably#595 from ably/feature/580-refactor-message-extras
Refactor MessageExtras
2 parents 049a445 + 72a166a commit 9621342

4 files changed

Lines changed: 13 additions & 44 deletions

File tree

lib/src/main/java/io/ably/lib/types/BaseMessage.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,16 @@ public static JsonObject toJsonObject(final BaseMessage message) {
204204
if(data != null) {
205205
if(data instanceof byte[]) {
206206
byte[] dataBytes = (byte[])data;
207-
json.addProperty("data", new String(Base64Coder.encode(dataBytes)));
207+
json.addProperty(DATA, new String(Base64Coder.encode(dataBytes)));
208208
encoding = (encoding == null) ? "base64" : encoding + "/base64";
209209
} else {
210-
json.addProperty("data", data.toString());
210+
json.addProperty(DATA, data.toString());
211211
}
212-
if(encoding != null) json.addProperty("encoding", encoding);
212+
if(encoding != null) json.addProperty(ENCODING, encoding);
213213
}
214-
if(message.id != null) json.addProperty("id", message.id);
215-
if(message.clientId != null) json.addProperty("clientId", message.clientId);
216-
if(message.connectionId != null) json.addProperty("connectionId", message.connectionId);
214+
if(message.id != null) json.addProperty(ID, message.id);
215+
if(message.clientId != null) json.addProperty(CLIENT_ID, message.clientId);
216+
if(message.connectionId != null) json.addProperty(CONNECTION_ID, message.connectionId);
217217
return json;
218218
}
219219

lib/src/main/java/io/ably/lib/types/DeltaExtras.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.ably.lib.types;
22

33
import com.google.gson.JsonObject;
4-
import org.msgpack.core.MessagePacker;
54
import org.msgpack.value.Value;
65
import org.msgpack.value.ValueFactory;
76

@@ -48,16 +47,6 @@ public String getFrom() {
4847
return from;
4948
}
5049

51-
/* package private */ void write(MessagePacker packer) throws IOException {
52-
packer.packMapHeader(2);
53-
54-
packer.packString(FORMAT);
55-
packer.packString(format);
56-
57-
packer.packString(FROM);
58-
packer.packString(from);
59-
}
60-
6150
/* package private */ static DeltaExtras read(final Map<Value, Value> map) throws IOException {
6251
final Value format = map.get(ValueFactory.newString(FORMAT));
6352
final Value from = map.get(ValueFactory.newString(FROM));

lib/src/main/java/io/ably/lib/types/Message.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ void writeMsgpack(MessagePacker packer) throws IOException {
116116
packer.packMapHeader(fieldCount);
117117
super.writeFields(packer);
118118
if(name != null) {
119-
packer.packString("name");
119+
packer.packString(NAME);
120120
packer.packString(name);
121121
}
122122
if(extras != null) {
123-
packer.packString("extras");
123+
packer.packString(EXTRAS);
124124
extras.write(packer);
125125
}
126126
}
@@ -173,7 +173,7 @@ public Batch(String[] channels, Message[] messages) {
173173
}
174174

175175
public Batch(Collection<String> channels, Collection<Message> messages) {
176-
this(channels.toArray(new String[channels.size()]), messages.toArray(new Message[messages.size()]));
176+
this(channels.toArray(new String[0]), messages.toArray(new Message[0]));
177177
}
178178

179179
public void writeMsgpack(MessagePacker packer) throws IOException {

lib/src/main/java/io/ably/lib/types/MessageExtras.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,8 @@ public JsonObject asJsonObject() {
5353
return jsonObject;
5454
}
5555

56-
/* package private */ void write(MessagePacker packer) throws IOException {
57-
if (null == jsonObject) {
58-
// raw is null, so delta is not null
59-
packer.packMapHeader(1);
60-
packer.packString(DELTA);
61-
delta.write(packer);
62-
} else {
63-
// raw is not null, so delta can be ignored
56+
/* package private */ void write(MessagePacker packer) {
6457
Serialisation.gsonToMsgpack(jsonObject, packer);
65-
}
6658
}
6759

6860
/* package private */ static MessageExtras read(MessageUnpacker unpacker) throws IOException {
@@ -112,14 +104,12 @@ public boolean equals(Object o) {
112104
if (this == o) return true;
113105
if (o == null || getClass() != o.getClass()) return false;
114106
MessageExtras that = (MessageExtras) o;
115-
return (null == jsonObject) ?
116-
Objects.equals(delta, that.delta) :
117-
Objects.equals(jsonObject, that.jsonObject);
107+
return Objects.equals(jsonObject, that.jsonObject);
118108
}
119109

120110
@Override
121111
public int hashCode() {
122-
return (null == jsonObject) ? Objects.hashCode(delta) : Objects.hashCode(jsonObject);
112+
return Objects.hashCode(jsonObject);
123113
}
124114

125115
@Override
@@ -133,17 +123,7 @@ public String toString() {
133123
public static class Serializer implements JsonSerializer<MessageExtras> {
134124
@Override
135125
public JsonElement serialize(final MessageExtras src, final Type typeOfSrc, final JsonSerializationContext context) {
136-
return (null != src.jsonObject) ? src.jsonObject : wrapDelta(src.getDelta());
137-
}
138-
139-
public static JsonObject wrapDelta(final DeltaExtras delta) {
140-
if (null == delta) {
141-
throw new NullPointerException("delta cannot be null.");
142-
}
143-
144-
final JsonObject json = new JsonObject();
145-
json.add(DELTA, Serialisation.gson.toJsonTree(delta));
146-
return json;
126+
return src.jsonObject;
147127
}
148128
}
149129
}

0 commit comments

Comments
 (0)