Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor MessageExtras #595

Merged
merged 5 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/src/main/java/io/ably/lib/types/BaseMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,16 @@ public static JsonObject toJsonObject(final BaseMessage message) {
if(data != null) {
if(data instanceof byte[]) {
byte[] dataBytes = (byte[])data;
json.addProperty("data", new String(Base64Coder.encode(dataBytes)));
json.addProperty(DATA, new String(Base64Coder.encode(dataBytes)));
encoding = (encoding == null) ? "base64" : encoding + "/base64";
} else {
json.addProperty("data", data.toString());
json.addProperty(DATA, data.toString());
}
if(encoding != null) json.addProperty("encoding", encoding);
if(encoding != null) json.addProperty(ENCODING, encoding);
}
if(message.id != null) json.addProperty("id", message.id);
if(message.clientId != null) json.addProperty("clientId", message.clientId);
if(message.connectionId != null) json.addProperty("connectionId", message.connectionId);
if(message.id != null) json.addProperty(ID, message.id);
if(message.clientId != null) json.addProperty(CLIENT_ID, message.clientId);
if(message.connectionId != null) json.addProperty(CONNECTION_ID, message.connectionId);
return json;
}

Expand Down
11 changes: 0 additions & 11 deletions lib/src/main/java/io/ably/lib/types/DeltaExtras.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.ably.lib.types;

import com.google.gson.JsonObject;
import org.msgpack.core.MessagePacker;
import org.msgpack.value.Value;
import org.msgpack.value.ValueFactory;

Expand Down Expand Up @@ -48,16 +47,6 @@ public String getFrom() {
return from;
}

/* package private */ void write(MessagePacker packer) throws IOException {
packer.packMapHeader(2);

packer.packString(FORMAT);
packer.packString(format);

packer.packString(FROM);
packer.packString(from);
}

/* package private */ static DeltaExtras read(final Map<Value, Value> map) throws IOException {
final Value format = map.get(ValueFactory.newString(FORMAT));
final Value from = map.get(ValueFactory.newString(FROM));
Expand Down
6 changes: 3 additions & 3 deletions lib/src/main/java/io/ably/lib/types/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ void writeMsgpack(MessagePacker packer) throws IOException {
packer.packMapHeader(fieldCount);
super.writeFields(packer);
if(name != null) {
packer.packString("name");
packer.packString(NAME);
packer.packString(name);
}
if(extras != null) {
packer.packString("extras");
packer.packString(EXTRAS);
extras.write(packer);
}
}
Expand Down Expand Up @@ -152,7 +152,7 @@ public Batch(String[] channels, Message[] messages) {
}

public Batch(Collection<String> channels, Collection<Message> messages) {
this(channels.toArray(new String[channels.size()]), messages.toArray(new Message[messages.size()]));
this(channels.toArray(new String[0]), messages.toArray(new Message[0]));
}

public void writeMsgpack(MessagePacker packer) throws IOException {
Expand Down
28 changes: 4 additions & 24 deletions lib/src/main/java/io/ably/lib/types/MessageExtras.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,8 @@ public JsonObject asJsonObject() {
return jsonObject;
}

/* package private */ void write(MessagePacker packer) throws IOException {
if (null == jsonObject) {
// raw is null, so delta is not null
packer.packMapHeader(1);
packer.packString(DELTA);
delta.write(packer);
} else {
// raw is not null, so delta can be ignored
/* package private */ void write(MessagePacker packer) {
Serialisation.gsonToMsgpack(jsonObject, packer);
}
}

/* package private */ static MessageExtras read(MessageUnpacker unpacker) throws IOException {
Expand Down Expand Up @@ -112,14 +104,12 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MessageExtras that = (MessageExtras) o;
return (null == jsonObject) ?
Objects.equals(delta, that.delta) :
Objects.equals(jsonObject, that.jsonObject);
return Objects.equals(jsonObject, that.jsonObject);
}

@Override
public int hashCode() {
return (null == jsonObject) ? Objects.hashCode(delta) : Objects.hashCode(jsonObject);
return Objects.hashCode(jsonObject);
}

@Override
Expand All @@ -133,17 +123,7 @@ public String toString() {
public static class Serializer implements JsonSerializer<MessageExtras> {
@Override
public JsonElement serialize(final MessageExtras src, final Type typeOfSrc, final JsonSerializationContext context) {
return (null != src.jsonObject) ? src.jsonObject : wrapDelta(src.getDelta());
}

public static JsonObject wrapDelta(final DeltaExtras delta) {
if (null == delta) {
throw new NullPointerException("delta cannot be null.");
}

final JsonObject json = new JsonObject();
json.add(DELTA, Serialisation.gson.toJsonTree(delta));
return json;
return src.jsonObject;
}
}
}