001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.tools; 003 004import java.text.MessageFormat; 005import java.util.function.Predicate; 006import java.util.function.Supplier; 007 008/** 009 * This utility class provides a collection of static helper methods for checking 010 * parameters at run-time. 011 * @since 2711 012 */ 013public final class CheckParameterUtil { 014 015 private CheckParameterUtil() { 016 // Hide default constructor for utils classes 017 } 018 019 /** 020 * Ensures that a parameter is not null and that a certain condition holds. 021 * @param <T> parameter type 022 * @param obj parameter value 023 * @param parameterName parameter name 024 * @param conditionMsg string, stating the condition 025 * @param condition the condition to check 026 * @throws IllegalArgumentException in case the object is null or the condition 027 * is violated 028 * @since 12713 029 */ 030 public static <T> void ensure(T obj, String parameterName, String conditionMsg, Predicate<T> condition) { 031 ensureParameterNotNull(obj, parameterName); 032 if (!condition.test(obj)) 033 throw new IllegalArgumentException( 034 MessageFormat.format("Parameter value ''{0}'' of type {1} is invalid, violated condition: ''{2}'', got ''{3}''", 035 parameterName, 036 obj.getClass().getCanonicalName(), 037 conditionMsg, 038 obj)); 039 } 040 041 /** 042 * Ensures that a parameter is not null and that a certain condition holds. 043 * @param <T> parameter type 044 * @param obj parameter value 045 * @param parameterName parameter name 046 * @param condition the condition to check 047 * @throws IllegalArgumentException in case the object is null or the condition 048 * is violated 049 * @since 12713 050 */ 051 public static <T> void ensure(T obj, String parameterName, Predicate<T> condition) { 052 ensureParameterNotNull(obj, parameterName); 053 if (!condition.test(obj)) 054 throw new IllegalArgumentException( 055 MessageFormat.format("Parameter value ''{0}'' of type {1} is invalid, got ''{2}''", 056 parameterName, 057 obj.getClass().getCanonicalName(), 058 obj)); 059 } 060 061 /** 062 * Ensures a parameter is not {@code null} 063 * @param value The parameter to check 064 * @param parameterName The parameter name 065 * @throws IllegalArgumentException if the parameter is {@code null} 066 */ 067 public static void ensureParameterNotNull(Object value, String parameterName) { 068 if (value == null) 069 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' must not be null", parameterName)); 070 } 071 072 /** 073 * Ensures a parameter is not {@code null}. Can find line number in the stack trace, so parameter name is optional 074 * @param value The parameter to check 075 * @throws IllegalArgumentException if the parameter is {@code null} 076 * @since 3871 077 */ 078 public static void ensureParameterNotNull(Object value) { 079 if (value == null) 080 throw new IllegalArgumentException("Parameter must not be null"); 081 } 082 083 /** 084 * Ensures that the condition {@code condition} holds. 085 * @param condition The condition to check 086 * @param message error message 087 * @throws IllegalArgumentException if the condition does not hold 088 * @see #ensureThat(boolean, Supplier) 089 */ 090 public static void ensureThat(boolean condition, String message) { 091 if (!condition) 092 throw new IllegalArgumentException(message); 093 } 094 095 /** 096 * Ensures that the condition {@code condition} holds. 097 * 098 * This method can be used when the message is not a plain string literal, 099 * 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}