Skip to content

Commit 431cfe7

Browse files
committed
[ECO-5375] Updated code as per review comments
1. Updated enum ObjectOperationAction with PascalCase values 2. Created separate file for adapter that extends LiveObjectsAdapter 3. Added custom Binary type to handle ByteArray values
1 parent 375328e commit 431cfe7

File tree

8 files changed

+92
-95
lines changed

8 files changed

+92
-95
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.ably.lib.objects;
2+
3+
import io.ably.lib.realtime.AblyRealtime;
4+
import io.ably.lib.realtime.CompletionListener;
5+
import io.ably.lib.types.AblyException;
6+
import io.ably.lib.types.ProtocolMessage;
7+
import io.ably.lib.util.Log;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public class Adapter implements LiveObjectsAdapter {
11+
private final AblyRealtime ably;
12+
private static final String TAG = LiveObjectsAdapter.class.getName();
13+
14+
public Adapter(@NotNull AblyRealtime ably) {
15+
this.ably = ably;
16+
}
17+
18+
@Override
19+
public void setChannelSerial(@NotNull String channelName, @NotNull String channelSerial) {
20+
if (ably.channels.containsKey(channelName)) {
21+
ably.channels.get(channelName).properties.channelSerial = channelSerial;
22+
} else {
23+
Log.e(TAG, "setChannelSerial(): channel not found: " + channelName);
24+
}
25+
}
26+
27+
@Override
28+
public void send(ProtocolMessage msg, CompletionListener listener) throws AblyException {
29+
// Always queue LiveObjects messages to ensure reliable state synchronization and proper acknowledgment
30+
ably.connection.connectionManager.send(msg, true, listener);
31+
}
32+
}
Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
11
package io.ably.lib.objects;
22

3-
import io.ably.lib.plugins.PluginConnectionAdapter;
4-
import io.ably.lib.realtime.AblyRealtime;
53
import io.ably.lib.realtime.CompletionListener;
64
import io.ably.lib.types.AblyException;
75
import io.ably.lib.types.ProtocolMessage;
8-
import io.ably.lib.util.Log;
96
import org.jetbrains.annotations.NotNull;
107

11-
public interface LiveObjectsAdapter extends PluginConnectionAdapter {
12-
void setChannelSerial(@NotNull String channelName, @NotNull String channelSerial);
13-
14-
class Adapter implements LiveObjectsAdapter {
15-
private final AblyRealtime ably;
16-
private static final String TAG = LiveObjectsAdapter.class.getName();
17-
18-
public Adapter(@NotNull AblyRealtime ably) {
19-
this.ably = ably;
20-
}
8+
public interface LiveObjectsAdapter {
9+
/**
10+
* Sends a protocol message to its intended recipient.
11+
* This method transmits a protocol message, allowing for queuing events if necessary,
12+
* and notifies the provided listener upon the success or failure of the send operation.
13+
*
14+
* @param msg the protocol message to send.
15+
* @param listener a listener to be notified of the success or failure of the send operation.
16+
* @throws AblyException if an error occurs during the send operation.
17+
*/
18+
void send(ProtocolMessage msg, CompletionListener listener) throws AblyException;
2119

22-
@Override
23-
public void setChannelSerial(@NotNull String channelName, @NotNull String channelSerial) {
24-
if (ably.channels.containsKey(channelName)) {
25-
ably.channels.get(channelName).properties.channelSerial = channelSerial;
26-
} else {
27-
Log.e(TAG, "setChannelSerial(): channel not found: " + channelName);
28-
}
29-
}
30-
31-
@Override
32-
public void send(ProtocolMessage msg, CompletionListener listener) throws AblyException {
33-
// Always queue LiveObjects messages to ensure reliable state synchronization and proper acknowledgment
34-
ably.connection.connectionManager.send(msg, true, listener);
35-
}
36-
}
20+
/**
21+
* Sets the channel serial for a specific channel.
22+
* @param channelName the name of the channel for which to set the serial
23+
* @param channelSerial the serial to set for the channel
24+
*/
25+
void setChannelSerial(@NotNull String channelName, @NotNull String channelSerial);
3726
}
27+

lib/src/main/java/io/ably/lib/objects/LiveObjectsPlugin.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package io.ably.lib.objects;
22

3-
import io.ably.lib.plugins.PluginInstance;
3+
import io.ably.lib.types.ProtocolMessage;
44
import org.jetbrains.annotations.NotNull;
55

66
/**
77
* The LiveObjectsPlugin interface provides a mechanism for managing and interacting with
88
* live data objects in a real-time environment. It allows for the retrieval, disposal, and
99
* management of LiveObjects instances associated with specific channel names.
1010
*/
11-
public interface LiveObjectsPlugin extends PluginInstance {
11+
public interface LiveObjectsPlugin {
1212

1313
/**
1414
* Retrieves an instance of LiveObjects associated with the specified channel name.
@@ -21,6 +21,15 @@ public interface LiveObjectsPlugin extends PluginInstance {
2121
@NotNull
2222
LiveObjects getInstance(@NotNull String channelName);
2323

24+
/**
25+
* Handles a protocol message.
26+
* This method is invoked whenever a protocol message is received, allowing the implementation
27+
* to process the message and take appropriate actions.
28+
*
29+
* @param message the protocol message to handle.
30+
*/
31+
void handle(@NotNull ProtocolMessage message);
32+
2433
/**
2534
* Disposes of the LiveObjects instance associated with the specified channel name.
2635
* This method removes the LiveObjects instance for the given channel, releasing any
@@ -29,4 +38,9 @@ public interface LiveObjectsPlugin extends PluginInstance {
2938
* @param channelName the name of the channel whose LiveObjects instance is to be removed.
3039
*/
3140
void dispose(@NotNull String channelName);
41+
42+
/**
43+
* Disposes of the plugin instance and all underlying resources.
44+
*/
45+
void dispose();
3246
}

lib/src/main/java/io/ably/lib/plugins/PluginConnectionAdapter.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/src/main/java/io/ably/lib/plugins/PluginInstance.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/src/main/java/io/ably/lib/realtime/AblyRealtime.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.List;
77
import java.util.Map;
88

9+
import io.ably.lib.objects.Adapter;
910
import io.ably.lib.objects.LiveObjectsAdapter;
1011
import io.ably.lib.objects.LiveObjectsPlugin;
1112
import io.ably.lib.rest.AblyRest;
@@ -187,7 +188,7 @@ public interface Channels extends ReadOnlyMap<String, Channel> {
187188
private LiveObjectsPlugin tryInitializeLiveObjectsPlugin() {
188189
try {
189190
Class<?> liveObjectsImplementation = Class.forName("io.ably.lib.objects.DefaultLiveObjectsPlugin");
190-
LiveObjectsAdapter adapter = new LiveObjectsAdapter.Adapter(this);
191+
LiveObjectsAdapter adapter = new Adapter(this);
191192
return (LiveObjectsPlugin) liveObjectsImplementation
192193
.getDeclaredConstructor(LiveObjectsAdapter.class)
193194
.newInstance(adapter);

live-objects/src/main/kotlin/io/ably/lib/objects/Helpers.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,21 @@ internal suspend fun LiveObjectsAdapter.sendAsync(message: ProtocolMessage) {
2323
deferred.await()
2424
}
2525

26-
internal enum class MessageFormat(private val value: String) {
27-
MSGPACK("msgpack"),
28-
JSON("json");
26+
internal enum class ProtocolMessageFormat(private val value: String) {
27+
Msgpack("msgpack"),
28+
Json("json");
2929

3030
override fun toString(): String = value
3131
}
32+
33+
internal class Binary(val data: ByteArray?) {
34+
override fun equals(other: Any?): Boolean {
35+
if (this === other) return true
36+
if (other !is Binary) return false
37+
return data?.contentEquals(other.data) == true
38+
}
39+
40+
override fun hashCode(): Int {
41+
return data?.contentHashCode() ?: 0
42+
}
43+
}

live-objects/src/main/kotlin/io/ably/lib/objects/ObjectMessage.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package io.ably.lib.objects
22

3-
import java.nio.ByteBuffer
4-
53
/**
64
* An enum class representing the different actions that can be performed on an object.
75
* Spec: OOP2
86
*/
97
internal enum class ObjectOperationAction(val code: Int) {
10-
MAP_CREATE(0),
11-
MAP_SET(1),
12-
MAP_REMOVE(2),
13-
COUNTER_CREATE(3),
14-
COUNTER_INC(4),
15-
OBJECT_DELETE(5);
8+
MapCreate(0),
9+
MapSet(1),
10+
MapRemove(2),
11+
CounterCreate(3),
12+
CounterInc(4),
13+
ObjectDelete(5);
1614
}
1715

1816
/**
@@ -190,12 +188,12 @@ internal data class ObjectOperation(
190188
* the initialValue, nonce, and initialValueEncoding will be removed.
191189
* Spec: OOP3h
192190
*/
193-
val initialValue: ByteBuffer? = null,
191+
val initialValue: Binary? = null,
194192

195193
/** The initial value encoding defines how the initialValue should be interpreted.
196194
* Spec: OOP3i
197195
*/
198-
val initialValueEncoding: MessageFormat? = null
196+
val initialValueEncoding: ProtocolMessageFormat? = null
199197
)
200198

201199
/**
@@ -301,7 +299,7 @@ internal data class ObjectMessage(
301299
* the `ProtocolMessage` encapsulating it is `OBJECT_SYNC`.
302300
* Spec: OM2g
303301
*/
304-
val `object`: ObjectState? = null,
302+
val objectState: ObjectState? = null,
305303

306304
/**
307305
* An opaque string that uniquely identifies this object message.

0 commit comments

Comments
 (0)