2020package io .lettuce .core ;
2121
2222import io .lettuce .core .annotations .Experimental ;
23+ import io .lettuce .core .protocol .CommandArgs ;
24+ import io .lettuce .core .protocol .CommandKeyword ;
2325
2426/**
2527 * A compare condition to be used with commands that support conditional value checks (e.g. SET with IFEQ/IFNE/IFDEQ/IFDNE and
@@ -38,20 +40,39 @@ public final class ValueCondition<V> {
3840 * The kind of condition represented by this instance.
3941 */
4042 public enum Kind {
41- /** unconditional */
42- ALWAYS ,
43- /** key must exist */
44- EXISTS ,
45- /** key must not exist */
46- NOT_EXISTS ,
43+
4744 /** current value must equal provided value */
48- EQUAL ,
45+ EQUAL ( CommandKeyword . IFEQ , Param . VALUE ) ,
4946 /** current value must not equal provided value */
50- NOT_EQUAL ,
47+ NOT_EQUAL ( CommandKeyword . IFNE , Param . VALUE ) ,
5148 /** current value's digest must equal provided digest */
52- DIGEST_EQUAL ,
49+ DIGEST_EQUAL ( CommandKeyword . IFDEQ , Param . DIGEST ) ,
5350 /** current value's digest must not equal provided digest */
54- DIGEST_NOT_EQUAL
51+ DIGEST_NOT_EQUAL (CommandKeyword .IFDNE , Param .DIGEST );
52+
53+ private final CommandKeyword keyword ;
54+
55+ private final Param param ;
56+
57+ Kind (CommandKeyword keyword , Param param ) {
58+ this .keyword = keyword ;
59+ this .param = param ;
60+ }
61+
62+ /** The protocol keyword to emit for this condition. */
63+ public CommandKeyword keyword () {
64+ return keyword ;
65+ }
66+
67+ /** Indicates whether this condition uses a value or a digest parameter. */
68+ public Param param () {
69+ return param ;
70+ }
71+ }
72+
73+ /** Parameter kind for condition arguments. */
74+ enum Param {
75+ VALUE , DIGEST
5576 }
5677
5778 private final Kind kind ;
@@ -66,45 +87,53 @@ private ValueCondition(Kind kind, V value, String digestHex) {
6687 this .digestHex = digestHex ;
6788 }
6889
69- /** A condition that always applies (no comparison). */
70- public static <V > ValueCondition <V > always () {
71- return new ValueCondition <>(Kind .ALWAYS , null , null );
72-
73- }
74-
75- /** A condition that requires the key to exist (equivalent to XX in SET). */
76- public static <V > ValueCondition <V > exists () {
77- return new ValueCondition <>(Kind .EXISTS , null , null );
78- }
79-
80- /** A condition that requires the key to not exist (equivalent to NX in SET). */
81- public static <V > ValueCondition <V > notExists () {
82- return new ValueCondition <>(Kind .NOT_EXISTS , null , null );
90+ /**
91+ * Append this condition's protocol arguments to the given args.
92+ */
93+ public <K > void build (CommandArgs <K , V > args ) {
94+ args .add (kind .keyword ());
95+ switch (kind .param ()) {
96+ case VALUE :
97+ args .addValue (value );
98+ break ;
99+ case DIGEST :
100+ args .add (digestHex );
101+ break ;
102+ default :
103+ break ;
104+ }
83105 }
84106
85- /** A value-based comparison: set/delete only if the current value equals {@code value}. */
86- public static <V > ValueCondition <V > equal (V value ) {
107+ // Factory methods for creating value- and digest-based conditions
108+ /** Create a value-based equality condition; succeeds only if the current value equals the given value. */
109+ public static <V > ValueCondition <V > valueEq (V value ) {
87110 if (value == null )
88111 throw new IllegalArgumentException ("value must not be null" );
89112 return new ValueCondition <>(Kind .EQUAL , value , null );
90113 }
91114
92- /** A value-based comparison: set/delete only if the current value does not equal {@code value} . */
93- public static <V > ValueCondition <V > notEqual (V value ) {
115+ /** Create a value-based inequality condition; succeeds only if the current value does not equal the given value. */
116+ public static <V > ValueCondition <V > valueNe (V value ) {
94117 if (value == null )
95118 throw new IllegalArgumentException ("value must not be null" );
96119 return new ValueCondition <>(Kind .NOT_EQUAL , value , null );
97120 }
98121
99- /** A digest-based comparison: set/delete only if the current value's digest equals {@code hex16Digest}. */
100- public static <V > ValueCondition <V > digestEqualHex (String hex16Digest ) {
122+ /**
123+ * Create a digest-based equality condition; succeeds only if the current value's digest matches the given 16-character
124+ * lower-case hex digest.
125+ */
126+ public static <V > ValueCondition <V > digestEq (String hex16Digest ) {
101127 if (hex16Digest == null )
102128 throw new IllegalArgumentException ("digest must not be null" );
103129 return new ValueCondition <>(Kind .DIGEST_EQUAL , null , hex16Digest );
104130 }
105131
106- /** A digest-based comparison: set/delete only if the current value's digest does not equal {@code hex16Digest}. */
107- public static <V > ValueCondition <V > digestNotEqualHex (String hex16Digest ) {
132+ /**
133+ * Create a digest-based inequality condition; succeeds only if the current value's digest does not match the given
134+ * 16-character lower-case hex digest.
135+ */
136+ public static <V > ValueCondition <V > digestNe (String hex16Digest ) {
108137 if (hex16Digest == null )
109138 throw new IllegalArgumentException ("digest must not be null" );
110139 return new ValueCondition <>(Kind .DIGEST_NOT_EQUAL , null , hex16Digest );
0 commit comments