Author: rwhitcomb Date: Fri Oct 20 17:05:37 2017 New Revision: 1812765 URL: http://svn.apache.org/viewvc?rev=1812765&view=rev Log: Add some more parameter checking methods in Utils: * Check for non-negative float values (parallel to the "int" value method already there). * Check for "positive" integer value (that is, it throws if the value is <= 0). * Update/correct some of the Javadoc.
Modified: pivot/trunk/core/src/org/apache/pivot/util/Utils.java Modified: pivot/trunk/core/src/org/apache/pivot/util/Utils.java URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/Utils.java?rev=1812765&r1=1812764&r2=1812765&view=diff ============================================================================== --- pivot/trunk/core/src/org/apache/pivot/util/Utils.java (original) +++ pivot/trunk/core/src/org/apache/pivot/util/Utils.java Fri Oct 20 17:05:37 2017 @@ -130,7 +130,7 @@ public class Utils { * * @param value The value to check. * @param argument A description for the argument, used to - * construct a message like {@code "xxx cannot be negative."}. + * construct a message like {@code "xxx must not be negative."}. * Can be {@code null} or an empty string, in which case a plain * {@link IllegalArgumentException} is thrown without any detail message. * @throws IllegalArgumentException if the value is negative. @@ -144,6 +144,50 @@ public class Utils { } } } + + /** + * Check if the input argument is negative (less than zero), and throw an + * {@link IllegalArgumentException} with or without a descriptive message, + * depending on the {@code argument} supplied. + * + * @param value The value to check. + * @param argument A description for the argument, used to + * construct a message like {@code "xxx must not be negative."}. + * Can be {@code null} or an empty string, in which case a plain + * {@link IllegalArgumentException} is thrown without any detail message. + * @throws IllegalArgumentException if the value is negative. + */ + public static void checkNonNegative(float value, String argument) { + if (value < 0.0f) { + if (isNullOrEmpty(argument)) { + throw new IllegalArgumentException(); + } else { + throw new IllegalArgumentException(argument + " must not be negative."); + } + } + } + + /** + * Check if the input argument is positive (greater than zero), and throw an + * {@link IllegalArgumentException} if not, with or without a descriptive message, + * depending on the {@code argument} supplied. + * + * @param value The value to check. + * @param argument A description for the argument, used to + * construct a message like {@code "xxx must be positive."}. + * Can be {@code null} or an empty string, in which case a plain + * {@link IllegalArgumentException} is thrown without any detail message. + * @throws IllegalArgumentException if the value is negative. + */ + public static void checkPositive(int value, String argument) { + if (value <= 0) { + if (isNullOrEmpty(argument)) { + throw new IllegalArgumentException(); + } else { + throw new IllegalArgumentException(argument + " must be positive."); + } + } + } /** * Check that the given {@code index} is between the values of {@code start} and {@code end}.