|
| 1 | +package io.ably.lib.object.instance; |
| 2 | + |
| 3 | +import com.google.gson.JsonElement; |
| 4 | +import io.ably.lib.object.ValueType; |
| 5 | +import io.ably.lib.object.instance.types.BinaryInstance; |
| 6 | +import io.ably.lib.object.instance.types.BooleanInstance; |
| 7 | +import io.ably.lib.object.instance.types.JsonArrayInstance; |
| 8 | +import io.ably.lib.object.instance.types.JsonObjectInstance; |
| 9 | +import io.ably.lib.object.instance.types.LiveCounterInstance; |
| 10 | +import io.ably.lib.object.instance.types.LiveMapInstance; |
| 11 | +import io.ably.lib.object.instance.types.NumberInstance; |
| 12 | +import io.ably.lib.object.instance.types.StringInstance; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | + |
| 15 | +/** |
| 16 | + * A direct-reference view of a single resolved LiveObject ({@code LiveMap} or |
| 17 | + * {@code LiveCounter}) or primitive value. |
| 18 | + * |
| 19 | + * <p>Unlike {@code PathObject}, which re-resolves its path on every call, an |
| 20 | + * {@code Instance} is identity-addressed: it is bound to a specific underlying value |
| 21 | + * and dereferenced in O(1), regardless of where that value sits in the graph. Read |
| 22 | + * operations validate the access API preconditions and fail with an |
| 23 | + * {@code AblyException} if they are not satisfied. |
| 24 | + * |
| 25 | + * <p>This base type exposes only the methods whose behaviour is independent of the |
| 26 | + * wrapped type; everything else - including {@code subscribe} (RTTS7b) - is |
| 27 | + * partitioned onto the sub-types. Use the {@code as*} helpers to obtain a sub-type |
| 28 | + * view without type validation, or discriminate via {@link #getType()}. |
| 29 | + * |
| 30 | + * <p>Spec: RTINS1, RTTS7 |
| 31 | + * |
| 32 | + * @see LiveMapInstance |
| 33 | + * @see LiveCounterInstance |
| 34 | + * @see InstanceListener |
| 35 | + */ |
| 36 | +public interface Instance { |
| 37 | + |
| 38 | + /** |
| 39 | + * Returns the {@link ValueType} of the value wrapped by this instance. Use this |
| 40 | + * instead of dedicated {@code isLiveMap}/{@code isLiveCounter}/etc. checks. |
| 41 | + * |
| 42 | + * <p>An {@code Instance} is always constructed from a resolved value, so this never |
| 43 | + * returns {@link ValueType#UNKNOWN} in normal operation. |
| 44 | + * |
| 45 | + * <p>Spec: RTTS8a |
| 46 | + * |
| 47 | + * @return the wrapped value type |
| 48 | + */ |
| 49 | + @NotNull ValueType getType(); |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns a JSON-serializable, recursively compacted snapshot of the wrapped value. |
| 53 | + * Behaves identically to {@code PathObject#compactJson} except that it operates on |
| 54 | + * the wrapped value directly instead of resolving a path. An {@code Instance} is |
| 55 | + * always bound to a resolved value, so this always returns a non-null result; |
| 56 | + * failures of the access API preconditions are signalled via {@code AblyException}. |
| 57 | + * |
| 58 | + * <p>Spec: RTINS11 / RTINS11c (universal non-null invariant - Instance is bound |
| 59 | + * to an already-resolved value, so the path-resolution failure mode of |
| 60 | + * PathObject#compactJson does not apply) / RTTS7a (typed-SDK signature reflects |
| 61 | + * the universal invariant) |
| 62 | + * |
| 63 | + * @return the compacted JSON snapshot |
| 64 | + */ |
| 65 | + @NotNull JsonElement compactJson(); |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns this instance wrapped as a {@link LiveMapInstance}. |
| 69 | + * |
| 70 | + * <p>Best-effort cast; does not validate the underlying type. Read operations on |
| 71 | + * the returned wrapper are always permitted; write/terminal operations will fail |
| 72 | + * at call time if the wrapped value is not a {@code LiveMap}. |
| 73 | + * |
| 74 | + * <p>Spec: RTTS9a |
| 75 | + * |
| 76 | + * @return a {@link LiveMapInstance} view of this instance |
| 77 | + */ |
| 78 | + @NotNull LiveMapInstance asLiveMap(); |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns this instance wrapped as a {@link LiveCounterInstance}. |
| 82 | + * Best-effort cast; does not validate the underlying type. |
| 83 | + * |
| 84 | + * <p>Spec: RTTS9b |
| 85 | + * |
| 86 | + * @return a {@link LiveCounterInstance} view of this instance |
| 87 | + */ |
| 88 | + @NotNull LiveCounterInstance asLiveCounter(); |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns this instance wrapped as a {@link NumberInstance}. |
| 92 | + * Best-effort cast; does not validate the underlying type. |
| 93 | + * |
| 94 | + * <p>Spec: RTTS9c |
| 95 | + * |
| 96 | + * @return a {@link NumberInstance} view of this instance |
| 97 | + */ |
| 98 | + @NotNull NumberInstance asNumber(); |
| 99 | + |
| 100 | + /** |
| 101 | + * Returns this instance wrapped as a {@link StringInstance}. |
| 102 | + * Best-effort cast; does not validate the underlying type. |
| 103 | + * |
| 104 | + * <p>Spec: RTTS9c |
| 105 | + * |
| 106 | + * @return a {@link StringInstance} view of this instance |
| 107 | + */ |
| 108 | + @NotNull StringInstance asString(); |
| 109 | + |
| 110 | + /** |
| 111 | + * Returns this instance wrapped as a {@link BooleanInstance}. |
| 112 | + * Best-effort cast; does not validate the underlying type. |
| 113 | + * |
| 114 | + * <p>Spec: RTTS9c |
| 115 | + * |
| 116 | + * @return a {@link BooleanInstance} view of this instance |
| 117 | + */ |
| 118 | + @NotNull BooleanInstance asBoolean(); |
| 119 | + |
| 120 | + /** |
| 121 | + * Returns this instance wrapped as a {@link BinaryInstance}. |
| 122 | + * Best-effort cast; does not validate the underlying type. |
| 123 | + * |
| 124 | + * <p>Spec: RTTS9c |
| 125 | + * |
| 126 | + * @return a {@link BinaryInstance} view of this instance |
| 127 | + */ |
| 128 | + @NotNull BinaryInstance asBinary(); |
| 129 | + |
| 130 | + /** |
| 131 | + * Returns this instance wrapped as a {@link JsonObjectInstance}. |
| 132 | + * Best-effort cast; does not validate the underlying type. |
| 133 | + * |
| 134 | + * <p>Spec: RTTS9c |
| 135 | + * |
| 136 | + * @return a {@link JsonObjectInstance} view of this instance |
| 137 | + */ |
| 138 | + @NotNull JsonObjectInstance asJsonObject(); |
| 139 | + |
| 140 | + /** |
| 141 | + * Returns this instance wrapped as a {@link JsonArrayInstance}. |
| 142 | + * Best-effort cast; does not validate the underlying type. |
| 143 | + * |
| 144 | + * <p>Spec: RTTS9c |
| 145 | + * |
| 146 | + * @return a {@link JsonArrayInstance} view of this instance |
| 147 | + */ |
| 148 | + @NotNull JsonArrayInstance asJsonArray(); |
| 149 | +} |
0 commit comments