11package org .baeldung .common .web ;
22
3+ import org .baeldung .common .web .exception .MyBadRequestException ;
34import org .baeldung .common .web .exception .MyConflictException ;
45import org .baeldung .common .web .exception .MyForbiddenException ;
56import org .baeldung .common .web .exception .MyResourceNotFoundException ;
67import org .springframework .http .HttpStatus ;
78
89/**
9- * Simple static methods to be called at the start of your own methods to verify correct arguments and state. If the Precondition fails, an {@link HttpStatus} code is thrown
10+ * Simple static methods to be called at the start of your own methods to verify
11+ * correct arguments and state. If the Precondition fails, an {@link HttpStatus}
12+ * code is thrown
1013 */
1114public final class RestPreconditions {
1215
@@ -18,89 +21,223 @@ private RestPreconditions() {
1821
1922 /**
2023 * Ensures that an object reference passed as a parameter to the calling method is not null.
21- *
24+ *
2225 * @param reference
2326 * an object reference
27+ *
2428 * @return the non-null reference that was validated
29+ *
2530 * @throws MyResourceNotFoundException
2631 * if {@code reference} is null
2732 */
2833 public static <T > T checkNotNull (final T reference ) {
34+ return checkNotNull (reference , null );
35+ }
36+
37+ /**
38+ * Ensures that an object reference passed as a parameter to the calling method is not null.
39+ *
40+ * @param reference
41+ * an object reference
42+ * @param message
43+ * the message of the exception if the check fails
44+ *
45+ * @return the non-null reference that was validated
46+ *
47+ * @throws MyResourceNotFoundException
48+ * if {@code reference} is null
49+ */
50+ public static <T > T checkNotNull (final T reference , final String message ) {
2951 if (reference == null ) {
30- throw new MyResourceNotFoundException ();
52+ throw new MyResourceNotFoundException (message );
3153 }
3254 return reference ;
3355 }
3456
3557 /**
36- * Ensures that an object reference passed as a parameter to the calling method is not null.
37- *
58+ * Ensures that an object reference passed as a parameter to the calling
59+ * method is not null.
60+ *
3861 * @param reference
3962 * an object reference
4063 * @return the non-null reference that was validated
41- * @throws MyConflictException
64+ *
65+ * @throws MyBadRequestException
4266 * if {@code reference} is null
4367 */
4468 public static <T > T checkRequestElementNotNull (final T reference ) {
69+ return checkRequestElementNotNull (reference , null );
70+ }
71+
72+ /**
73+ * Ensures that an object reference passed as a parameter to the calling method is not null.
74+ *
75+ * @param reference
76+ * an object reference
77+ * @param message
78+ * the message of the exception if the check fails
79+ *
80+ * @return the non-null reference that was validated
81+ *
82+ * @throws MyBadRequestException
83+ * if {@code reference} is null
84+ */
85+ public static <T > T checkRequestElementNotNull (final T reference , final String message ) {
4586 if (reference == null ) {
46- throw new MyConflictException ( );
87+ throw new MyBadRequestException ( message );
4788 }
4889 return reference ;
4990 }
5091
5192 /**
5293 * Ensures the truth of an expression
53- *
94+ *
5495 * @param expression
5596 * a boolean expression
97+ *
98+ * @throws MyConflictException
99+ * if {@code expression} is false
56100 */
57101 public static void checkRequestState (final boolean expression ) {
102+ checkRequestState (expression , null );
103+ }
104+
105+ /**
106+ * Ensures the truth of an expression
107+ *
108+ * @param expression
109+ * a boolean expression
110+ * @param message
111+ * the message of the exception if the check fails
112+ *
113+ * @throws MyConflictException
114+ * if {@code expression} is false
115+ */
116+ public static void checkRequestState (final boolean expression , final String message ) {
58117 if (!expression ) {
59- throw new MyConflictException ();
118+ throw new MyConflictException (message );
119+ }
120+ }
121+
122+ /**
123+ * Ensures the truth of an expression related to the validity of the request
124+ *
125+ * @param expression
126+ * a boolean expression
127+ *
128+ * @throws MyBadRequestException
129+ * if {@code expression} is false
130+ */
131+ public static void checkIfBadRequest (final boolean expression ) {
132+ checkIfBadRequest (expression , null );
133+ }
134+
135+ /**
136+ * Ensures the truth of an expression related to the validity of the request
137+ *
138+ * @param expression
139+ * a boolean expression
140+ * @param message
141+ * the message of the exception if the check fails
142+ *
143+ * @throws MyBadRequestException
144+ * if {@code expression} is false
145+ */
146+ public static void checkIfBadRequest (final boolean expression , final String message ) {
147+ if (!expression ) {
148+ throw new MyBadRequestException (message );
60149 }
61150 }
62151
63152 /**
64153 * Check if some value was found, otherwise throw exception.
65- *
154+ *
66155 * @param expression
67156 * has value true if found, otherwise false
157+ *
68158 * @throws MyResourceNotFoundException
69159 * if expression is false, means value not found.
70160 */
71161 public static void checkFound (final boolean expression ) {
162+ checkFound (expression , null );
163+ }
164+
165+ /**
166+ * Check if some value was found, otherwise throw exception.
167+ *
168+ * @param expression
169+ * has value true if found, otherwise false
170+ * @param message
171+ * the message of the exception if the check fails
172+ *
173+ * @throws MyResourceNotFoundException
174+ * if expression is false, means value not found.
175+ */
176+ public static void checkFound (final boolean expression , final String message ) {
72177 if (!expression ) {
73- throw new MyResourceNotFoundException ();
178+ throw new MyResourceNotFoundException (message );
74179 }
75180 }
76181
77182 /**
78183 * Check if some value was found, otherwise throw exception.
79- *
184+ *
80185 * @param expression
81186 * has value true if found, otherwise false
187+ *
82188 * @throws MyResourceNotFoundException
83189 * if expression is false, means value not found.
84190 */
85191 public static <T > T checkFound (final T resource ) {
192+ return checkFound (resource , null );
193+ }
194+
195+ /**
196+ * Check if some value was found, otherwise throw exception.
197+ *
198+ * @param expression
199+ * has value true if found, otherwise false
200+ * @param message
201+ * the message of the exception if the check fails
202+ *
203+ * @throws MyResourceNotFoundException
204+ * if expression is false, means value not found.
205+ */
206+ public static <T > T checkFound (final T resource , final String message ) {
86207 if (resource == null ) {
87- throw new MyResourceNotFoundException ();
208+ throw new MyResourceNotFoundException (message );
88209 }
89210
90211 return resource ;
91212 }
92213
93214 /**
94215 * Check if some value was found, otherwise throw exception.
95- *
216+ *
96217 * @param expression
97218 * has value true if found, otherwise false
219+ *
98220 * @throws MyForbiddenException
99221 * if expression is false, means operation not allowed.
100222 */
101223 public static void checkAllowed (final boolean expression ) {
224+ checkAllowed (expression , null );
225+ }
226+
227+ /**
228+ * Check if some value was found, otherwise throw exception.
229+ *
230+ * @param expression
231+ * has value true if found, otherwise false
232+ * @param message
233+ * the message of the exception if the check fails
234+ *
235+ * @throws MyForbiddenException
236+ * if expression is false, means operation not allowed.
237+ */
238+ public static void checkAllowed (final boolean expression , final String message ) {
102239 if (!expression ) {
103- throw new MyForbiddenException ();
240+ throw new MyForbiddenException (message );
104241 }
105242 }
106243
0 commit comments