Java Utililty Methods Assert

List of utility methods to do Assert

  1. HOME
  2. Java
  3. A
  4. Assert

Description

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

Method

void assertArgNotEmptyString(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));
void assertArgumentNotMinusInteger(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);
void assertArrayIndex(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");
void assertArrayIndexScale(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);
void assertArrayType(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);
void assertAssertionsEnabled()
assert Assertions Enabled
boolean assertOn = false;
assert assertOn = true;
if (!assertOn) {
 throw new RuntimeException("Asserts not enabled; enable assertions using the '-ea' JVM option");
void assertAttributeNameIsLegal(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]/");
void assertBounds(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;
void assertByThrowing(boolean condition, String errorMessage)
assert By Throwing
if (!condition) {
 throw new RuntimeException(errorMessage);
String assertCharactersNotInString(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;


AltStyle によって変換されたページ (->オリジナル) /