1616
1717package com .mongodb .client .model .expressions ;
1818
19+ import com .mongodb .annotations .Beta ;
20+ import com .mongodb .assertions .Assertions ;
21+
1922import java .util .ArrayList ;
2023import java .util .List ;
2124import java .util .function .Function ;
2831 * to succeed will produce the value that it specifies. If no check succeeds,
2932 * then the operation
3033 * {@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.
3235 *
3336 * @param <T> the type of the values that may be checked.
37+ * @since 4.9.0
3438 */
39+ @ Beta (Beta .Reason .CLIENT )
3540public final class Branches <T extends Expression > {
3641
3742 Branches () {
@@ -59,6 +64,8 @@ private static <T extends Expression> MqlExpression<?> mqlEx(final T value) {
5964 * @return the appended sequence of checks.
6065 */
6166 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 );
6269 return with (value -> new SwitchCase <>(predicate .apply (value ), mapping .apply (value )));
6370 }
6471
@@ -74,6 +81,8 @@ public <R extends Expression> BranchesIntermediary<T, R> is(final Function<? sup
7481 * @return the appended sequence of checks.
7582 */
7683 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 );
7786 return is (value -> value .eq (v ), mapping );
7887 }
7988
@@ -89,6 +98,8 @@ public <R extends Expression> BranchesIntermediary<T, R> eq(final T v, final Fun
8998 * @return the appended sequence of checks.
9099 */
91100 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 );
92103 return is (value -> value .lt (v ), mapping );
93104 }
94105
@@ -104,6 +115,8 @@ public <R extends Expression> BranchesIntermediary<T, R> lt(final T v, final Fun
104115 * @return the appended sequence of checks.
105116 */
106117 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 );
107120 return is (value -> value .lte (v ), mapping );
108121 }
109122
@@ -119,12 +132,13 @@ public <R extends Expression> BranchesIntermediary<T, R> lte(final T v, final Fu
119132 * @param <R> the type of the produced value.
120133 */
121134 public <R extends Expression > BranchesIntermediary <T , R > isBoolean (final Function <? super BooleanExpression , ? extends R > mapping ) {
135+ Assertions .notNull ("mapping" , mapping );
122136 return is (v -> mqlEx (v ).isBoolean (), v -> mapping .apply ((BooleanExpression ) v ));
123137 }
124138
125139 /**
126140 * A successful check for
127- * {@linkplain Expression#isBooleanOr(BooleanExpression ) being a boolean }
141+ * {@linkplain Expression#isNumberOr(NumberExpression ) being a number }
128142 * produces a value specified by the {@code mapping}.
129143 *
130144 * @mongodb.server.release 4.4
@@ -133,6 +147,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isBoolean(final Functio
133147 * @param <R> the type of the produced value.
134148 */
135149 public <R extends Expression > BranchesIntermediary <T , R > isNumber (final Function <? super NumberExpression , ? extends R > mapping ) {
150+ Assertions .notNull ("mapping" , mapping );
136151 return is (v -> mqlEx (v ).isNumber (), v -> mapping .apply ((NumberExpression ) v ));
137152 }
138153
@@ -147,6 +162,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isNumber(final Function
147162 * @param <R> the type of the produced value.
148163 */
149164 public <R extends Expression > BranchesIntermediary <T , R > isInteger (final Function <? super IntegerExpression , ? extends R > mapping ) {
165+ Assertions .notNull ("mapping" , mapping );
150166 return is (v -> mqlEx (v ).isInteger (), v -> mapping .apply ((IntegerExpression ) v ));
151167 }
152168
@@ -160,6 +176,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isInteger(final Functio
160176 * @param <R> the type of the produced value.
161177 */
162178 public <R extends Expression > BranchesIntermediary <T , R > isString (final Function <? super StringExpression , ? extends R > mapping ) {
179+ Assertions .notNull ("mapping" , mapping );
163180 return is (v -> mqlEx (v ).isString (), v -> mapping .apply ((StringExpression ) v ));
164181 }
165182
@@ -173,6 +190,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isString(final Function
173190 * @param <R> the type of the produced value.
174191 */
175192 public <R extends Expression > BranchesIntermediary <T , R > isDate (final Function <? super DateExpression , ? extends R > mapping ) {
193+ Assertions .notNull ("mapping" , mapping );
176194 return is (v -> mqlEx (v ).isDate (), v -> mapping .apply ((DateExpression ) v ));
177195 }
178196
@@ -192,33 +210,33 @@ public <R extends Expression> BranchesIntermediary<T, R> isDate(final Function<?
192210 */
193211 @ SuppressWarnings ("unchecked" )
194212 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 );
195214 return is (v -> mqlEx (v ).isArray (), v -> mapping .apply ((ArrayExpression <Q >) v ));
196215 }
197216
198217 /**
199218 * A successful check for
200219 * {@linkplain Expression#isDocumentOr(DocumentExpression) being a document}
220+ * (or document-like value, see
221+ * {@link MapExpression} and {@link EntryExpression})
201222 * produces a value specified by the {@code mapping}.
202223 *
203- * <p>Note: Any value considered to be a document by this API
204- * will also be considered a map, and vice-versa.
205- *
206224 * @param mapping the mapping.
207225 * @return the appended sequence of checks.
208226 * @param <R> the type of the produced value.
209227 */
210228 public <R extends Expression > BranchesIntermediary <T , R > isDocument (final Function <? super DocumentExpression , ? extends R > mapping ) {
229+ Assertions .notNull ("mapping" , mapping );
211230 return is (v -> mqlEx (v ).isDocumentOrMap (), v -> mapping .apply ((DocumentExpression ) v ));
212231 }
213232
214233 /**
215234 * A successful check for
216235 * {@linkplain Expression#isMapOr(MapExpression) being a map}
236+ * (or map-like value, see
237+ * {@link DocumentExpression} and {@link EntryExpression})
217238 * produces a value specified by the {@code mapping}.
218239 *
219- * <p>Note: Any value considered to be a map by this API
220- * will also be considered a document, and vice-versa.
221- *
222240 * <p>Warning: The type argument of the map is not
223241 * enforced by the API. The use of this method is an
224242 * unchecked assertion that the type argument is correct.
@@ -230,6 +248,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isDocument(final Functi
230248 */
231249 @ SuppressWarnings ("unchecked" )
232250 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 );
233252 return is (v -> mqlEx (v ).isDocumentOrMap (), v -> mapping .apply ((MapExpression <Q >) v ));
234253 }
235254
@@ -243,6 +262,7 @@ public <R extends Expression, Q extends Expression> BranchesIntermediary<T, R> i
243262 * @param <R> the type of the produced value.
244263 */
245264 public <R extends Expression > BranchesIntermediary <T , R > isNull (final Function <? super Expression , ? extends R > mapping ) {
265+ Assertions .notNull ("mapping" , mapping );
246266 return is (v -> mqlEx (v ).isNull (), v -> mapping .apply (v ));
247267 }
248268}
0 commit comments