Skip to content

Commit

Permalink
Support unsigned parameter range checks (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlloyd authored Apr 14, 2023
1 parent e51c425 commit 77723e0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
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

0 comments on commit 77723e0

Please sign in to comment.