The most commonly used (favorite) methods

Whenever we write code we will have certain set of code blocks – or methods written by us – which we will be using in every other method. isValidString(String aString) is my favorite method.

public static boolean isValidString(String aString)
{
    return((null != aString) && (aString.trim().length() > 0));
}

This one comes second in my list. This method is very useful if we are writing loads of conventional JSPs/Servlets.

public static String getRequestParameter(HttpServletRequest request,
		String paramName)
{
	return (getRequestParameter(request, paramName, null));
}

public static String getRequestParameter(HttpServletRequest request,
		String paramName, String defaultValue)
{
	String paramValue = null;

	if ((null != request) && isValidString(paramName))
	{
		paramValue = request.getParameter(paramName);
		paramValue = !Utility.isValidString(paramValue)
			? defaultValue : paramValue;
	}
	else
		paramValue = defaultValue;

	return (paramValue);
}

This method comes third. This method tries to implement in_array() of Php. This method checks whether the specified object is available in the given array.

public static boolean inArray(Object[] array, Object key)
{
boolean isKeyInArray = false;
if (null != array)
{
for (int i = 0; i < array.length; i++) { if (array[i].equals(key)) { isKeyInArray = true; break; } } } return (isKeyInArray); } [/sourcecode] But this was like reinventing the wheel. From Java 1.2, they had an API - java.util.Arrays - which does almost everything. I implementing it without knowing that (I am very bad).  But I love my code. So, what's your most commonly used (favorite) method?

Advertisements