source: josm/trunk/src/org/openstreetmap/josm/tools/CheckParameterUtil.java@ 17335

Last change on this file since 17335 was 16056, checked in by simon04, 4 years ago

CheckParameterUtil: replace ensure() with ensureThat()

Makes code easier to understand; gets rid of one level of indirection.

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.text.MessageFormat;
5import java.util.function.Supplier;
6
7/**
8 * This utility class provides a collection of static helper methods for checking
9 * parameters at run-time.
10 * @since 2711
11 */
12public final class CheckParameterUtil {
13
14 private CheckParameterUtil() {
15 // Hide default constructor for utils classes
16 }
17
18 /**
19 * Ensures a parameter is not {@code null}
20 * @param value The parameter to check
21 * @param parameterName The parameter name
22 * @throws IllegalArgumentException if the parameter is {@code null}
23 */
24 public static void ensureParameterNotNull(Object value, String parameterName) {
25 if (value == null)
26 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
27 }
28
29 /**
30 * Ensures a parameter is not {@code null}. Can find line number in the stack trace, so parameter name is optional
31 * @param value The parameter to check
32 * @throws IllegalArgumentException if the parameter is {@code null}
33 * @since 3871
34 */
35 public static void ensureParameterNotNull(Object value) {
36 if (value == null)
37 throw new IllegalArgumentException("Parameter must not be null");
38 }
39
40 /**
41 * Ensures that the condition {@code condition} holds.
42 * @param condition The condition to check
43 * @param message error message
44 * @throws IllegalArgumentException if the condition does not hold
45 * @see #ensureThat(boolean, Supplier)
46 */
47 public static void ensureThat(boolean condition, String message) {
48 if (!condition)
49 throw new IllegalArgumentException(message);
50 }
51
52 /**
53 * Ensures that the condition {@code condition} holds.
54 *
55 * This method can be used when the message is not a plain string literal,
56 * but somehow constructed. Using a {@link Supplier} improves the performance,
57 * as the string construction is skipped when the condition holds.
58 * @param condition The condition to check
59 * @param messageSupplier supplier of the error message
60 * @throws IllegalArgumentException if the condition does not hold
61 * @since 12822
62 */
63 public static void ensureThat(boolean condition, Supplier<String> messageSupplier) {
64 if (!condition)
65 throw new IllegalArgumentException(messageSupplier.get());
66 }
67}
Note: See TracBrowser for help on using the repository browser.