Java Utililty Methods Assert

List of utility methods to do Assert

Description

The list of methods to do Assert are organized into topic(s).

Method

voidassertArgNotEmptyString(String value, String argName)
assert Arg Not Empty String
if (value.isEmpty()) {
    throw new IllegalArgumentException(String.format("Argument [%s] cannot be an empty String", argName));
voidassertArgumentNotMinusInteger(String name, int value)
assert Argument Not Minus Integer
if (value < 0) {
    String msg = "The argument '" + name + "' should be plus or zero value: " + value;
    throw new IllegalArgumentException(msg);
voidassertArrayIndex(int arrayLength, int offset, int length)
assert Array Index
if (offset < 0 || length < 0) {
    throw new IndexOutOfBoundsException("Negative index specified");
if ((long) offset + (long) length > arrayLength) {
    throw new IndexOutOfBoundsException("Size mismatch");
voidassertArrayIndexScale(final String name, int actualIndexScale, int expectedIndexScale)
assert Array Index Scale
if (actualIndexScale != expectedIndexScale) {
    throw new IllegalStateException(
            name + " array index scale must be " + expectedIndexScale + ", but is " + actualIndexScale);
voidassertArrayType(Object array)
Assert the object is array or not
Class<?> type = array.getClass();
if (!type.isArray()) {
    String message = String.format("The argument is not an array object, its type is %s", type.getName());
    throw new IllegalArgumentException(message);
voidassertAssertionsEnabled()
assert Assertions Enabled
boolean assertOn = false;
assert assertOn = true;
if (!assertOn) {
    throw new RuntimeException("Asserts not enabled; enable assertions using the '-ea' JVM option");
voidassertAttributeNameIsLegal(final String attributeName)
Check whether the given String represents a legal attribute name according to the SAM spec, and throw an exception if it doesn't.
if (attributeName == null || attributeName.length() != 2 || !Character.isLetter(attributeName.charAt(0))
        || !Character.isLetterOrDigit(attributeName.charAt(1))) {
    throw new IllegalArgumentException("Read attribute " + attributeName
            + " invalid: attribute names must be non-null two-character Strings matching the pattern /[A-Za-z][A-Za-z0-9]/");
voidassertBounds(final long reqOff, final long reqLen, final long allocSize)
Perform bounds checking using java assert (if enabled) checking the requested offset and length against the allocated size.
assert ((reqOff | reqLen | (reqOff + reqLen) | (allocSize - (reqOff + reqLen))) >= 0) : "offset: " + reqOff
        + ", reqLength: " + reqLen + ", size: " + allocSize;
voidassertByThrowing(boolean condition, String errorMessage)
assert By Throwing
if (!condition) {
    throw new RuntimeException(errorMessage);
StringassertCharactersNotInString(final String illegalChars, final char... chars)
Checks that a String doesn't contain one or more characters of interest.
for (final char illegalChar : illegalChars.toCharArray()) {
    for (final char ch : chars) {
        if (illegalChar == ch) {
            throw new IllegalArgumentException(
                    "Supplied String contains illegal character '" + illegalChar + "'.");
return illegalChars;