diff --git a/constraint/src/main/java/io/smallrye/common/constraint/Assert.java b/constraint/src/main/java/io/smallrye/common/constraint/Assert.java index 0abb2b80..17a1c152 100644 --- a/constraint/src/main/java/io/smallrye/common/constraint/Assert.java +++ b/constraint/src/main/java/io/smallrye/common/constraint/Assert.java @@ -285,6 +285,20 @@ public static void checkMinimumParameter(String name, int min, int actual) throw throw Messages.log.paramLessThan(name, min); } + /** + * Check that the named parameter is greater than or equal to {@code min} using unsigned comparison. + * + * @param name the parameter name + * @param min the minimum value + * @param actual the actual parameter value + * @throws IllegalArgumentException if the actual value is less than the minimum value + */ + public static void checkMinimumParameterUnsigned(String name, int min, int actual) throws IllegalArgumentException { + checkNotNullParamChecked("name", name); + if (Integer.compareUnsigned(actual, min) < 0) + throw Messages.log.paramLessThan(name, Integer.toUnsignedLong(min)); + } + /** * Check that the named parameter is greater than or equal to {@code min}. * @@ -299,6 +313,20 @@ public static void checkMinimumParameter(String name, long min, long actual) thr throw Messages.log.paramLessThan(name, min); } + /** + * Check that the named parameter is greater than or equal to {@code min} using unsigned comparison. + * + * @param name the parameter name + * @param min the minimum value + * @param actual the actual parameter value + * @throws IllegalArgumentException if the actual value is less than the minimum value + */ + public static void checkMinimumParameterUnsigned(String name, long min, long actual) throws IllegalArgumentException { + checkNotNullParamChecked("name", name); + if (Long.compareUnsigned(actual, min) < 0) + throw Messages.log.paramLessThan(name, Long.toUnsignedString(min)); + } + /** * Check that the named parameter is greater than or equal to {@code min}. * @@ -357,6 +385,20 @@ public static void checkMaximumParameter(String name, int max, int actual) throw throw Messages.log.paramGreaterThan(name, max); } + /** + * Check that the named parameter is less than or equal to {@code max} using unsigned comparison. + * + * @param name the parameter name + * @param max the maximum value + * @param actual the actual parameter value + * @throws IllegalArgumentException if the actual value is greater than the maximum value + */ + public static void checkMaximumParameterUnsigned(String name, int max, int actual) throws IllegalArgumentException { + checkNotNullParamChecked("name", name); + if (Integer.compareUnsigned(actual, max) > 0) + throw Messages.log.paramGreaterThan(name, Integer.toUnsignedLong(max)); + } + /** * Check that the named parameter is less than or equal to {@code max}. * @@ -371,6 +413,20 @@ public static void checkMaximumParameter(String name, long max, long actual) thr throw Messages.log.paramGreaterThan(name, max); } + /** + * Check that the named parameter is less than or equal to {@code max} using unsigned comparison. + * + * @param name the parameter name + * @param max the maximum value + * @param actual the actual parameter value + * @throws IllegalArgumentException if the actual value is greater than the maximum value + */ + public static void checkMaximumParameterUnsigned(String name, long max, long actual) throws IllegalArgumentException { + checkNotNullParamChecked("name", name); + if (Long.compareUnsigned(actual, max) > 0) + throw Messages.log.paramGreaterThan(name, Long.toUnsignedString(max)); + } + /** * Check that the named parameter is less than or equal to {@code max}. * diff --git a/constraint/src/main/java/io/smallrye/common/constraint/Messages.java b/constraint/src/main/java/io/smallrye/common/constraint/Messages.java index 76fa875e..b5adeda3 100644 --- a/constraint/src/main/java/io/smallrye/common/constraint/Messages.java +++ b/constraint/src/main/java/io/smallrye/common/constraint/Messages.java @@ -10,14 +10,14 @@ interface Messages { @Message(id = 0, value = "Parameter '%s' may not be null") IllegalArgumentException nullParam(String paramName); - @Message(id = 1, value = "Parameter '%s' may not be less than %d") + @Message(id = 1, value = "Parameter '%s' may not be less than %s") IllegalArgumentException paramLessThan(String name, long min); IllegalArgumentException paramLessThan(String name, double min); IllegalArgumentException paramLessThan(String name, Object min); - @Message(id = 2, value = "Parameter '%s' may not be greater than than %d") + @Message(id = 2, value = "Parameter '%s' may not be greater than than %s") IllegalArgumentException paramGreaterThan(String name, long max); IllegalArgumentException paramGreaterThan(String name, double max);