From 05cd90a85ac231d7860324b3bfb2ae8411b8b72f Mon Sep 17 00:00:00 2001 From: Maayan Shani Date: Sun, 2 Feb 2025 18:31:46 +0000 Subject: [PATCH] mutual exclusive Signed-off-by: Maayan Shani --- .../glide/api/models/commands/SetOptions.java | 65 ++++++++++--------- .../test/java/glide/api/GlideClientTest.java | 4 +- 2 files changed, 36 insertions(+), 33 deletions(-) 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 e27dfb300d..4b0bc0fc6d 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 @@ -63,52 +63,55 @@ public enum ConditionalSet { private final String valkeyApi; } - // Builder class for SetOptions + /** + * Builder class for {@link SetOptions}. + *

+ * Provides methods to set conditions under which a value should be set. + *

+ * Note: Calling any of these methods will override the existing values of + * {@code conditionalSet} and {@code comparisonValue}, if they are already set. + */ public static class SetOptionsBuilder { /** - * Sets the condition for the value to be set.
- * In order to set {@link ConditionalSet#ONLY_IF_EQUAL} use {@link #conditionalSetIfEqualTo}. + * Sets the condition to {@link ConditionalSet#ONLY_IF_EXISTS} for setting the value. + *

+ * This method overrides any previously set {@code conditionalSet} and + * {@code comparisonValue}. * - * @param conditionalSet The condition to set, either {@link ConditionalSet#ONLY_IF_EXISTS} or - * {@link ConditionalSet#ONLY_IF_DOES_NOT_EXIST}. * @return This builder instance - * @throws IllegalArgumentException if the conditionalSet is ONLY_IF_EQUAL - * @throws IllegalStateException if a conflicting condition has already been set */ - public SetOptionsBuilder conditionalSet(@NonNull ConditionalSet conditionalSet) { - if (conditionalSet == ConditionalSet.ONLY_IF_EQUAL) { - throw new IllegalArgumentException( - "For ONLY_IF_EQUAL, use the conditionalSetIfEqualTo(String value) method."); - } + public SetOptionsBuilder conditionalSetOnlyIfExists() { + this.conditionalSet = ConditionalSet.ONLY_IF_EXISTS; + this.comparisonValue = null; + return this; + } - if (this.conditionalSet == ConditionalSet.ONLY_IF_EQUAL) { - throw new IllegalStateException( - "Cannot set conditionalSet to " - + conditionalSet - + " when ONLY_IF_EQUAL is already set. Use either conditionalSet or" - + " conditionalSetIfEqualTo, not both."); - } - this.conditionalSet = conditionalSet; - this.comparisonValue = null; // Clear comparisonValue when not using ONLY_IF_EQUAL + /** + * Sets the condition to {@link ConditionalSet#ONLY_IF_DOES_NOT_EXIST} for setting the value. + *

+ * This method overrides any previously set {@code conditionalSet} and + * {@code comparisonValue}. + * + * @return This builder instance + */ + public SetOptionsBuilder conditionalSetOnlyIfNotExist() { + this.conditionalSet = ConditionalSet.ONLY_IF_DOES_NOT_EXIST; + this.comparisonValue = null; return this; } /** - * Set the key if the comparison value matches the existing value. + * Sets the condition to {@link ConditionalSet#ONLY_IF_EQUAL} for setting the value. + * The key will be set if the provided comparison value matches the existing value. + *

+ * This method overrides any previously set {@code conditionalSet} and + * {@code comparisonValue}. * * @since Valkey 8.1 and above. * @param value the value to compare * @return this builder instance - * @throws IllegalStateException if a conflicting condition has already been set */ - public SetOptionsBuilder conditionalSetIfEqualTo(@NonNull String value) { - if (this.conditionalSet != null && this.conditionalSet != ConditionalSet.ONLY_IF_EQUAL) { - throw new IllegalStateException( - "Cannot set conditionalSetIfEqualTo when " - + this.conditionalSet - + " is already set. Use either conditionalSet or conditionalSetIfEqualTo, not" - + " both."); - } + public SetOptionsBuilder conditionalSetOnlyIfEqualTo(@NonNull String value) { this.conditionalSet = ConditionalSet.ONLY_IF_EQUAL; this.comparisonValue = value; return this; diff --git a/java/client/src/test/java/glide/api/GlideClientTest.java b/java/client/src/test/java/glide/api/GlideClientTest.java index 2fdcb18c17..a4c74e4a86 100644 --- a/java/client/src/test/java/glide/api/GlideClientTest.java +++ b/java/client/src/test/java/glide/api/GlideClientTest.java @@ -976,7 +976,7 @@ public void set_with_SetOptions_OnlyIfEqual_success() { // Set `key` to `newValue` with the correct condition SetOptions setOptions = SetOptions.builder() - .conditionalSetIfEqualTo(value) // Key must currently have `value` + .conditionalSetOnlyIfEqualTo(value) // Key must currently have `value` .expiry(Expiry.UnixSeconds(60L)) .build(); String[] correctConditionArguments = @@ -1022,7 +1022,7 @@ public void set_with_SetOptions_OnlyIfEqual_fails() { // Attempt to set `key` to `newValue` with the wrong condition SetOptions wrongConditionOptions = SetOptions.builder() - .conditionalSetIfEqualTo(newValue) // Incorrect: current value of key is `value` + .conditionalSetOnlyIfEqualTo(newValue) // Incorrect: current value of key is `value` .expiry(Expiry.UnixSeconds(60L)) .build();