16
16
17
17
package com .mongodb .client .model .expressions ;
18
18
19
+ import com .mongodb .annotations .Beta ;
20
+ import com .mongodb .assertions .Assertions ;
21
+
19
22
import java .util .ArrayList ;
20
23
import java .util .List ;
21
24
import java .util .function .Function ;
28
31
* to succeed will produce the value that it specifies. If no check succeeds,
29
32
* then the operation
30
33
* {@linkplain BranchesIntermediary#defaults(Function) defaults} to a default
31
- * value, or if none is specified, the operation causes an error.
34
+ * value, or if none is specified, the operation will cause an error.
32
35
*
33
36
* @param <T> the type of the values that may be checked.
37
+ * @since 4.9.0
34
38
*/
39
+ @ Beta (Beta .Reason .CLIENT )
35
40
public final class Branches <T extends Expression > {
36
41
37
42
Branches () {
@@ -59,6 +64,8 @@ private static <T extends Expression> MqlExpression<?> mqlEx(final T value) {
59
64
* @return the appended sequence of checks.
60
65
*/
61
66
public <R extends Expression > BranchesIntermediary <T , R > is (final Function <? super T , BooleanExpression > predicate , final Function <? super T , ? extends R > mapping ) {
67
+ Assertions .notNull ("predicate" , predicate );
68
+ Assertions .notNull ("mapping" , mapping );
62
69
return with (value -> new SwitchCase <>(predicate .apply (value ), mapping .apply (value )));
63
70
}
64
71
@@ -74,6 +81,8 @@ public <R extends Expression> BranchesIntermediary<T, R> is(final Function<? sup
74
81
* @return the appended sequence of checks.
75
82
*/
76
83
public <R extends Expression > BranchesIntermediary <T , R > eq (final T v , final Function <? super T , ? extends R > mapping ) {
84
+ Assertions .notNull ("v" , v );
85
+ Assertions .notNull ("mapping" , mapping );
77
86
return is (value -> value .eq (v ), mapping );
78
87
}
79
88
@@ -89,6 +98,8 @@ public <R extends Expression> BranchesIntermediary<T, R> eq(final T v, final Fun
89
98
* @return the appended sequence of checks.
90
99
*/
91
100
public <R extends Expression > BranchesIntermediary <T , R > lt (final T v , final Function <? super T , ? extends R > mapping ) {
101
+ Assertions .notNull ("v" , v );
102
+ Assertions .notNull ("mapping" , mapping );
92
103
return is (value -> value .lt (v ), mapping );
93
104
}
94
105
@@ -104,6 +115,8 @@ public <R extends Expression> BranchesIntermediary<T, R> lt(final T v, final Fun
104
115
* @return the appended sequence of checks.
105
116
*/
106
117
public <R extends Expression > BranchesIntermediary <T , R > lte (final T v , final Function <? super T , ? extends R > mapping ) {
118
+ Assertions .notNull ("v" , v );
119
+ Assertions .notNull ("mapping" , mapping );
107
120
return is (value -> value .lte (v ), mapping );
108
121
}
109
122
@@ -119,12 +132,13 @@ public <R extends Expression> BranchesIntermediary<T, R> lte(final T v, final Fu
119
132
* @param <R> the type of the produced value.
120
133
*/
121
134
public <R extends Expression > BranchesIntermediary <T , R > isBoolean (final Function <? super BooleanExpression , ? extends R > mapping ) {
135
+ Assertions .notNull ("mapping" , mapping );
122
136
return is (v -> mqlEx (v ).isBoolean (), v -> mapping .apply ((BooleanExpression ) v ));
123
137
}
124
138
125
139
/**
126
140
* A successful check for
127
- * {@linkplain Expression#isBooleanOr(BooleanExpression ) being a boolean }
141
+ * {@linkplain Expression#isNumberOr(NumberExpression ) being a number }
128
142
* produces a value specified by the {@code mapping}.
129
143
*
130
144
* @mongodb.server.release 4.4
@@ -133,6 +147,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isBoolean(final Functio
133
147
* @param <R> the type of the produced value.
134
148
*/
135
149
public <R extends Expression > BranchesIntermediary <T , R > isNumber (final Function <? super NumberExpression , ? extends R > mapping ) {
150
+ Assertions .notNull ("mapping" , mapping );
136
151
return is (v -> mqlEx (v ).isNumber (), v -> mapping .apply ((NumberExpression ) v ));
137
152
}
138
153
@@ -147,6 +162,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isNumber(final Function
147
162
* @param <R> the type of the produced value.
148
163
*/
149
164
public <R extends Expression > BranchesIntermediary <T , R > isInteger (final Function <? super IntegerExpression , ? extends R > mapping ) {
165
+ Assertions .notNull ("mapping" , mapping );
150
166
return is (v -> mqlEx (v ).isInteger (), v -> mapping .apply ((IntegerExpression ) v ));
151
167
}
152
168
@@ -160,6 +176,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isInteger(final Functio
160
176
* @param <R> the type of the produced value.
161
177
*/
162
178
public <R extends Expression > BranchesIntermediary <T , R > isString (final Function <? super StringExpression , ? extends R > mapping ) {
179
+ Assertions .notNull ("mapping" , mapping );
163
180
return is (v -> mqlEx (v ).isString (), v -> mapping .apply ((StringExpression ) v ));
164
181
}
165
182
@@ -173,6 +190,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isString(final Function
173
190
* @param <R> the type of the produced value.
174
191
*/
175
192
public <R extends Expression > BranchesIntermediary <T , R > isDate (final Function <? super DateExpression , ? extends R > mapping ) {
193
+ Assertions .notNull ("mapping" , mapping );
176
194
return is (v -> mqlEx (v ).isDate (), v -> mapping .apply ((DateExpression ) v ));
177
195
}
178
196
@@ -192,33 +210,33 @@ public <R extends Expression> BranchesIntermediary<T, R> isDate(final Function<?
192
210
*/
193
211
@ SuppressWarnings ("unchecked" )
194
212
public <R extends Expression , Q extends Expression > BranchesIntermediary <T , R > isArray (final Function <? super ArrayExpression <@ MqlUnchecked (TYPE_ARGUMENT ) Q >, ? extends R > mapping ) {
213
+ Assertions .notNull ("mapping" , mapping );
195
214
return is (v -> mqlEx (v ).isArray (), v -> mapping .apply ((ArrayExpression <Q >) v ));
196
215
}
197
216
198
217
/**
199
218
* A successful check for
200
219
* {@linkplain Expression#isDocumentOr(DocumentExpression) being a document}
220
+ * (or document-like value, see
221
+ * {@link MapExpression} and {@link EntryExpression})
201
222
* produces a value specified by the {@code mapping}.
202
223
*
203
- * <p>Note: Any value considered to be a document by this API
204
- * will also be considered a map, and vice-versa.
205
- *
206
224
* @param mapping the mapping.
207
225
* @return the appended sequence of checks.
208
226
* @param <R> the type of the produced value.
209
227
*/
210
228
public <R extends Expression > BranchesIntermediary <T , R > isDocument (final Function <? super DocumentExpression , ? extends R > mapping ) {
229
+ Assertions .notNull ("mapping" , mapping );
211
230
return is (v -> mqlEx (v ).isDocumentOrMap (), v -> mapping .apply ((DocumentExpression ) v ));
212
231
}
213
232
214
233
/**
215
234
* A successful check for
216
235
* {@linkplain Expression#isMapOr(MapExpression) being a map}
236
+ * (or map-like value, see
237
+ * {@link DocumentExpression} and {@link EntryExpression})
217
238
* produces a value specified by the {@code mapping}.
218
239
*
219
- * <p>Note: Any value considered to be a map by this API
220
- * will also be considered a document, and vice-versa.
221
- *
222
240
* <p>Warning: The type argument of the map is not
223
241
* enforced by the API. The use of this method is an
224
242
* unchecked assertion that the type argument is correct.
@@ -230,6 +248,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isDocument(final Functi
230
248
*/
231
249
@ SuppressWarnings ("unchecked" )
232
250
public <R extends Expression , Q extends Expression > BranchesIntermediary <T , R > isMap (final Function <? super MapExpression <@ MqlUnchecked (TYPE_ARGUMENT ) Q >, ? extends R > mapping ) {
251
+ Assertions .notNull ("mapping" , mapping );
233
252
return is (v -> mqlEx (v ).isDocumentOrMap (), v -> mapping .apply ((MapExpression <Q >) v ));
234
253
}
235
254
@@ -243,6 +262,7 @@ public <R extends Expression, Q extends Expression> BranchesIntermediary<T, R> i
243
262
* @param <R> the type of the produced value.
244
263
*/
245
264
public <R extends Expression > BranchesIntermediary <T , R > isNull (final Function <? super Expression , ? extends R > mapping ) {
265
+ Assertions .notNull ("mapping" , mapping );
246
266
return is (v -> mqlEx (v ).isNull (), v -> mapping .apply (v ));
247
267
}
248
268
}
0 commit comments