package us.kbase.common.utils;

/**
 * Miscellaneous methods for dealing with strings.
 * 
 * @author gaprice@lbl.gov
 *
 */
public class StringUtils {
	
	/**
	 * Checks that a string is neither null or empty.
	 * @param s the string to check.
	 * @param sname the name of the variable. This name will be used in the 
	 * thrown exception.
	 * @throws IllegalArgumentException if the string is null or empty.
	 */
	public static void checkString(final String s, final String sname) {
		checkString(s, sname, -1);
	}
	
	/**
	 * Checks that a string is neither null or empty and is below a maximum
	 * length.
	 * @param s the string to check.
	 * @param sname the name of the variable. This name will be used in the 
	 * thrown exception.
	 * @param length the maximum allowed length of the string. If 0 or less,
	 * no checking is performed.
	 * @throws IllegalArgumentException if the string is null, empty, or
	 * longer than the maximum length allowed.
	 */
	public static void checkString(final String s, final String sname,
			final int length) {
		if (s == null || s.isEmpty()) {
			throw new IllegalArgumentException(sname + 
					" cannot be null or the empty string");
		}
		if (length > 0 && s.length() > length) {
			throw new IllegalArgumentException(sname + 
					" exceeds the maximum length of " + length);
		}
	}
	
	/**
	 * Checks that a string is below a maximum length. Null or empty strings
	 * are allowed.
	 * @param s the string to check.
	 * @param sname the name of the variable. This name will be used in the 
	 * thrown exception.
	 * @param length the maximum allowed length of the string.
	 * @throws IllegalArgumentException if the string is longer than the
	 * maximum length allowed.
	 */
	public static void checkMaxLen(final String s, final String sname,
			final int length) {
		if (s != null) {
			if (s.length() > length) {
				throw new IllegalArgumentException(sname + 
						" exceeds the maximum length of " + length);
			}
		}
	}
}

@@ -1964,10 +1964,793 @@
 %0A%09%09%7D%0A%09%7D%0A
+%09%0A%09/**%0A%09 * Checks that a string is below a maximum length and not empty. Null%0A%09 * strings are allowed.%0A%09 * @param s the string to check.%0A%09 * @param sname the name of the variable. This name will be used in the %0A%09 * thrown exception.%0A%09 * @param length the maximum allowed length of the string.%0A%09 * @throws IllegalArgumentException if the string is longer than the%0A%09 * maximum length allowed or is empty.%0A%09 */%0A%09public static void checkMaxLenAndNonEmpty(final String s,%0A%09%09%09final String sname, final int length) %7B%0A%09%09if (s != null) %7B%0A%09%09%09if (s.isEmpty()) %7B%0A%09%09%09%09throw new IllegalArgumentException(sname + %0A%09%09%09%09%09%09%22 cannot be the empty string%22);%0A%09%09%09%7D%0A%09%09%09if (s.length() %3E length) %7B%0A%09%09%09%09throw new IllegalArgumentException(sname + %0A%09%09%09%09%09%09%22 exceeds the maximum length of %22 + length);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A
 %7D%0A

Add string checking fn