diff --git a/CHANGELOG.md b/CHANGELOG.md index 951db65fda..71368a8634 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ * Java: Shadow `protobuf` dependency ([#2931](https://github.com/valkey-io/valkey-glide/pull/2931)) * Java: Add `RESP2` support ([#2383](https://github.com/valkey-io/valkey-glide/pull/2383)) * Node, Python: Add `IFEQ` option ([#2909](https://github.com/valkey-io/valkey-glide/pull/2909), [#2962](https://github.com/valkey-io/valkey-glide/pull/2962)) +* Java: Add `IFEQ` option ([#2978](https://github.com/valkey-io/valkey-glide/pull/2978)) #### Breaking Changes diff --git a/java/client/src/main/java/glide/api/commands/StringBaseCommands.java b/java/client/src/main/java/glide/api/commands/StringBaseCommands.java index 20f13c30f2..e033dab9f7 100644 --- a/java/client/src/main/java/glide/api/commands/StringBaseCommands.java +++ b/java/client/src/main/java/glide/api/commands/StringBaseCommands.java @@ -209,6 +209,7 @@ public interface StringBaseCommands { /** * Sets the given key with the given value. Return value is dependent on the passed options. * + * @since Valkey 8.1 and above * @see valkey.io for details. * @param key The key to store. * @param value The value to store with the given key. @@ -223,6 +224,12 @@ public interface StringBaseCommands { * String value = client.set("key", "value", options).get(); * assert value.equals("OK"); * } + *
{@code + * client.set("key", "value").get(); + * SetOptions options = SetOptions.builder().conditionalSet(ONLY_IF_EQUAL).comparisonValue("value")).build(); + * String value = client.set("key", "newValue", options).get(); + * assert value.equals("OK"); + * }*/ CompletableFuture
"OK"
. If value isn't set because
* of {@link ConditionalSet#ONLY_IF_EXISTS} or {@link ConditionalSet#ONLY_IF_DOES_NOT_EXIST}
- * conditions, return null
. If {@link SetOptionsBuilder#returnOldValue(boolean)}
- * is set, return the old value as a String
.
+ * or {@link ConditionalSet#ONLY_IF_EQUAL} conditions, return null
. If {@link
+ * SetOptionsBuilder#returnOldValue(boolean)} is set, return the old value as a String
+ *
.
* @example
* {@code * SetOptions options = SetOptions.builder().conditionalSet(ONLY_IF_EXISTS).expiry(Seconds(5L)).build(); diff --git a/java/client/src/main/java/glide/api/models/commands/SetOptions.java b/java/client/src/main/java/glide/api/models/commands/SetOptions.java index b010a99930..e2f30a481b 100644 --- a/java/client/src/main/java/glide/api/models/commands/SetOptions.java +++ b/java/client/src/main/java/glide/api/models/commands/SetOptions.java @@ -29,7 +29,7 @@ public final class SetOptions { */ private final ConditionalSet conditionalSet; - /** Value to compare whenIFEQ comparison-value
is set. */ + /** Value to compare when {@link ConditionalSet#ONLY_IF_EQUAL} is set. */ private final String comparisonValue; /** @@ -54,8 +54,8 @@ public enum ConditionalSet { */ ONLY_IF_DOES_NOT_EXIST("NX"), /** - * Only set the key if the current value equals the provided comparison value. Equivalent to - *IFEQ comparison-value
in the Valkey API. + * Only set the key if the current value of key equals the {@link SetOptions#comparisonValue}. + * Equivalent toIFEQ comparison-value
in the Valkey API. */ ONLY_IF_EQUAL("IFEQ"); @@ -163,8 +163,14 @@ public String[] toArgs() { // Add comparison value if ONLY_IF_EQUAL is selected if (conditionalSet == ConditionalSet.ONLY_IF_EQUAL) { - assert comparisonValue != null : "comparisonValue must be set for ONLY_IF_EQUAL condition."; + if (comparisonValue == null) { + throw new IllegalArgumentException( + "comparisonValue must be set when conditionalSet is ONLY_IF_EQUAL."); + } optionArgs.add(comparisonValue); + } else if (comparisonValue != null) { + throw new IllegalArgumentException( + "comparisonValue can only be set when conditionalSet is ONLY_IF_EQUAL."); } if (returnOldValue) {