185 * Checks that the specified object reference is not {@code null}. This |
185 * Checks that the specified object reference is not {@code null}. This |
186 * method is designed primarily for doing parameter validation in methods |
186 * method is designed primarily for doing parameter validation in methods |
187 * and constructors, as demonstrated below: |
187 * and constructors, as demonstrated below: |
188 * <blockquote><pre> |
188 * <blockquote><pre> |
189 * public Foo(Bar bar) { |
189 * public Foo(Bar bar) { |
190 * this.bar = Objects.nonNull(bar); |
190 * this.bar = Objects.requireNonNull(bar); |
191 * } |
191 * } |
192 * </pre></blockquote> |
192 * </pre></blockquote> |
193 * |
193 * |
194 * @param obj the object reference to check for nullity |
194 * @param obj the object reference to check for nullity |
195 * @param <T> the type of the reference |
195 * @param <T> the type of the reference |
196 * @return {@code obj} if not {@code null} |
196 * @return {@code obj} if not {@code null} |
197 * @throws NullPointerException if {@code obj} is {@code null} |
197 * @throws NullPointerException if {@code obj} is {@code null} |
198 */ |
198 */ |
199 public static <T> T nonNull(T obj) { |
199 public static <T> T requireNonNull(T obj) { |
200 if (obj == null) |
200 if (obj == null) |
201 throw new NullPointerException(); |
201 throw new NullPointerException(); |
202 return obj; |
202 return obj; |
203 } |
203 } |
204 |
204 |
207 * throws a customized {@link NullPointerException} if it is. This method |
207 * throws a customized {@link NullPointerException} if it is. This method |
208 * is designed primarily for doing parameter validation in methods and |
208 * is designed primarily for doing parameter validation in methods and |
209 * constructors with multiple parameters, as demonstrated below: |
209 * constructors with multiple parameters, as demonstrated below: |
210 * <blockquote><pre> |
210 * <blockquote><pre> |
211 * public Foo(Bar bar, Baz baz) { |
211 * public Foo(Bar bar, Baz baz) { |
212 * this.bar = Objects.nonNull(bar, "bar must not be null"); |
212 * this.bar = Objects.requireNonNull(bar, "bar must not be null"); |
213 * this.baz = Objects.nonNull(baz, "baz must not be null"); |
213 * this.baz = Objects.requireNonNull(baz, "baz must not be null"); |
214 * } |
214 * } |
215 * </pre></blockquote> |
215 * </pre></blockquote> |
216 * |
216 * |
217 * @param obj the object reference to check for nullity |
217 * @param obj the object reference to check for nullity |
218 * @param message detail message to be used in the event that a {@code |
218 * @param message detail message to be used in the event that a {@code |
219 * NullPointerException} is thrown |
219 * NullPointerException} is thrown |
220 * @param <T> the type of the reference |
220 * @param <T> the type of the reference |
221 * @return {@code obj} if not {@code null} |
221 * @return {@code obj} if not {@code null} |
222 * @throws NullPointerException if {@code obj} is {@code null} |
222 * @throws NullPointerException if {@code obj} is {@code null} |
223 */ |
223 */ |
224 public static <T> T nonNull(T obj, String message) { |
224 public static <T> T requireNonNull(T obj, String message) { |
225 if (obj == null) |
225 if (obj == null) |
226 throw new NullPointerException(message); |
226 throw new NullPointerException(message); |
227 return obj; |
227 return obj; |
228 } |
228 } |
229 } |
229 } |