25
25
import java .util .ArrayList ;
26
26
import java .util .Collection ;
27
27
import java .util .Collections ;
28
+ import java .util .EnumSet ;
28
29
import java .util .List ;
29
30
import java .util .Optional ;
31
+ import java .util .Set ;
30
32
import java .util .concurrent .TimeUnit ;
31
33
import java .util .function .Function ;
32
34
37
39
import org .springframework .data .redis .connection .RedisZSetCommands .Limit ;
38
40
import org .springframework .data .redis .connection .RedisZSetCommands .Tuple ;
39
41
import org .springframework .data .redis .connection .RedisZSetCommands .Weights ;
42
+ import org .springframework .data .redis .connection .RedisZSetCommands .ZAddArgs .Flag ;
40
43
import org .springframework .data .redis .core .ScanOptions ;
41
44
import org .springframework .data .redis .util .ByteUtils ;
42
45
import org .springframework .lang .Nullable ;
@@ -61,23 +64,16 @@ public interface ReactiveZSetCommands {
61
64
class ZAddCommand extends KeyCommand {
62
65
63
66
private final List <Tuple > tuples ;
64
- private final boolean upsert ;
65
- private final boolean returnTotalChanged ;
67
+ private final Set <Flag > flags ;
66
68
private final boolean incr ;
67
- private final boolean gt ;
68
- private final boolean lt ;
69
69
70
- private ZAddCommand (@ Nullable ByteBuffer key , List <Tuple > tuples , boolean upsert , boolean returnTotalChanged ,
71
- boolean incr , boolean gt , boolean lt ) {
70
+ private ZAddCommand (@ Nullable ByteBuffer key , List <Tuple > tuples , Set <Flag > flags , boolean incr ) {
72
71
73
72
super (key );
74
73
75
74
this .tuples = tuples ;
76
- this .upsert = upsert ;
77
- this .returnTotalChanged = returnTotalChanged ;
75
+ this .flags = flags ;
78
76
this .incr = incr ;
79
- this .gt = gt ;
80
- this .lt = lt ;
81
77
}
82
78
83
79
/**
@@ -103,7 +99,7 @@ public static ZAddCommand tuples(Collection<? extends Tuple> tuples) {
103
99
104
100
Assert .notNull (tuples , "Tuples must not be null!" );
105
101
106
- return new ZAddCommand (null , new ArrayList <>(tuples ), false , false , false , false , false );
102
+ return new ZAddCommand (null , new ArrayList <>(tuples ), EnumSet . noneOf ( Flag . class ) , false );
107
103
}
108
104
109
105
/**
@@ -116,7 +112,7 @@ public ZAddCommand to(ByteBuffer key) {
116
112
117
113
Assert .notNull (key , "Key must not be null!" );
118
114
119
- return new ZAddCommand (key , tuples , upsert , returnTotalChanged , incr , gt , lt );
115
+ return new ZAddCommand (key , tuples , flags , incr );
120
116
}
121
117
122
118
/**
@@ -126,7 +122,11 @@ public ZAddCommand to(ByteBuffer key) {
126
122
* @return a new {@link ZAddCommand} with {@literal xx} applied.
127
123
*/
128
124
public ZAddCommand xx () {
129
- return new ZAddCommand (getKey (), tuples , false , returnTotalChanged , incr , gt , lt );
125
+
126
+ EnumSet <Flag > flags = EnumSet .copyOf (this .flags );
127
+ flags .remove (Flag .NX );
128
+ flags .add (Flag .XX );
129
+ return new ZAddCommand (getKey (), tuples , flags , incr );
130
130
}
131
131
132
132
/**
@@ -136,7 +136,11 @@ public ZAddCommand xx() {
136
136
* @return a new {@link ZAddCommand} with {@literal nx} applied.
137
137
*/
138
138
public ZAddCommand nx () {
139
- return new ZAddCommand (getKey (), tuples , true , returnTotalChanged , incr , gt , lt );
139
+
140
+ EnumSet <Flag > flags = EnumSet .copyOf (this .flags );
141
+ flags .remove (Flag .XX );
142
+ flags .add (Flag .NX );
143
+ return new ZAddCommand (getKey (), tuples , flags , incr );
140
144
}
141
145
142
146
/**
@@ -146,17 +150,20 @@ public ZAddCommand nx() {
146
150
* @return a new {@link ZAddCommand} with {@literal ch} applied.
147
151
*/
148
152
public ZAddCommand ch () {
149
- return new ZAddCommand (getKey (), tuples , upsert , true , incr , gt , lt );
153
+
154
+ EnumSet <Flag > flags = EnumSet .copyOf (this .flags );
155
+ flags .add (Flag .CH );
156
+ return new ZAddCommand (getKey (), tuples , flags , incr );
150
157
}
151
158
152
159
/**
153
160
* Applies {@literal incr} mode (When this option is specified ZADD acts like ZINCRBY). Constructs a new command
154
- * instance with all previously configured properties.
161
+ * instance with all previously configured properties. Note that the command result returns the score of the member.
155
162
*
156
163
* @return a new {@link ZAddCommand} with {@literal incr} applied.
157
164
*/
158
165
public ZAddCommand incr () {
159
- return new ZAddCommand (getKey (), tuples , upsert , upsert , true , gt , lt );
166
+ return new ZAddCommand (getKey (), tuples , flags , true );
160
167
}
161
168
162
169
/**
@@ -166,7 +173,11 @@ public ZAddCommand incr() {
166
173
* @since 2.5
167
174
*/
168
175
public ZAddCommand gt () {
169
- return new ZAddCommand (getKey (), tuples , upsert , upsert , incr , true , lt );
176
+
177
+ EnumSet <Flag > flags = EnumSet .copyOf (this .flags );
178
+ flags .remove (Flag .LT );
179
+ flags .add (Flag .GT );
180
+ return new ZAddCommand (getKey (), tuples , flags , incr );
170
181
}
171
182
172
183
/**
@@ -176,7 +187,11 @@ public ZAddCommand gt() {
176
187
* @since 2.5
177
188
*/
178
189
public ZAddCommand lt () {
179
- return new ZAddCommand (getKey (), tuples , upsert , upsert , incr , gt , true );
190
+
191
+ EnumSet <Flag > flags = EnumSet .copyOf (this .flags );
192
+ flags .remove (Flag .GT );
193
+ flags .add (Flag .LT );
194
+ return new ZAddCommand (getKey (), tuples , flags , incr );
180
195
}
181
196
182
197
/**
@@ -187,10 +202,26 @@ public List<Tuple> getTuples() {
187
202
}
188
203
189
204
/**
190
- * @return
205
+ * @return {@code true} if the command does not contain NX or XX flags.
191
206
*/
192
207
public boolean isUpsert () {
193
- return upsert ;
208
+ return !flags .contains (Flag .NX ) && !flags .contains (Flag .XX );
209
+ }
210
+
211
+ /**
212
+ * @return {@code true} if the command contains the XX flag.
213
+ * @since 2.7.17
214
+ */
215
+ public boolean isIfExists () {
216
+ return flags .contains (Flag .XX );
217
+ }
218
+
219
+ /**
220
+ * @return {@code true} if the command contains the NX flag.
221
+ * @since 2.7.17
222
+ */
223
+ public boolean isIfNotExists () {
224
+ return flags .contains (Flag .NX );
194
225
}
195
226
196
227
/**
@@ -205,22 +236,22 @@ public boolean isIncr() {
205
236
* @since 2.5
206
237
*/
207
238
public boolean isGt () {
208
- return gt ;
239
+ return flags . contains ( Flag . GT ) ;
209
240
}
210
241
211
242
/**
212
243
* @return {@literal true} if {@literal LT} is set.
213
244
* @since 2.5
214
245
*/
215
246
public boolean isLt () {
216
- return lt ;
247
+ return flags . contains ( Flag . LT ) ;
217
248
}
218
249
219
250
/**
220
251
* @return
221
252
*/
222
253
public boolean isReturnTotalChanged () {
223
- return returnTotalChanged ;
254
+ return flags . contains ( Flag . CH ) ;
224
255
}
225
256
}
226
257
0 commit comments