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 e033dab9f7..c2612198c3 100644 --- a/java/client/src/main/java/glide/api/commands/StringBaseCommands.java +++ b/java/client/src/main/java/glide/api/commands/StringBaseCommands.java @@ -226,7 +226,7 @@ public interface StringBaseCommands { * } *
{@code
      * client.set("key", "value").get();
-     * SetOptions options = SetOptions.builder().conditionalSet(ONLY_IF_EQUAL).comparisonValue("value")).build();
+     * SetOptions options = SetOptions.builder().conditionalSetIfEqualTo("value").build();
      * String value = client.set("key", "newValue", options).get();
      * assert value.equals("OK");
      * }
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 e2f30a481b..6de5d8e908 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 @@ -62,6 +62,27 @@ public enum ConditionalSet { private final String valkeyApi; } + // Builder class for SetOptions + public static class SetOptionsBuilder { + private ConditionalSet conditionalSet; + private String comparisonValue; + + /** + * Set the condition to ONLY_IF_EQUAL and specify the comparison value + * + * @param value the value to compare + * @return this builder instance + */ + public SetOptionsBuilder conditionalSetIfEqualTo(String value) { + if (value == null || value.isEmpty()) { + throw new IllegalArgumentException("comparisonValue cannot be null or empty."); + } + this.conditionalSet = ConditionalSet.ONLY_IF_EQUAL; + this.comparisonValue = value; + return this; + } + } + /** Configuration of value lifetime. */ public static final class Expiry { diff --git a/java/client/src/test/java/glide/api/GlideClientTest.java b/java/client/src/test/java/glide/api/GlideClientTest.java index f1fadd2eb1..1b896dedc9 100644 --- a/java/client/src/test/java/glide/api/GlideClientTest.java +++ b/java/client/src/test/java/glide/api/GlideClientTest.java @@ -976,8 +976,7 @@ public void set_with_SetOptions_OnlyIfEqual_success() { // Set `key` to `newValue` with the correct condition SetOptions setOptions = SetOptions.builder() - .conditionalSet(ONLY_IF_EQUAL) - .comparisonValue(value) // Key must currently have `value` + .conditionalSetIfEqualTo(value) // Key must currently have `value` .expiry(Expiry.UnixSeconds(60L)) .build(); String[] correctConditionArguments = @@ -1023,8 +1022,7 @@ public void set_with_SetOptions_OnlyIfEqual_fails() { // Attempt to set `key` to `newValue` with the wrong condition SetOptions wrongConditionOptions = SetOptions.builder() - .conditionalSet(ONLY_IF_EQUAL) - .comparisonValue(newValue) // Incorrect condition: current value of key is `value` + .conditionalSetIfEqualTo(newValue) // Incorrect condition: current value of key is `value` .expiry(Expiry.UnixSeconds(60L)) .build();