code
stringlengths
67
466k
docstring
stringlengths
1
13.2k
private static <T> Number checkNumberInRange(final String value, final Class<T> type) { final Number ret; if (type.equals(Byte.class)) { final Number number = new BigInteger(value); NumberInRange.checkByte(number); ret = Byte.valueOf(number.byteValue()); } else if (type.equals(Double.class)) { final Number number = new BigDecimal(value); NumberInRange.checkDouble(number); ret = Double.valueOf(number.doubleValue()); } else if (type.equals(Float.class)) { final Number number = new BigDecimal(value); NumberInRange.checkFloat(number); ret = Float.valueOf(number.floatValue()); } else if (type.equals(Integer.class)) { final Number number = new BigInteger(value); NumberInRange.checkInteger(number); ret = Integer.valueOf(number.intValue()); } else if (type.equals(Long.class)) { final Number number = new BigInteger(value); NumberInRange.checkLong(number); ret = Long.valueOf(number.longValue()); } else if (type.equals(Short.class)) { final Number number = new BigInteger(value); NumberInRange.checkShort(number); ret = Short.valueOf(number.shortValue()); } else if (type.equals(BigInteger.class)) { ret = new BigInteger(value); } else if (type.equals(BigDecimal.class)) { ret = new BigDecimal(value); } else { throw new IllegalNumberArgumentException("Return value is no known subclass of 'java.lang.Number': " + type.getName()); } return ret; }
Checks the passed {@code value} against the ranges of the given datatype. @param value value which must be a number and in the range of the given datatype. @param type requested return value type, must be a subclass of {@code Number} @return a number @throws NumberFormatException if the given value can not be parsed as a number
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNotContainedArgumentException.class }) public static <T extends Object> T contains(@Nonnull final Collection<T> haystack, @Nonnull final T needle) { Check.notNull(haystack, "haystack"); Check.notNull(needle, "needle"); if (!haystack.contains(needle)) { throw new IllegalNotContainedArgumentException(needle); } return needle; }
Ensures that an element {@code needle} is contained in a collection {@code haystack}. <p> This is in particular useful if you want to check whether an enum value is contained in an {@code EnumSet}. The check is implemented using {@link java.util.Collection#contains(Object)}. <p> We recommend to use the overloaded method {@link Check#contains(Collection, Object, String)} and pass as second argument the name of the parameter to enhance the exception message. @param haystack A collection which must contain {@code needle} @param needle An object that must be contained into a collection. @return the passed argument {@code needle} @throws IllegalNotContainedArgumentException if the passed {@code needle} can not be found in {@code haystack}
private static boolean containsNullElements(@Nonnull final Object[] array) { boolean containsNull = false; for (final Object o : array) { if (o == null) { containsNull = true; break; } } return containsNull; }
Checks if the given array contains {@code null}. @param array reference to an array @return {@code true} if the array contains {@code null}, otherwise {@code false}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNotGreaterOrEqualThanException.class }) public static <T extends Comparable<T>> T greaterOrEqualThan(@Nonnull final T expected, @Nonnull final T check, @Nonnull final String message) { Check.notNull(expected, "expected"); Check.notNull(check, "check"); if (expected.compareTo(check) > 0) { throw new IllegalNotGreaterOrEqualThanException(message, check); } return check; }
Ensures that a passed {@code Comparable} is greater or equal compared to another {@code Comparable}. The comparison is made using {@code expected.compareTo(check) > 0}. @param expected Expected value @param check Comparable to be checked @param message an error message describing why the comparable must be greater than a value (will be passed to {@code IllegalNotGreaterOrEqualThanException}) @return the passed {@code Comparable} argument {@code check} @throws IllegalNotGreaterOrEqualThanException if the argument value {@code check} is not greater or equal than the value {@code expected} when using method {@code compareTo}
@ArgumentsChecked @Throws(IllegalNotGreaterThanException.class) public static byte greaterThan(final byte expected, final byte check) { if (expected >= check) { throw new IllegalNotGreaterThanException(check); } return check; }
Ensures that a passed {@code byte} is greater to another {@code byte}. @param expected Expected value @param check Comparable to be checked @return the passed {@code Comparable} argument {@code check} @throws IllegalNotGreaterThanException if the argument value {@code check} is not greater than value {@code expected}
@ArgumentsChecked @Throws(IllegalNotGreaterThanException.class) public static byte greaterThan(final byte expected, final byte check, @Nonnull final String message) { if (expected >= check) { throw new IllegalNotGreaterThanException(message, check); } return check; }
Ensures that a passed {@code byte} is greater than another {@code byte}. @param expected Expected value @param check Comparable to be checked @param message an error message describing why the comparable must be greater than a value (will be passed to {@code IllegalNotGreaterThanException}) @return the passed {@code Comparable} argument {@code check} @throws IllegalNotGreaterThanException if the argument value {@code check} is not greater than value {@code expected}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNotGreaterThanException.class }) public static <T extends Comparable<T>> T greaterThan(@Nonnull final T expected, @Nonnull final T check, @Nonnull final String message) { Check.notNull(expected, "expected"); Check.notNull(check, "check"); if (expected.compareTo(check) >= 0) { throw new IllegalNotGreaterThanException(message, check); } return check; }
Ensures that a passed {@code Comparable} is greater than another {@code Comparable}. The comparison is made using {@code expected.compareTo(check) >= 0}. @param expected Expected value @param check Comparable to be checked @param message an error message describing why the comparable must be greater than a value (will be passed to {@code IllegalNotGreaterThanException}) @return the passed {@code Comparable} argument {@code check} @throws IllegalNotGreaterThanException if the argument value {@code check} is not greater than value {@code expected} when using method {@code compareTo}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalMissingAnnotationException.class }) public static Annotation hasAnnotation(@Nonnull final Class<?> clazz, @Nonnull final Class<? extends Annotation> annotation) { Check.notNull(clazz, "clazz"); Check.notNull(annotation, "annotation"); if (!clazz.isAnnotationPresent(annotation)) { throw new IllegalMissingAnnotationException(annotation, clazz); } return clazz.getAnnotation(annotation); }
Ensures that a passed class has an annotation of a specific type @param clazz the class that must have a required annotation @param annotation the type of annotation that is required on the class @return the given annotation which is present on the checked class @throws IllegalMissingAnnotationException if the passed annotation is not annotated at the given class
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalInstanceOfArgumentException.class }) @SuppressWarnings("unchecked") public static <T> T instanceOf(@Nonnull final Class<?> type, @Nonnull final Object obj) { return (T) instanceOf(type, obj, EMPTY_ARGUMENT_NAME); }
Ensures that a passed argument is a member of a specific type. @param type class that the given object is a member of @param obj the object reference that should be a member of a specific {@code type} @return the given object cast to type @throws IllegalInstanceOfArgumentException if the given argument {@code obj} is not a member of {@code type}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalInstanceOfArgumentException.class }) @SuppressWarnings("unchecked") public static <T> T instanceOf(@Nonnull final Class<?> type, @Nonnull final Object obj, @Nullable final String name) { Check.notNull(type, "type"); Check.notNull(obj, "obj"); if (!type.isInstance(obj)) { throw new IllegalInstanceOfArgumentException(name, type, obj.getClass()); } return (T) obj; }
Ensures that a passed argument is a member of a specific type. @param type class that the given object is a member of @param obj the object reference that should be a member of a specific {@code type} @param name name of object reference (in source code) @return the given object cast to type @throws IllegalInstanceOfArgumentException if the given argument {@code obj} is not a member of {@code type}
@Throws(IllegalNotNullArgumentException.class) public static void isNull(@Nullable final Object reference) { if (reference != null) { throw new IllegalNotNullArgumentException(reference); } }
Ensures that a given argument is {@code null}. Normally, the usage of {@code null} arguments is disregarded by the authors of quality-check. Still, there are certain circumstances where null is required, e.g. the primary key of an entity before it is written to the database for the first time. In such cases it is ok to use null values and there should also be checks for them. For example, to avoid overwriting an existing primary key with a new one. @param reference reference which must be null @throws IllegalNotNullArgumentException if the given argument {@code reference} is not null
@Throws(IllegalNotNullArgumentException.class) public static void isNull(@Nullable final Object reference, @Nullable final String name) { if (reference != null) { throw new IllegalNotNullArgumentException(name, reference); } }
Ensures that a given argument is {@code null}. Normally, the usage of {@code null} arguments is disregarded by the authors of quality-check. Still, there are certain circumstances where null is required, e.g. the primary key of an entity before it is written to the database for the first time. In such cases it is ok to use null values and there should also be checks for them. For example, to avoid overwriting an existing primary key with a new one. @param reference reference which must be null. @param name name of object reference (in source code) @throws IllegalNotNullArgumentException if the given argument {@code reference} is not null
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNumberArgumentException.class }) public static int isNumber(@Nonnull final String value, @Nullable final String name) { Check.notNull(value, "value"); return Check.isNumber(value, name, Integer.class).intValue(); }
Ensures that a string argument is a number according to {@code Integer.parseInt} @param value value which must be a number @param name name of object reference (in source code) @return the given string argument converted to an int @throws IllegalNumberArgumentException if the given argument {@code value} is no number
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNumberArgumentException.class }) public static <T extends Number> T isNumber(@Nonnull final String value, @Nullable final String name, @Nonnull final Class<T> type) { Check.notNull(value, "value"); Check.notNull(type, "type"); final Number ret; try { ret = checkNumberInRange(value, type); } catch (final NumberFormatException nfe) { if (name == null) { throw new IllegalNumberArgumentException(value, nfe); } else { throw new IllegalNumberArgumentException(name, value, nfe); } } return type.cast(ret); }
Ensures that a String argument is a number. This overload supports all subclasses of {@code Number}. The number is first converted to a {@code BigDecimal} or {@code BigInteger}. Floating point types are only supported if the {@code type} is one of {@code Float, Double, BigDecimal}. <p> This method does also check against the ranges of the given datatypes. @param value value which must be a number and in the range of the given datatype. @param name (optional) name of object reference (in source code). @param type requested return value type, must be a subclass of {@code Number}, i.e. one of {@code BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short} @return the given string argument converted to a number of the requested type @throws IllegalNumberArgumentException if the given argument {@code value} is no number
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNumberArgumentException.class }) public static <T extends CharSequence> T isNumeric(@Nonnull final T value) { return isNumeric(value, EMPTY_ARGUMENT_NAME); }
Ensures that a readable sequence of {@code char} values is numeric. Numeric arguments consist only of the characters 0-9 and may start with 0 (compared to number arguments, which must be valid numbers - think of a bank account number). <p> We recommend to use the overloaded method {@link Check#isNumeric(CharSequence, String)} and pass as second argument the name of the parameter to enhance the exception message. @param value a readable sequence of {@code char} values which must be a number @return the given string argument @throws IllegalNumberArgumentException if the given argument {@code value} is no number
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNumericArgumentException.class }) public static <T extends CharSequence> T isNumeric(@Nonnull final T value, @Nullable final String name) { Check.notNull(value, "value"); if (!matches(NumericRegularExpressionHolder.getPattern(), value)) { throw new IllegalNumericArgumentException(name, value); } return value; }
Ensures that a readable sequence of {@code char} values is numeric. Numeric arguments consist only of the characters 0-9 and may start with 0 (compared to number arguments, which must be valid numbers - think of a bank account number). @param value a readable sequence of {@code char} values which must be a number @param name name of object reference (in source code) @return the given string argument @throws IllegalNumberArgumentException if the given argument {@code value} is no number
@ArgumentsChecked @Throws(IllegalNotLesserThanException.class) public static byte lesserThan(final byte expected, final byte check) { if (expected <= check) { throw new IllegalNotLesserThanException(check); } return check; }
Ensures that a passed {@code byte} is less than another {@code byte}. @param expected Expected value @param check Comparable to be checked @return the passed {@code byte} argument {@code check} @throws IllegalNotLesserThanException if the argument value {@code check} is not lesser than value {@code expected}
@ArgumentsChecked @Throws(IllegalNotLesserThanException.class) public static byte lesserThan(final byte expected, final byte check, @Nonnull final String message) { if (expected <= check) { throw new IllegalNotLesserThanException(message, check); } return check; }
Ensures that a passed {@code byte} is less than another {@code byte}. @param expected Expected value @param check Comparable to be checked @param message an error message describing why the comparables must be less than a value (will be passed to {@code IllegalNotLessThanException}) @return the passed {@code Comparable} argument {@code check} @throws IllegalNotLesserThanException if the argument value {@code check} is not lesser than value {@code expected}
private static boolean matches(@Nonnull final Pattern pattern, @Nonnull final CharSequence chars) { return pattern.matcher(chars).matches(); }
Checks whether a character sequence matches against a specified pattern or not. @param pattern pattern, that the {@code chars} must correspond to @param chars a readable sequence of {@code char} values which should match the given pattern @return {@code true} when {@code chars} matches against the passed {@code pattern}, otherwise {@code false}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalPatternArgumentException.class }) public static <T extends CharSequence> T matchesPattern(@Nonnull final Pattern pattern, @Nonnull final T chars) { return matchesPattern(pattern, chars, EMPTY_ARGUMENT_NAME); }
Ensures that a readable sequence of {@code char} values matches a specified pattern. If the given character sequence does not match against the passed pattern, an {@link IllegalPatternArgumentException} will be thrown. <p> We recommend to use the overloaded method {@link Check#matchesPattern(Pattern, CharSequence, String)} and pass as second argument the name of the parameter to enhance the exception message. @param pattern pattern, that the {@code chars} must correspond to @param chars a readable sequence of {@code char} values which should match the given pattern @return the passed {@code chars} that matches the given pattern @throws IllegalNullArgumentException if the given argument {@code chars} is {@code null} @throws IllegalPatternArgumentException if the given {@code chars} that does not match the {@code pattern}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalPatternArgumentException.class }) public static <T extends CharSequence> T matchesPattern(@Nonnull final Pattern pattern, @Nonnull final T chars, @Nullable final String name) { Check.notNull(pattern, "pattern"); Check.notNull(chars, "chars"); if (!matches(pattern, chars)) { throw new IllegalPatternArgumentException(name, pattern, chars); } return chars; }
Ensures that a readable sequence of {@code char} values matches a specified pattern. If the given character sequence does not match against the passed pattern, an {@link IllegalPatternArgumentException} will be thrown. @param pattern pattern, that the {@code chars} must correspond to @param chars a readable sequence of {@code char} values which should match the given pattern @param name name of object reference (in source code) @return the passed {@code chars} that matches the given pattern @throws IllegalNullArgumentException if the given argument {@code chars} is {@code null} @throws IllegalPatternArgumentException if the given {@code chars} that does not match the {@code pattern}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class }) public static <T extends Iterable<?>> T noNullElements(@Nonnull final T iterable) { return noNullElements(iterable, EMPTY_ARGUMENT_NAME); }
Ensures that an iterable reference is neither {@code null} nor contains any elements that are {@code null}. <p> We recommend to use the overloaded method {@link Check#noNullElements(Iterable, String)} and pass as second argument the name of the parameter to enhance the exception message. @param iterable the iterable reference which should not contain {@code null} @return the passed reference which contains no elements that are {@code null} @throws IllegalNullElementsException if the given argument {@code iterable} contains elements that are {@code null}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class }) public static <T extends Iterable<?>> T noNullElements(@Nonnull final T iterable, final String name) { Check.notNull(iterable, "iterable"); for (final Object element : iterable) { if (element == null) { throw new IllegalNullElementsException(name); } } return iterable; }
Ensures that an iterable reference is neither {@code null} nor contains any elements that are {@code null}. @param iterable the iterable reference which should not contain {@code null} @param name name of object reference (in source code) @return the passed reference which contains no elements that are {@code null} @throws IllegalNullElementsException if the given argument {@code iterable} contains elements that are {@code null}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class }) public static <T> T[] noNullElements(@Nonnull final T[] array) { return noNullElements(array, EMPTY_ARGUMENT_NAME); }
Ensures that an array does not contain {@code null}. <p> We recommend to use the overloaded method {@link Check#noNullElements(Object[], String)} and pass as second argument the name of the parameter to enhance the exception message. @param array reference to an array @return the passed reference which contains no elements that are {@code null} @throws IllegalNullElementsException if the given argument {@code array} contains {@code null}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class }) public static <T> T[] noNullElements(@Nonnull final T[] array, @Nullable final String name) { Check.notNull(array, "array"); if (containsNullElements(array)) { throw new IllegalNullElementsException(name); } return array; }
Ensures that an array does not contain {@code null}. @param array reference to an array @param name name of object reference (in source code) @return the passed reference which contains no elements that are {@code null} @throws IllegalNullElementsException if the given argument {@code array} contains {@code null}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static void notEmpty(final boolean expression, @Nullable final String name) { if (expression) { throw new IllegalEmptyArgumentException(name); } }
Ensures that a passed parameter of the calling method is not empty, using the passed expression to evaluate the emptiness. @param expression the result of the expression to verify the emptiness of a reference ({@code true} means empty, {@code false} means not empty) @param name name of object reference (in source code) @throws IllegalEmptyArgumentException if the given argument {@code reference} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T extends CharSequence> T notEmpty(@Nonnull final T chars) { notNull(chars); notEmpty(chars, chars.length() == 0, EMPTY_ARGUMENT_NAME); return chars; }
Ensures that a passed string as a parameter of the calling method is not empty. <p> We recommend to use the overloaded method {@link Check#notEmpty(CharSequence, String)} and pass as second argument the name of the parameter to enhance the exception message. @param chars a readable sequence of {@code char} values which should not be empty @return the passed reference that is not empty @throws IllegalNullArgumentException if the given argument {@code reference} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code reference} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T extends Collection<?>> T notEmpty(@Nonnull final T collection) { notNull(collection); notEmpty(collection, collection.isEmpty(), EMPTY_ARGUMENT_NAME); return collection; }
Ensures that a passed collection as a parameter of the calling method is not empty. <p> We recommend to use the overloaded method {@link Check#notEmpty(Collection, String)} and pass as second argument the name of the parameter to enhance the exception message. @param collection a collection which should not be empty @return the passed reference that is not empty @throws IllegalNullArgumentException if the given argument {@code collection} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code collection} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T extends Iterable<?>> T notEmpty(@Nonnull final T iterable) { notNull(iterable); notEmpty(iterable, !iterable.iterator().hasNext(), EMPTY_ARGUMENT_NAME); return iterable; }
Ensures that a passed iterable as a parameter of the calling method is not empty. <p> We recommend to use the overloaded method {@link Check#notEmpty(Iterable, String)} and pass as second argument the name of the parameter to enhance the exception message. @param iterable an iterable which should not be empty @return the passed reference that is not empty @throws IllegalNullArgumentException if the given argument {@code iterable} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code iterable} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T extends Map<?, ?>> T notEmpty(@Nonnull final T map) { notNull(map); notEmpty(map, map.isEmpty(), EMPTY_ARGUMENT_NAME); return map; }
Ensures that a passed map as a parameter of the calling method is not empty. <p> We recommend to use the overloaded method {@link Check#notEmpty(Collection, String)} and pass as second argument the name of the parameter to enhance the exception message. @param map a map which should not be empty @return the passed reference that is not empty @throws IllegalNullArgumentException if the given argument {@code map} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code map} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T> T notEmpty(@Nonnull final T reference, final boolean expression, @Nullable final String name) { notNull(reference, name); if (expression) { throw new IllegalEmptyArgumentException(name); } return reference; }
Ensures that an object reference passed as a parameter to the calling method is not empty. The passed boolean value is the result of checking whether the reference is empty or not. <p> The following example describes how to use it. <pre> &#064;ArgumentsChecked public setText(String text) { Check.notEmpty(text, text.isEmpty(), &quot;text&quot;); this.text = text; } </pre> @param reference an object reference which should not be empty @param expression the result of the expression to verify the emptiness of a reference ({@code true} means empty, {@code false} means not empty) @param name name of object reference (in source code) @return the passed reference that is not empty @throws IllegalNullArgumentException if the given argument {@code reference} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code reference} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T extends CharSequence> T notEmpty(@Nonnull final T chars, @Nullable final String name) { notNull(chars, name); notEmpty(chars, chars.length() == 0, name); return chars; }
Ensures that a passed string as a parameter of the calling method is not empty. <p> The following example describes how to use it. <pre> &#064;ArgumentsChecked public setText(String text) { this.text = Check.notEmpty(text, &quot;text&quot;); } </pre> @param chars a readable sequence of {@code char} values which should not be empty @param name name of object reference (in source code) @return the passed reference that is not empty @throws IllegalNullArgumentException if the given argument {@code string} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code string} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T extends Map<?, ?>> T notEmpty(@Nonnull final T map, @Nullable final String name) { notNull(map); notEmpty(map, map.isEmpty(), name); return map; }
Ensures that a passed map as a parameter of the calling method is not empty. <p> We recommend to use the overloaded method {@link Check#notEmpty(Collection, String)} and pass as second argument the name of the parameter to enhance the exception message. @param map a map which should not be empty @param name name of object reference (in source code) @return the passed reference that is not empty @throws IllegalNullArgumentException if the given argument {@code map} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code map} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T extends Collection<?>> T notEmpty(@Nonnull final T collection, @Nullable final String name) { notNull(collection, name); notEmpty(collection, collection.isEmpty(), name); return collection; }
Ensures that a passed collection as a parameter of the calling method is not empty. <p> The following example describes how to use it. <pre> &#064;ArgumentsChecked public setCollection(Collection&lt;String&gt; collection) { this.collection = Check.notEmpty(collection, &quot;collection&quot;); } </pre> @param collection a collection which should not be empty @param name name of object reference (in source code) @return the passed reference that is not empty @throws IllegalNullArgumentException if the given argument {@code collection} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code collection} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T extends Iterable<?>> T notEmpty(@Nonnull final T iterable, @Nullable final String name) { notNull(iterable, name); notEmpty(iterable, !iterable.iterator().hasNext(), name); return iterable; }
Ensures that a passed iterable as a parameter of the calling method is not empty. <p> The following example describes how to use it. <pre> &#064;ArgumentsChecked public setIterable(Iterable&lt;String&gt; iterable) { this.iterable = Check.notEmpty(iterable, &quot;iterable&quot;); } </pre> @param iterable an iterable which should not be empty @param name name of object reference (in source code) @return the passed reference that is not empty @throws IllegalNullArgumentException if the given argument {@code iterable} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code iterable} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T> T[] notEmpty(@Nonnull final T[] array) { notNull(array); notEmpty(array, array.length == 0, EMPTY_ARGUMENT_NAME); return array; }
Ensures that a passed map as a parameter of the calling method is not empty. <p> We recommend to use the overloaded method {@link Check#notEmpty(Object[], String)} and pass as second argument the name of the parameter to enhance the exception message. @param array a map which should not be empty @return the passed reference that is not empty @throws IllegalNullArgumentException if the given argument {@code array} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code array} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T> T[] notEmpty(@Nonnull final T[] array, @Nullable final String name) { notNull(array); notEmpty(array, array.length == 0, EMPTY_ARGUMENT_NAME); return array; }
Ensures that a passed map as a parameter of the calling method is not empty. @param array a map which should not be empty @param name name of object reference (in source code) @return the passed reference that is not empty @throws IllegalNullArgumentException if the given argument {@code array} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code array} is empty
@Throws(IllegalEqualException.class) public static boolean notEquals(final boolean expected, final boolean check) { if (expected == check) { throw new IllegalEqualException(check); } return check; }
Ensures that a passed boolean is not equal to another boolean. The comparison is made using <code>expected == check</code>. <p> We recommend to use the overloaded method {@link Check#notEquals(boolean, boolean, String)} and pass as second argument the name of the parameter to enhance the exception message. @param expected Expected value @param check boolean to be checked @return the passed boolean argument {@code check} @throws IllegalEqualException if both argument values are not equal
@Throws(IllegalEqualException.class) public static boolean notEquals(final boolean expected, final boolean check, @Nonnull final String message) { if (expected == check) { throw new IllegalEqualException(message, check); } return check; }
Ensures that a passed boolean is not equal to another boolean. The comparison is made using <code>expected == check</code>. @param expected Expected value @param check boolean to be checked @param message an error message describing why the booleans must equal (will be passed to {@code IllegalEqualException}) @return the passed boolean argument {@code check} @throws IllegalEqualException if both argument values are not equal
@Throws(IllegalNaNArgumentException.class) public static double notNaN(final double value, @Nullable final String name) { // most efficient check for NaN, see Double.isNaN(value)) if (value != value) { throw new IllegalNaNArgumentException(name); } return value; }
Ensures that a double argument is not NaN (not a number). @see java.lang.Double#NaN @param value value which should not be NaN @param name name of object reference (in source code) @return the given double value @throws IllegalNaNArgumentException if the given argument {@code value} is NaN
@Throws(IllegalNegativeArgumentException.class) public static double notNegative(final double value, @Nullable final String name) { if (value < 0.0) { throw new IllegalNegativeArgumentException(name, value); } return value; }
Ensures that an double reference passed as a parameter to the calling method is not smaller than {@code 0}. @param value a number @param name name of the number reference (in source code) @return the non-null reference that was validated @throws IllegalNullArgumentException if the given argument {@code reference} is smaller than {@code 0}
@Throws(IllegalNullArgumentException.class) public static <T> T notNull(@Nonnull final T reference) { if (reference == null) { throw new IllegalNullArgumentException(); } return reference; }
Ensures that an object reference passed as a parameter to the calling method is not {@code null}. <p> We recommend to use the overloaded method {@link Check#notNull(Object, String)} and pass as second argument the name of the parameter to enhance the exception message. @param reference an object reference @return the non-null reference that was validated @throws IllegalNullArgumentException if the given argument {@code reference} is {@code null}
@Throws(IllegalNullArgumentException.class) public static <T> T notNull(@Nonnull final T reference, @Nullable final String name) { if (reference == null) { throw new IllegalNullArgumentException(name); } return reference; }
Ensures that an object reference passed as a parameter to the calling method is not {@code null}. @param reference an object reference @param name name of object reference (in source code) @return the non-null reference that was validated @throws IllegalNullArgumentException if the given argument {@code reference} is {@code null}
@Throws(IllegalPositiveArgumentException.class) public static double notPositive(final double value, @Nullable final String name) { if (value > 0.0) { throw new IllegalPositiveArgumentException(name, value); } return value; }
Ensures that an double reference passed as a parameter to the calling method is not greater than {@code 0}. @param value a number @param name name of the number reference (in source code) @return the non-null reference that was validated @throws IllegalNullArgumentException if the given argument {@code reference} is smaller than {@code 0}
@Throws(IllegalPositionIndexException.class) public static int positionIndex(final int index, final int size) { final boolean isIndexValid = (size >= 0) && (index >= 0) && (index < size); if (!isIndexValid) { throw new IllegalPositionIndexException(index, size); } return index; }
Ensures that a given position index is valid within the size of an array, list or string ... @param index index of an array, list or string @param size size of an array list or string @return the index @throws IllegalPositionIndexException if the index is not a valid position index within an array, list or string of size <em>size</em>
@Throws(IllegalRangeException.class) public static void range(@Nonnegative final int start, @Nonnegative final int end, @Nonnegative final int size) { final boolean rangeIsValid = (start <= size) && (end <= size) && (start <= end); final boolean inputValuesAreValid = (size >= 0) && (start >= 0) && (end >= 0); if (!rangeIsValid || !inputValuesAreValid) { throw new IllegalRangeException(start, end, size); } }
Ensures that the given arguments are a valid range. A range (<em>start</em>, <em>end</em>, <em>size</em>) is valid if the following conditions are {@code true}: <ul> <li>start <= size</li> <li>end <= size</li> <li>start <= end</li> <li>size >= 0</li> <li>start >= 0</li> <li>end >= 0</li> </ul> @param start the start value of the range (must be a positive integer or 0) @param end the end value of the range (must be a positive integer or 0) @param size the size value of the range (must be a positive integer or 0) @throws IllegalRangeException if the given arguments do not form a valid range
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, RuntimeInstantiationException.class }) public static void stateIsTrue(final boolean expression, final Class<? extends RuntimeException> clazz) { Check.notNull(clazz, "clazz"); if (!expression) { RuntimeException re; try { re = clazz.newInstance(); } catch (final InstantiationException e) { throw new RuntimeInstantiationException(clazz.getSimpleName(), e); } catch (final IllegalAccessException e) { throw new RuntimeInstantiationException(clazz.getSimpleName(), e); } throw re; } }
Ensures that a given state is {@code true} and allows to specify the class of exception which is thrown in case the state is not {@code true}. @param expression an expression that must be {@code true} to indicate a valid state @param clazz an subclass of {@link RuntimeException} which will be thrown if the given state is not valid @throws clazz a new instance of {@code clazz} if the given arguments caused an invalid state @throws RuntimeInstantiationException <strong>Attention</strong>: Be aware, that a {@code RuntimeInstantiationException} can be thrown when the given {@code clazz} cannot be instantiated
@Throws(IllegalStateOfArgumentException.class) public static void stateIsTrue(final boolean expression, @Nonnull final String description) { if (!expression) { throw new IllegalStateOfArgumentException(description); } }
Ensures that a given state is {@code true}. @param expression an expression that must be {@code true} to indicate a valid state @param description will be used in the error message to describe why the arguments caused an invalid state @throws IllegalStateOfArgumentException if the given arguments caused an invalid state
@Throws(IllegalStateOfArgumentException.class) public static void stateIsTrue(final boolean expression, @Nonnull final String descriptionTemplate, final Object... descriptionTemplateArgs) { if (!expression) { throw new IllegalStateOfArgumentException(descriptionTemplate, descriptionTemplateArgs); } }
Ensures that a given state is {@code true} @param expression an expression that must be {@code true} to indicate a valid state @param descriptionTemplate format string template that explains why the state is invalid @param descriptionTemplateArgs format string template arguments to explain why the state is invalid @throws IllegalStateOfArgumentException if the given arguments caused an invalid state
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNotContainedArgumentException.class }) public static <T extends Object> void contains(final boolean condition, @Nonnull final Collection<T> haystack, @Nonnull final T needle) { if (condition) { Check.contains(haystack, needle); } }
Ensures that an element {@code needle} is contained in a collection {@code haystack}. <p> This is in particular useful if you want to check whether an enum value is contained in an {@code EnumSet}. The check is implemented using {@link java.util.Collection#contains(Object)}. <p> The condition must evaluate to {@code true} so that the check is executed. <p> We recommend to use the overloaded method {@link Check#contains(Collection, Object, String)} and pass as second argument the name of the parameter to enhance the exception message. @param condition condition must be {@code true}^ so that the check will be performed @param haystack A collection which must contain {@code needle} @param needle An object that must be contained into a collection. @throws IllegalNotContainedArgumentException if the passed {@code needle} can not be found in {@code haystack}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalMissingAnnotationException.class }) public static void hasAnnotation(final boolean condition, @Nonnull final Class<?> clazz, @Nonnull final Class<? extends Annotation> annotation) { Check.notNull(clazz, "clazz"); Check.notNull(annotation, "annotation"); if (condition) { Check.hasAnnotation(clazz, annotation); } }
Ensures that a passed class has an annotation of a specific type @param condition condition must be {@code true}^ so that the check will be performed @param clazz the class that must have a required annotation @param annotation the type of annotation that is required on the class @throws IllegalMissingAnnotationException if the passed annotation is not annotated at the given class
@Throws(IllegalNotNullArgumentException.class) public static void isNull(final boolean condition, @Nullable final Object reference, @Nullable final String name) { if (condition) { Check.isNull(reference, name); } }
Ensures that a given argument is {@code null}. Normally, the usage of {@code null} arguments is disregarded by the authors of quality-check. Still, there are certain circumstances where null is required, e.g. the primary key of an entity before it is written to the database for the first time. In such cases it is ok to use null values and there should also be checks for them. For example, to avoid overwriting an existing primary key with a new one. @param condition condition must be {@code true}^ so that the check will be performed @param reference reference which must be null. @param name name of object reference (in source code) @throws IllegalNotNullArgumentException if the given argument {@code reference} is not null
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNumberArgumentException.class }) public static void isNumber(final boolean condition, @Nonnull final String value) { if (condition) { Check.isNumber(value); } }
Ensures that a String argument is a number. @param condition condition must be {@code true}^ so that the check will be performed @param value value which must be a number @throws IllegalNumberArgumentException if the given argument {@code value} is not a number
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNumberArgumentException.class }) public static <T extends Number> void isNumber(final boolean condition, @Nonnull final String value, @Nonnull final Class<T> type) { if (condition) { Check.isNumber(value, type); } }
Ensures that a String argument is a number. This overload supports all subclasses of {@code Number}. The number is first converted to a BigInteger @param condition condition must be {@code true}^ so that the check will be performed @param value value which must be a number @param type requested return value type, must be a subclass of {@code Number}, i.e. one of {@code BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short} @throws IllegalNumberArgumentException if the given argument {@code value} is no number
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNumberArgumentException.class }) public static <T extends CharSequence> void isNumeric(final boolean condition, @Nonnull final T value) { if (condition) { Check.isNumeric(value); } }
Ensures that a readable sequence of {@code char} values is numeric. Numeric arguments consist only of the characters 0-9 and may start with 0 (compared to number arguments, which must be valid numbers - think of a bank account number). <p> We recommend to use the overloaded method {@link Check#isNumeric(CharSequence, String)} and pass as second argument the name of the parameter to enhance the exception message. @param condition condition must be {@code true}^ so that the check will be performed @param value a readable sequence of {@code char} values which must be a number @throws IllegalNumberArgumentException if the given argument {@code value} is no number
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNumericArgumentException.class }) public static <T extends CharSequence> void isNumeric(final boolean condition, @Nonnull final T value, @Nullable final String name) { if (condition) { Check.isNumeric(value, name); } }
Ensures that a readable sequence of {@code char} values is numeric. Numeric arguments consist only of the characters 0-9 and may start with 0 (compared to number arguments, which must be valid numbers - think of a bank account number). @param condition condition must be {@code true}^ so that the check will be performed @param value a readable sequence of {@code char} values which must be a number @param name name of object reference (in source code) @throws IllegalNumberArgumentException if the given argument {@code value} is no number
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNotLesserThanException.class }) public static <T extends Comparable<T>> void lesserThan(final boolean condition, @Nonnull final T expected, @Nonnull final T check, final String message) { Check.notNull(expected, "expected"); Check.notNull(check, "check"); if (condition) { Check.lesserThan(expected, check, message); } }
Ensures that a passed {@code Comparable} is less than another {@code Comparable}. The comparison is made using {@code expected.compareTo(check) <= 0}. @param condition condition must be {@code true}^ so that the check will be performed @param expected Expected value @param check Comparable to be checked @param message an error message describing why the comparables must be less than a value (will be passed to {@code IllegalNotLessThanException}) @throws IllegalNotLesserThanException if the argument value {@code check} is not lesser than value {@code expected} when using method {@code compareTo}
@ArgumentsChecked @Throws(IllegalNullArgumentException.class) public static <T extends CharSequence> void matchesPattern(final boolean condition, @Nonnull final Pattern pattern, @Nonnull final T chars) { if (condition) { Check.matchesPattern(pattern, chars); } }
Ensures that a readable sequence of {@code char} values matches a specified pattern. If the given character sequence does not match against the passed pattern, an {@link IllegalPatternArgumentException} will be thrown. <p> We recommend to use the overloaded method {@link Check#matchesPattern(Pattern, CharSequence, String)} and pass as second argument the name of the parameter to enhance the exception message. @param condition condition must be {@code true}^ so that the check will be performed @param pattern pattern, that the {@code chars} must correspond to @param chars a readable sequence of {@code char} values which should match the given pattern @throws IllegalNullArgumentException if the given argument {@code chars} is {@code null} @throws IllegalPatternArgumentException if the given {@code chars} that does not match the {@code pattern}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalPatternArgumentException.class }) public static <T extends CharSequence> void matchesPattern(final boolean condition, @Nonnull final Pattern pattern, @Nonnull final T chars, @Nullable final String name) { if (condition) { Check.matchesPattern(pattern, chars, name); } }
Ensures that a readable sequence of {@code char} values matches a specified pattern. If the given character sequence does not match against the passed pattern, an {@link IllegalPatternArgumentException} will be thrown. @param condition condition must be {@code true}^ so that the check will be performed @param pattern pattern, that the {@code chars} must correspond to @param chars a readable sequence of {@code char} values which should match the given pattern @param name name of object reference (in source code) @throws IllegalNullArgumentException if the given argument {@code chars} is {@code null} @throws IllegalPatternArgumentException if the given {@code chars} that does not match the {@code pattern}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class }) public static <T extends Iterable<?>> void noNullElements(final boolean condition, @Nonnull final T iterable) { if (condition) { Check.noNullElements(iterable); } }
Ensures that an iterable reference is neither {@code null} nor contains any elements that are {@code null}. <p> We recommend to use the overloaded method {@link Check#noNullElements(Iterable, String)} and pass as second argument the name of the parameter to enhance the exception message. @param condition condition must be {@code true}^ so that the check will be performed @param iterable the iterable reference which should not contain {@code null} @throws IllegalNullElementsException if the given argument {@code iterable} contains elements that are {@code null}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class }) public static <T> void noNullElements(final boolean condition, @Nonnull final T[] array) { if (condition) { Check.noNullElements(array); } }
Ensures that an array does not contain {@code null}. <p> We recommend to use the overloaded method {@link Check#noNullElements(Object[], String)} and pass as second argument the name of the parameter to enhance the exception message. @param condition condition must be {@code true}^ so that the check will be performed @param array reference to an array @throws IllegalNullElementsException if the given argument {@code array} contains {@code null}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class }) public static <T> void noNullElements(final boolean condition, @Nonnull final T[] array, @Nullable final String name) { if (condition) { Check.noNullElements(array, name); } }
Ensures that an array does not contain {@code null}. @param condition condition must be {@code true}^ so that the check will be performed @param array reference to an array @param name name of object reference (in source code) @throws IllegalNullElementsException if the given argument {@code array} contains {@code null}
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static void notEmpty(final boolean condition, final boolean expression) { if (condition) { Check.notEmpty(expression); } }
Ensures that a passed parameter of the calling method is not empty, using the passed expression to evaluate the emptiness. <p> We recommend to use the overloaded method {@link Check#notEmpty(boolean, String)} and pass as second argument the name of the parameter to enhance the exception message. @param condition condition must be {@code true}^ so that the check will be performed @param expression the result of the expression to verify the emptiness of a reference ({@code true} means empty, {@code false} means not empty) @throws IllegalNullArgumentException if the given argument {@code reference} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code reference} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static void notEmpty(final boolean condition, final boolean expression, @Nullable final String name) { if (condition) { Check.notEmpty(expression, name); } }
Ensures that a passed parameter of the calling method is not empty, using the passed expression to evaluate the emptiness. @param condition condition must be {@code true}^ so that the check will be performed @param expression the result of the expression to verify the emptiness of a reference ({@code true} means empty, {@code false} means not empty) @param name name of object reference (in source code) @throws IllegalEmptyArgumentException if the given argument {@code reference} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T extends CharSequence> void notEmpty(final boolean condition, @Nonnull final T chars) { if (condition) { Check.notEmpty(chars); } }
Ensures that a passed string as a parameter of the calling method is not empty. <p> We recommend to use the overloaded method {@link Check#notEmpty(CharSequence, String)} and pass as second argument the name of the parameter to enhance the exception message. @param condition condition must be {@code true}^ so that the check will be performed @param chars a readable sequence of {@code char} values which should not be empty @throws IllegalNullArgumentException if the given argument {@code reference} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code reference} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T extends Collection<?>> void notEmpty(final boolean condition, @Nonnull final T collection) { if (condition) { Check.notEmpty(collection); } }
Ensures that a passed collection as a parameter of the calling method is not empty. <p> We recommend to use the overloaded method {@link Check#notEmpty(Collection, String)} and pass as second argument the name of the parameter to enhance the exception message. @param condition condition must be {@code true}^ so that the check will be performed @param collection a collection which should not be empty @throws IllegalNullArgumentException if the given argument {@code collection} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code collection} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T extends Map<?, ?>> void notEmpty(final boolean condition, @Nonnull final T map) { if (condition) { Check.notEmpty(map); } }
Ensures that a passed map as a parameter of the calling method is not empty. <p> We recommend to use the overloaded method {@link Check#notEmpty(Collection, String)} and pass as second argument the name of the parameter to enhance the exception message. @param condition condition must be {@code true}^ so that the check will be performed @param map a map which should not be empty @throws IllegalNullArgumentException if the given argument {@code map} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code map} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T> void notEmpty(final boolean condition, @Nonnull final T reference, final boolean expression, @Nullable final String name) { if (condition) { Check.notEmpty(reference, expression, name); } }
Ensures that an object reference passed as a parameter to the calling method is not empty. The passed boolean value is the result of checking whether the reference is empty or not. <p> The following example describes how to use it. <pre> &#064;ArgumentsChecked public setText(String text) { ConditionalCheck.notEmpty(true, text, text.isEmpty(), &quot;text&quot;); this.text = text; } </pre> @param condition condition must be {@code true}^ so that the check will be performed @param reference an object reference which should not be empty @param expression the result of the expression to verify the emptiness of a reference ({@code true} means empty, {@code false} means not empty) @param name name of object reference (in source code) @throws IllegalNullArgumentException if the given argument {@code reference} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code reference} is empty
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class }) public static <T> void notEmpty(final boolean condition, @Nonnull final T[] array) { if (condition) { Check.notEmpty(array); } }
Ensures that a passed map as a parameter of the calling method is not empty. <p> We recommend to use the overloaded method {@link Check#notEmpty(Object[], String)} and pass as second argument the name of the parameter to enhance the exception message. @param condition condition must be {@code true}^ so that the check will be performed @param array a map which should not be empty @throws IllegalNullArgumentException if the given argument {@code array} is {@code null} @throws IllegalEmptyArgumentException if the given argument {@code array} is empty
@Throws(IllegalEqualException.class) public static void notEquals(final boolean condition, final boolean expected, final boolean check) { if (condition) { Check.notEquals(expected, check); } }
Ensures that a passed boolean is not equal to another boolean. The comparison is made using <code>expected == check</code>. <p> We recommend to use the overloaded method {@link ConditionalCheck#notEquals(condition,boolean, boolean, String)} and pass as second argument the name of the parameter to enhance the exception message. @param condition condition must be {@code true}^ so that the check will be performed @param expected Expected value @param check boolean to be checked @throws IllegalEqualException if both argument values are not equal
@Throws(IllegalEqualException.class) public static void notEquals(final boolean condition, final boolean expected, final boolean check, @Nonnull final String message) { if (condition) { Check.notEquals(expected, check, message); } }
Ensures that a passed boolean is not equal to another boolean. The comparison is made using <code>expected == check</code>. @param condition condition must be {@code true}^ so that the check will be performed @param expected Expected value @param check boolean to be checked @param message an error message describing why the booleans must equal (will be passed to {@code IllegalEqualException}) @throws IllegalEqualException if both argument values are not equal
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, IllegalEqualException.class }) public static <T extends Comparable<T>> void notEquals(final boolean condition, @Nonnull final T expected, @Nonnull final T check) { Check.notNull(expected, "expected"); Check.notNull(check, "check"); if (condition) { Check.notEquals(expected, check); } }
Ensures that a passed {@code Comparable} is not equal to another {@code Comparable}. The comparison is made using {@code expected.compareTo(check) == 0}. <p> We recommend to use the overloaded method {@link ConditionalCheck#notEquals(condition,Comparable, Comparable, String)} and pass as second argument the name of the parameter to enhance the exception message. @param condition condition must be {@code true}^ so that the check will be performed @param expected Expected value @param check Comparable to be checked @throws IllegalEqualException if both argument values are not equal
@Throws(IllegalNaNArgumentException.class) public static void notNaN(final boolean condition, final double value, @Nullable final String name) { if (condition) { Check.notNaN(value, name); } }
Ensures that a double argument is not NaN (not a number). @see java.lang.Double#NaN @param condition condition must be {@code true}^ so that the check will be performed @param value value which should not be NaN @param name name of object reference (in source code) @throws IllegalNaNArgumentException if the given argument {@code value} is NaN
@Throws(IllegalNegativeArgumentException.class) public static void notNegative(final boolean condition, final int value, @Nullable final String name) { if (condition) { Check.notNegative(value, name); } }
Ensures that an integer reference passed as a parameter to the calling method is not smaller than {@code 0}. @param condition condition must be {@code true}^ so that the check will be performed @param value a number @param name name of the number reference (in source code) @throws IllegalNullArgumentException if the given argument {@code reference} is smaller than {@code 0}
@Throws(IllegalNullArgumentException.class) public static <T> void notNull(final boolean condition, @Nonnull final T reference) { if (condition) { Check.notNull(reference); } }
Ensures that an object reference passed as a parameter to the calling method is not {@code null}. <p> We recommend to use the overloaded method {@link Check#notNull(Object, String)} and pass as second argument the name of the parameter to enhance the exception message. @param condition condition must be {@code true}^ so that the check will be performed @param reference an object reference @throws IllegalNullArgumentException if the given argument {@code reference} is {@code null}
@Throws(IllegalNullArgumentException.class) public static <T> void notNull(final boolean condition, @Nonnull final T reference, @Nullable final String name) { if (condition) { Check.notNull(reference, name); } }
Ensures that an object reference passed as a parameter to the calling method is not {@code null}. @param condition condition must be {@code true}^ so that the check will be performed @param reference an object reference @param name name of object reference (in source code) @throws IllegalNullArgumentException if the given argument {@code reference} is {@code null}
@Throws(IllegalPositiveArgumentException.class) public static void notPositive(final boolean condition, final int value, @Nullable final String name) { if (condition) { Check.notPositive(value, name); } }
Ensures that an integer reference passed as a parameter to the calling method is not greater than {@code 0}. @param condition condition must be {@code true}^ so that the check will be performed @param value a number @param name name of the number reference (in source code) @throws IllegalNullArgumentException if the given argument {@code reference} is smaller than {@code 0}
@Throws(IllegalPositionIndexException.class) public static void positionIndex(final boolean condition, final int index, final int size) { if (condition) { Check.positionIndex(index, size); } }
Ensures that a given position index is valid within the size of an array, list or string ... @param condition condition must be {@code true}^ so that the check will be performed @param index index of an array, list or string @param size size of an array list or string @throws IllegalPositionIndexException if the index is not a valid position index within an array, list or string of size <em>size</em>
@Throws(IllegalRangeException.class) public static void range(final boolean condition, @Nonnegative final int start, @Nonnegative final int end, @Nonnegative final int size) { if (condition) { Check.range(start, end, size); } }
Ensures that the given arguments are a valid range. A range (<em>start</em>, <em>end</em>, <em>size</em>) is valid if the following conditions are {@code true}: <ul> <li>start <= size</li> <li>end <= size</li> <li>start <= end</li> <li>size >= 0</li> <li>start >= 0</li> <li>end >= 0</li> </ul> @param condition condition must be {@code true}^ so that the check will be performed @param start the start value of the range (must be a positive integer or 0) @param end the end value of the range (must be a positive integer or 0) @param size the size value of the range (must be a positive integer or 0) @throws IllegalRangeException if the given arguments do not form a valid range
@ArgumentsChecked @Throws({ IllegalNullArgumentException.class, RuntimeInstantiationException.class }) public static void stateIsTrue(final boolean condition, final boolean expression, final Class<? extends RuntimeException> clazz) { if (condition) { Check.stateIsTrue(expression, clazz); } }
Ensures that a given state is {@code true} and allows to specify the class of exception which is thrown in case the state is not {@code true}. @param condition condition must be {@code true}^ so that the check will be performed @param expression an expression that must be {@code true} to indicate a valid state @param clazz an subclass of {@link RuntimeException} which will be thrown if the given state is not valid @throws clazz a new instance of {@code clazz} if the given arguments caused an invalid state @throws RuntimeInstantiationException <strong>Attention</strong>: Be aware, that a {@code RuntimeInstantiationException} can be thrown when the given {@code clazz} cannot be instantiated
@Throws(IllegalStateOfArgumentException.class) public static void stateIsTrue(final boolean condition, final boolean expression, @Nonnull final String description) { if (condition) { Check.stateIsTrue(expression, description); } }
Ensures that a given state is {@code true}. @param condition condition must be {@code true}^ so that the check will be performed @param expression an expression that must be {@code true} to indicate a valid state @param description will be used in the error message to describe why the arguments caused an invalid state @throws IllegalStateOfArgumentException if the given arguments caused an invalid state
public void removeAllWriter(boolean close) { if (close) synchronized (writers) { for (final Iterator i = writers.iterator(); i.hasNext();) ((LogWriter) i.next()).close(); } writers = new Vector(); }
Removes all registered log writer from this log service. <p> @param close should the writers be closed before removal
public void warn(String msg, Throwable t) { log(LogLevel.WARN, msg, t); }
Offers <code>msg</code> and the <code>throwable</code> object with log level {@link LogLevel#WARN}. <p> @param msg log information @param t throwable object
public void error(String msg, Throwable t) { log(LogLevel.ERROR, msg, t); }
Offers <code>msg</code> and the <code>throwable</code> object with log level {@link LogLevel#ERROR}. <p> @param msg log information @param t throwable object
public void fatal(String msg, Throwable t) { log(LogLevel.FATAL, msg, t); }
Offers <code>msg</code> and the <code>throwable</code> object with log level {@link LogLevel#FATAL}. <p> @param msg log information @param t throwable object
public void log(LogLevel level, String msg) { if (level != LogLevel.OFF && level.level <= logLevel.level) logger.add(writers, name, level, msg, null); }
Offers <code>msg</code> with log <code>level</code>. <p> @param level log level for this message @param msg log information
public void log(LogLevel level, String msg, Throwable t) { if (level != LogLevel.OFF && level.level <= logLevel.level) logger.add(writers, name, level, msg, t); }
Offers <code>msg</code> and the <code>throwable</code> object with log <code>level</code>. <p> @param level log level for this message and throwable @param msg log information @param t throwable object
@ArgumentsChecked @Throws(IllegalNullArgumentException.class) private static String format(@Nonnull final Class<? extends Annotation> annotation) { if (annotation == null) { throw new IllegalNullArgumentException("annotation"); } return String.format(MESSAGE_WITH_ANNOTATION, annotation.getName()); }
Returns the formatted string {@link IllegalMissingAnnotationException#MESSAGE_WITH_ANNOTATION} with the given {@code annotation}. @param annotation the name of the required annotation @return a formatted string of message with the given argument name
@ArgumentsChecked @Throws(IllegalNullArgumentException.class) private static String format(@Nonnull final Class<? extends Annotation> annotation, @Nullable final Class<?> clazz) { if (annotation == null) { throw new IllegalNullArgumentException("annotation"); } if (clazz != null) { return String.format(MESSAGE_WITH_ANNOTATION_AND_CLASS, clazz.getName(), annotation.getName()); } else { return format(annotation); } }
Returns the formatted string {@link IllegalMissingAnnotationException#MESSAGE_WITH_ANNOTATION_AND_CLASS} with the given {@code annotation} and {@code clazz}. @param annotation the required annotation @param clazz the name of the class which does not have the required annotation @return a formatted string of message with the given argument name
@Override public Trajectory subList(int fromIndex, int toIndex) { Trajectory t = new Trajectory(dimension); for(int i = fromIndex; i < toIndex; i++){ t.add(this.get(i)); } return t; }
Generates a sub-trajectory
public double[][] getPositionsAsArray(){ double[][] posAsArr = new double[size()][3]; for(int i = 0; i < size(); i++){ if(get(i)!=null){ posAsArr[i][0] = get(i).x; posAsArr[i][1] = get(i).y; posAsArr[i][2] = get(i).z; } else{ posAsArr[i] = null; } } return posAsArr; }
Converts the positions to a 2D double array @return 2d double array [i][j], i=Time index, j=coordinate index
public void scale(double v){ for(int i = 0; i < this.size(); i++){ this.get(i).scale(v);; } }
Multiplies all positions with a factor v @param v Multiplication factor
public static int randomIntBetween(int min, int max) { Random rand = new Random(); return rand.nextInt((max - min) + 1) + min; }
Returns an integer between interval @param min Minimum value @param max Maximum value @return int number
public static long randomLongBetween(long min, long max) { Random rand = new Random(); return min + (long) (rand.nextDouble() * (max - min)); }
Returns a long between interval @param min Minimum value @param max Maximum value @return long number
byte[] toByteArray(ByteArrayOutputStream os) { byte[] buf = desc.getDevice().toByteArray(); os.write(buf, 0, buf.length); buf = desc.getServiceFamilies().toByteArray(); os.write(buf, 0, buf.length); return os.toByteArray(); }
/* (non-Javadoc) @see tuwien.auto.calimero.knxnetip.servicetype.ServiceType#toByteArray (java.io.ByteArrayOutputStream)
public static BufferedImage trim(BufferedImage img) { int width = getTrimmedWidth(img); int height = getTrimmedHeight(img); int xStart = getTrimmedXStart(img); int yStart = getTrimmedYStart(img); BufferedImage newImg = new BufferedImage(width - xStart, height - yStart, BufferedImage.TYPE_INT_ARGB); Graphics g = newImg.createGraphics(); g.drawImage(img, 0 - xStart, 0 - yStart, null); return newImg; }
Trims an image (removes all leading and trailing white spaces) @param img Image in memory @return Trimmed image
private static int getTrimmedXStart(BufferedImage img) { int height = img.getHeight(); int width = img.getWidth(); int xStart = width; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (img.getRGB(j, i) != Color.WHITE.getRGB() && j < xStart) { xStart = j; break; } } } return xStart; }
Get the first non-white X point @param img Image n memory @return the x start
private static int getTrimmedWidth(BufferedImage img) { int height = img.getHeight(); int width = img.getWidth(); int trimmedWidth = 0; for (int i = 0; i < height; i++) { for (int j = width - 1; j >= 0; j--) { if (img.getRGB(j, i) != Color.WHITE.getRGB() && j > trimmedWidth) { trimmedWidth = j; break; } } } return trimmedWidth; }
Get the last non-white X point @param img Image in memory @return the trimmed width
private static int getTrimmedYStart(BufferedImage img) { int width = img.getWidth(); int height = img.getHeight(); int yStart = height; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (img.getRGB(i, j) != Color.WHITE.getRGB() && j < yStart) { yStart = j; break; } } } return yStart; }
Get the first non-white Y point @param img Image in memory @return the trimmed y start
private static int getTrimmedHeight(BufferedImage img) { int width = img.getWidth(); int height = img.getHeight(); int trimmedHeight = 0; for (int i = 0; i < width; i++) { for (int j = height - 1; j >= 0; j--) { if (img.getRGB(i, j) != Color.WHITE.getRGB() && j > trimmedHeight) { trimmedHeight = j; break; } } } return trimmedHeight; }
Get the last non-white Y point @param img Image in memory @return The trimmed height