package us.kbase.auth2.lib;

import us.kbase.auth2.lib.exceptions.IllegalParameterException;
import us.kbase.auth2.lib.exceptions.MissingParameterException;

public class Utils {

	//TODO TESTS
	//TODO JAVADOC
	
	public static void checkString(final String s, final String name)
			throws MissingParameterException {
		try {
			checkString(s, name, -1);
		} catch (IllegalParameterException e) {
			throw new RuntimeException("Programming error: " +
					e.getMessage(), e);
		}
	}
	
	public static void checkString(
			final String s,
			final String name,
			final int max)
			throws MissingParameterException, IllegalParameterException {
		if (s == null || s.trim().isEmpty()) {
			throw new MissingParameterException(name);
		}
		
		if (max > 0 && codePoints(s) > max) {
			throw new IllegalParameterException(
					name + " size greater than limit " + max);
		}
	}
	
	private static int codePoints(final String s) {
		return s.codePointCount(0, s.length());
	}
	public static void checkStringNoCheckedException(
			final String s,
			final String name) {
		if (s == null || s.trim().isEmpty()) {
			throw new IllegalArgumentException("Missing argument: " + name);
		}
	}

	// prevents overflows by returning max long if a + b > maxlong
	public static long addLong(final long a, final long b) {
		final long c;
		if (Long.MAX_VALUE - a < b) {
			c = Long.MAX_VALUE;
		} else {
			c = a + b;
		}
		return c;
	}
	
	public static void clear(final byte[] bytes) {
		for (int i = 0; i < bytes.length; i++) {
			bytes[i] = 0;
		}
	}
}

@@ -155,333 +155,1257 @@
 n;%0A%0A
-public class Utils %7B%0A%0A%09//TODO TESTS%0A%09//TODO JAVADOC%0A%09%0A%09public static void checkString(final String s, final String name)%0A%09%09%09throws MissingParameterException %7B%0A%09%09try %7B%0A%09%09%09checkString(s, name, -1);%0A%09%09%7D catch (IllegalParameterException e) %7B%0A%09%09%09throw new RuntimeException(%22Programming error: %22 +%0A%09%09%09%09%09e.getMessage(), e);%0A%09%09%7D%0A%09%7D%0A%09
+/** Miscellaneous utility functions including checking strings for existence and length,%0A * safely adding numbers, and clearing byte arrays.%0A * @author gaprice@lbl.gov%0A *%0A */%0Apublic class Utils %7B%0A%0A%09//TODO TESTS%0A%09%0A%09/** Check that a string is non-null and has at least one non-whitespace character.%0A%09 * @param s the string to check.%0A%09 * @param name the name of the string to use in any error messages.%0A%09 * @throws MissingParameterException if the string fails the check.%0A%09 */%0A%09public static void checkString(final String s, final String name)%0A%09%09%09throws MissingParameterException %7B%0A%09%09try %7B%0A%09%09%09checkString(s, name, -1);%0A%09%09%7D catch (IllegalParameterException e) %7B%0A%09%09%09throw new RuntimeException(%22Programming error: %22 +%0A%09%09%09%09%09e.getMessage(), e);%0A%09%09%7D%0A%09%7D%0A%09%0A%09/** Check that a string is non-null, has at least one non-whitespace character, and is below%0A%09 * a specified length.%0A%09 * @param s the string to check.%0A%09 * @param name the name of the string to use in any error messages.%0A%09 * @param max the maximum number of code points in the string. If 0 or less, the length is not%0A%09 * checked.%0A%09 * @throws MissingParameterException if the string is null or contains only whitespace%0A%09 * characters.%0A%09 * @throws IllegalParameterException if the string is too long.%0A%09 */
 %0A%09pu
@@ -1883,16 +1883,188 @@
 ());%0A%09%7D%0A
+%09%0A%09/** As checkString(), but doesn't throw a checked exception.%0A%09 * @param s the string to check.%0A%09 * @param name the name of the string to use in any error messages.%0A%09 */%0A
 %09public 
@@ -2272,69 +2272,192 @@
 %0A%0A%09/
-/ prevents overflows by returning max long if a + b %3E maxlong
+** Adds two longs, returning Long.MAX_VALUE if the calculation overflows.%0A%09 * @param a a long.%0A%09 * @param b a long.%0A%09 * @return Long.MAX_VALUE if a + b %3E Long.MAX_VALUE else a + b%0A%09 */
 %0A%09pu
@@ -2628,16 +2628,112 @@
 c;%0A%09%7D%0A%09%0A
+%09/** Sets all the elements of a byte array to 0.%0A%09 * @param bytes the byte array to clear.%0A%09 */%0A
 %09public 

add jdoc for utils