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

Last change on this file since 14253 was 13173, checked in by Don-vip, 6 years ago

see #15310 - remove most of deprecated APIs

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.text.MessageFormat;
5import java.util.function.Predicate;
6import java.util.function.Supplier;
7
8/**
9 * This utility class provides a collection of static helper methods for checking
10 * parameters at run-time.
11 * @since 2711
12 */
13public final class CheckParameterUtil {
14
15 private CheckParameterUtil() {
16 // Hide default constructor for utils classes
17 }
18
19 /**
20 * Ensures that a parameter is not null and that a certain condition holds.
21 * @param <T> parameter type
22 * @param obj parameter value
23 * @param parameterName parameter name
24 * @param conditionMsg string, stating the condition
25 * @param condition the condition to check
26 * @throws IllegalArgumentException in case the object is null or the condition
27 * is violated
28 * @since 12713
29 */
30 public static <T> void ensure(T obj, String parameterName, String conditionMsg, Predicate<T> condition) {
31 ensureParameterNotNull(obj, parameterName);
32 if (!condition.test(obj))
33 throw new IllegalArgumentException(
34 MessageFormat.format("Parameter value ''{0}'' of type {1} is invalid, violated condition: ''{2}'', got ''{3}''",
35 parameterName,
36 obj.getClass().getCanonicalName(),
37 conditionMsg,
38 obj));
39 }
40
41 /**
42 * Ensures that a parameter is not null and that a certain condition holds.
43 * @param <T> parameter type
44 * @param obj parameter value
45 * @param parameterName parameter name
46 * @param condition the condition to check
47 * @throws IllegalArgumentException in case the object is null or the condition
48 * is violated
49 * @since 12713
50 */
51 public static <T> void ensure(T obj, String parameterName, Predicate<T> condition) {
52 ensureParameterNotNull(obj, parameterName);
53 if (!condition.test(obj))
54 throw new IllegalArgumentException(
55 MessageFormat.format("Parameter value ''{0}'' of type {1} is invalid, got ''{2}''",
56 parameterName,
57 obj.getClass().getCanonicalName(),
58 obj));
59 }
60
61 /**
62 * Ensures a parameter is not {@code null}
63 * @param value The parameter to check
64 * @param parameterName The parameter name
65 * @throws IllegalArgumentException if the parameter is {@code null}
66 */
67 public static void ensureParameterNotNull(Object value, String parameterName) {
68 if (value == null)
69 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName));
70 }
71
72 /**
73 * Ensures a parameter is not {@code null}. Can find line number in the stack trace, so parameter name is optional
74 * @param value The parameter to check
75 * @throws IllegalArgumentException if the parameter is {@code null}
76 * @since 3871
77 */
78 public static void ensureParameterNotNull(Object value) {
79 if (value == null)
80 throw new IllegalArgumentException("Parameter must not be null");
81 }
82
83 /**
84 * Ensures that the condition {@code condition} holds.
85 * @param condition The condition to check
86 * @param message error message
87 * @throws IllegalArgumentException if the condition does not hold
88 * @see #ensureThat(boolean, Supplier)
89 */
90 public static void ensureThat(boolean condition, String message) {
91 if (!condition)
92 throw new IllegalArgumentException(message);
93 }
94
95 /**
96 * Ensures that the condition {@code condition} holds.
97 *
98 * This method can be used when the message is not a plain string literal,
99 * but somehow constructed. Using a {@link Supplier} improves the performance,
100 * as the string construction is skipped when the condition holds.
101 * @param condition The condition to check
102 * @param messageSupplier supplier of the error message
103 * @throws IllegalArgumentException if the condition does not hold
104 * @since 12822
105 */
106 public static void ensureThat(boolean condition, Supplier<String> messageSupplier) {
107 if (!condition)
108 throw new IllegalArgumentException(messageSupplier.get());
109 }
110}
Note: See TracBrowser for help on using the repository browser.