|
| 1 | +package io.ably.lib.objects.type.counter; |
| 2 | + |
| 3 | +import io.ably.lib.objects.type.LiveObjectUpdate; |
| 4 | +import org.jetbrains.annotations.NotNull; |
| 5 | + |
| 6 | +/** |
| 7 | + * Represents an update that occurred on a LiveCounter object. |
| 8 | + * Contains information about counter value changes from increment/decrement operations. |
| 9 | + * Updates can represent positive changes (increments) or negative changes (decrements). |
| 10 | + * |
| 11 | + * @spec RTLC11, RTLC11a - LiveCounter update structure and behavior |
| 12 | + */ |
| 13 | +public class LiveCounterUpdate extends LiveObjectUpdate { |
| 14 | + |
| 15 | + /** |
| 16 | + * Creates a no-op LiveCounterUpdate representing no actual change. |
| 17 | + */ |
| 18 | + public LiveCounterUpdate() { |
| 19 | + super(null); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Creates a LiveCounterUpdate with the specified amount change. |
| 24 | + * |
| 25 | + * @param amount the amount by which the counter changed (positive = increment, negative = decrement) |
| 26 | + */ |
| 27 | + public LiveCounterUpdate(@NotNull Double amount) { |
| 28 | + super(new Update(amount)); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Gets the update information containing the amount of change. |
| 33 | + * |
| 34 | + * @return the Update object with the counter modification amount |
| 35 | + */ |
| 36 | + @NotNull |
| 37 | + public LiveCounterUpdate.Update getUpdate() { |
| 38 | + return (Update) update; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Returns a string representation of this LiveCounterUpdate. |
| 43 | + * |
| 44 | + * @return a string showing the amount of change to the counter |
| 45 | + */ |
| 46 | + @Override |
| 47 | + public String toString() { |
| 48 | + if (update == null) { |
| 49 | + return "LiveCounterUpdate{no change}"; |
| 50 | + } |
| 51 | + return "LiveCounterUpdate{amount=" + getUpdate().getAmount() + "}"; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Contains the specific details of a counter update operation. |
| 56 | + * |
| 57 | + * @spec RTLC11b, RTLC11b1 - Counter update data structure |
| 58 | + */ |
| 59 | + public static class Update { |
| 60 | + private final @NotNull Double amount; |
| 61 | + |
| 62 | + /** |
| 63 | + * Creates an Update with the specified amount. |
| 64 | + * |
| 65 | + * @param amount the counter change amount (positive = increment, negative = decrement) |
| 66 | + */ |
| 67 | + public Update(@NotNull Double amount) { |
| 68 | + this.amount = amount; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Gets the amount by which the counter value was modified. |
| 73 | + * |
| 74 | + * @return the change amount (positive for increments, negative for decrements) |
| 75 | + */ |
| 76 | + public @NotNull Double getAmount() { |
| 77 | + return amount; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments