|
| 1 | +package io.ably.lib.objects; |
| 2 | + |
| 3 | +import io.ably.lib.types.Callback; |
| 4 | +import org.jetbrains.annotations.Blocking; |
| 5 | +import org.jetbrains.annotations.NonBlocking; |
| 6 | +import org.jetbrains.annotations.Contract; |
| 7 | +import org.jetbrains.annotations.NotNull; |
| 8 | +import org.jetbrains.annotations.Nullable; |
| 9 | +import org.jetbrains.annotations.Unmodifiable; |
| 10 | + |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +/** |
| 14 | + * The LiveMap interface provides methods to interact with a live, real-time map structure. |
| 15 | + * It supports both synchronous and asynchronous operations for managing key-value pairs. |
| 16 | + */ |
| 17 | +public interface LiveMap { |
| 18 | + |
| 19 | + /** |
| 20 | + * Retrieves the value associated with the specified key. |
| 21 | + * If this map object is tombstoned (deleted), `undefined` is returned. |
| 22 | + * If no entry is associated with the specified key, `undefined` is returned. |
| 23 | + * If map entry is tombstoned (deleted), `undefined` is returned. |
| 24 | + * If the value associated with the provided key is an objectId string of another LiveObject, a reference to that LiveObject |
| 25 | + * is returned, provided it exists in the local pool and is not tombstoned. Otherwise, `undefined` is returned. |
| 26 | + * If the value is not an objectId, then that value is returned. |
| 27 | + * |
| 28 | + * @param keyName the key whose associated value is to be returned. |
| 29 | + * @return the value associated with the specified key, or null if the key does not exist. |
| 30 | + */ |
| 31 | + @Nullable |
| 32 | + Object get(@NotNull String keyName); |
| 33 | + |
| 34 | + /** |
| 35 | + * Retrieves all entries (key-value pairs) in the map. |
| 36 | + * |
| 37 | + * @return an iterable collection of all entries in the map. |
| 38 | + */ |
| 39 | + @NotNull |
| 40 | + @Unmodifiable |
| 41 | + Iterable<Map.Entry<String, Object>> entries(); |
| 42 | + |
| 43 | + /** |
| 44 | + * Retrieves all keys in the map. |
| 45 | + * |
| 46 | + * @return an iterable collection of all keys in the map. |
| 47 | + */ |
| 48 | + @NotNull |
| 49 | + @Unmodifiable |
| 50 | + Iterable<String> keys(); |
| 51 | + |
| 52 | + /** |
| 53 | + * Retrieves all values in the map. |
| 54 | + * |
| 55 | + * @return an iterable collection of all values in the map. |
| 56 | + */ |
| 57 | + @NotNull |
| 58 | + @Unmodifiable |
| 59 | + Iterable<Object> values(); |
| 60 | + |
| 61 | + /** |
| 62 | + * Sets the specified key to the given value in the map. |
| 63 | + * Send a MAP_SET operation to the realtime system to set a key on this LiveMap object to a specified value. |
| 64 | + * This does not modify the underlying data of this LiveMap object. Instead, the change will be applied when |
| 65 | + * the published MAP_SET operation is echoed back to the client and applied to the object following the regular |
| 66 | + * operation application procedure. |
| 67 | + * |
| 68 | + * @param keyName the key to be set. |
| 69 | + * @param value the value to be associated with the key. |
| 70 | + */ |
| 71 | + @Blocking |
| 72 | + void set(@NotNull String keyName, @NotNull Object value); |
| 73 | + |
| 74 | + /** |
| 75 | + * Removes the specified key and its associated value from the map. |
| 76 | + * Send a MAP_REMOVE operation to the realtime system to tombstone a key on this LiveMap object. |
| 77 | + * This does not modify the underlying data of this LiveMap object. Instead, the change will be applied when |
| 78 | + * the published MAP_REMOVE operation is echoed back to the client and applied to the object following the regular |
| 79 | + * operation application procedure. |
| 80 | + * |
| 81 | + * @param keyName the key to be removed. |
| 82 | + */ |
| 83 | + @Blocking |
| 84 | + void remove(@NotNull String keyName); |
| 85 | + |
| 86 | + /** |
| 87 | + * Retrieves the number of entries in the map. |
| 88 | + * |
| 89 | + * @return the size of the map. |
| 90 | + */ |
| 91 | + @Contract(pure = true) // Indicates this method does not modify the state of the object. |
| 92 | + @NotNull |
| 93 | + Long size(); |
| 94 | + |
| 95 | + /** |
| 96 | + * Asynchronously sets the specified key to the given value in the map. |
| 97 | + * Send a MAP_SET operation to the realtime system to set a key on this LiveMap object to a specified value. |
| 98 | + * This does not modify the underlying data of this LiveMap object. Instead, the change will be applied when |
| 99 | + * the published MAP_SET operation is echoed back to the client and applied to the object following the regular |
| 100 | + * operation application procedure. |
| 101 | + * |
| 102 | + * @param keyName the key to be set. |
| 103 | + * @param value the value to be associated with the key. |
| 104 | + * @param callback the callback to handle the result or any errors. |
| 105 | + */ |
| 106 | + @NonBlocking |
| 107 | + void setAsync(@NotNull String keyName, @NotNull Object value, @NotNull Callback<Void> callback); |
| 108 | + |
| 109 | + /** |
| 110 | + * Asynchronously removes the specified key and its associated value from the map. |
| 111 | + * Send a MAP_REMOVE operation to the realtime system to tombstone a key on this LiveMap object. |
| 112 | + * This does not modify the underlying data of this LiveMap object. Instead, the change will be applied when |
| 113 | + * the published MAP_REMOVE operation is echoed back to the client and applied to the object following the regular |
| 114 | + * operation application procedure. |
| 115 | + * |
| 116 | + * @param keyName the key to be removed. |
| 117 | + * @param callback the callback to handle the result or any errors. |
| 118 | + */ |
| 119 | + @NonBlocking |
| 120 | + void removeAsync(@NotNull String keyName, @NotNull Callback<Void> callback); |
| 121 | +} |
0 commit comments