Skip to content

Commit

Permalink
Merge pull request #244 from dmlloyd/pow2
Browse files Browse the repository at this point in the history
Introduce power-of-two validation methods
  • Loading branch information
dmlloyd authored Sep 11, 2023
2 parents 4b3492e + dc0c898 commit 77ea531
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
28 changes: 28 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 @@ -455,6 +455,34 @@ public static void checkMaximumParameter(String name, double max, double actual)
throw Messages.log.paramGreaterThan(name, max);
}

/**
* Check that the named parameter is zero or a power of two (unsigned).
* For a signed check, or to eliminate zero values, use {@link #checkMinimumParameter(String, int, int)}.
*
* @param name the parameter name
* @param actual the actual parameter value
* @throws IllegalArgumentException if the actual value is not zero or a power of two
*/
public static void checkPow2Parameter(String name, int actual) throws IllegalArgumentException {
checkNotNullParamChecked("name", name);
if (actual != 0 && Integer.bitCount(actual) != 1)
throw Messages.log.paramNotPow2(name);
}

/**
* Check that the named parameter is zero or a power of two (unsigned).
* For a signed check, or to eliminate zero values, use {@link #checkMinimumParameter(String, long, long)}.
*
* @param name the parameter name
* @param actual the actual parameter value
* @throws IllegalArgumentException if the actual value is not zero or a power of two
*/
public static void checkPow2Parameter(String name, long actual) throws IllegalArgumentException {
checkNotNullParamChecked("name", name);
if (actual != 0 && Long.bitCount(actual) != 1)
throw Messages.log.paramNotPow2(name);
}

/**
* Check that the given offset and length fall completely within the bounds of the given array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ interface Messages {

@Message(id = 13, value = "Method \"%s\" of class \"%s\" is not supported")
UnsupportedOperationException unsupported(String methodName, String className);

@Message(id = 14, value = "Parameter '%s' must be a power of two")
IllegalArgumentException paramNotPow2(String name);
}

0 comments on commit 77ea531

Please sign in to comment.