Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support unsigned parameter range checks #216

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions constraint/src/main/java/io/smallrye/common/constraint/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
*
Expand All @@ -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}.
*
Expand Down Expand Up @@ -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}.
*
Expand All @@ -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}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down