com.sonalb
Class Utils

java.lang.Object
  extended by com.sonalb.Utils

public abstract class Utils
extends java.lang.Object

Utility class containing several general-purpose methods for system-wide consumption.

Author:
Sonal Bansal

Method Summary
static java.lang.String commaFormatInteger(int i)
          Converts the input integer into a more readable comma-separated String.
static java.lang.String convertToHttpDate(java.util.Date d)
          Converts the input Date into a String formatted as specified by RFC 1123 of the IETF.
static int countInstances(java.lang.String s, char c)
          Counts the number of times the specified character appears in the input String.
static int countInstances(java.lang.String s, java.lang.String c)
          Counts the number of times the specified String appears in the input String.
static java.lang.String[] csvStringToArray(java.lang.String s)
          Tokenizes the argument String into several constituent Strings, separated by commas, and returns the tokens as an array.
static java.lang.String[] delimitedStringToArray(java.lang.String s, java.lang.String delimiters)
          Tokenizes the argument String into several constituent Strings, separated by specified delimiters, and returns the tokens as an array.
static java.lang.Object findMax(java.lang.Comparable[] inputlist, int n)
          Returns the nth largest object in the input array.
static int findMax(int[] inputlist, int n)
          Returns the nth largest number in the input array.
static java.lang.Object findMax(java.lang.Object[] inputlist, int n, java.util.Comparator c)
          Returns the nth largest object in the input array.
static java.lang.Object findMin(java.lang.Comparable[] inputlist, int n)
          Returns the nth smallest object in the input array.
static int findMin(int[] inputlist, int n)
          Returns the nth smallest number in the input array.
static java.lang.Object findMin(java.lang.Object[] inputlist, int n, java.util.Comparator c)
          Returns the nth smallest object in the input array.
static java.lang.Object findOrderedMax(java.lang.Comparable[] inputlist, int n)
          Returns the object which would be in the nth place if the input array were sorted in descending order.
static int findOrderedMax(int[] inputlist, int n)
          Returns the number which would be in the nth place if the input array were sorted in descending order.
static java.lang.Object findOrderedMax(java.lang.Object[] inputlist, int n, java.util.Comparator c)
          Returns the object which would be in the nth place if the input array were sorted in descending order.
static java.lang.Object findOrderedMin(java.lang.Comparable[] inputlist, int n)
          Returns the object which would be in the nth place if the input array were sorted in ascending order.
static int findOrderedMin(int[] inputlist, int n)
          Returns the number which would be in the nth place if the input array were sorted in ascending order.
static java.lang.Object findOrderedMin(java.lang.Object[] inputlist, int n, java.util.Comparator c)
          Returns the object which would be in the nth place if the input array were sorted in ascending order.
static boolean isEmpty(java.lang.String s)
          Returns true if argument is either null or it equals("").
static boolean isInArray(java.lang.Object obj, java.lang.Object[] array)
          Checks whether the specified Object exists in the given array.
static boolean isIPAddress(java.lang.String s)
          Checks whether the input String is a valid IP address (quad).
static boolean isNullOrWhiteSpace(java.lang.String s)
          Returns true if argument is either null or is full of whitespace.
static boolean isQuoted(java.lang.String s)
          Determines whether the input String is enclosed in double-quotes.
static boolean matchQuotes(java.lang.String s)
          Determines whether every double-quote has its closing partner in the input String.
static java.util.Date parseHttpDateStringToDate(java.lang.String date)
          Parses the input date String into a Date object.
static java.lang.String replaceAll(java.lang.String source, java.lang.String find, java.lang.String replace, boolean bIgnoreCase)
          Replaces all occurrences of a String in the input String with another String.
static java.lang.String stripQuotes(java.lang.String s)
          Removes the outermost double-quotes pair from the input String.
static java.lang.String trimLeftWS(java.lang.String s)
          Returns the String obtained by removing any whitespace at the left hand side of the argument.
static java.lang.String trimRightWS(java.lang.String s)
          Returns the String obtained by removing any whitespace at the right hand side of the argument.
static java.lang.String trimWhitespace(java.lang.String s)
          Returns the String obtained by removing any whitespace at both ends of the argument.
static java.lang.String trimWS(java.lang.String s)
          Convenience synonym for trimWhitespace(String).
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isNullOrWhiteSpace

public static boolean isNullOrWhiteSpace(java.lang.String s)
Returns true if argument is either null or is full of whitespace. The argument is full of whitespace if after calling String.trim() on it, it equals(""). In other words, whitespace determination is done by String.trim().

Parameters:
s - the String to be tested.
See Also:
String.trim()

trimWhitespace

public static java.lang.String trimWhitespace(java.lang.String s)
Returns the String obtained by removing any whitespace at both ends of the argument. Whitespace is defined as stated by Character.isWhitespace(char). It is different from String.trim() which also removes any control characters.

Parameters:
s - the String to be trimmed.
Returns:
the trimmed String.
See Also:
Character.isWhitespace(char), String.trim()

trimLeftWS

public static java.lang.String trimLeftWS(java.lang.String s)
Returns the String obtained by removing any whitespace at the left hand side of the argument. Whitespace is defined as stated by Character.isWhitespace(char).

Parameters:
s - the String to be trimmed.
Returns:
the trimmed String.
See Also:
Character.isWhitespace(char)

trimRightWS

public static java.lang.String trimRightWS(java.lang.String s)
Returns the String obtained by removing any whitespace at the right hand side of the argument. Whitespace is defined as stated by Character.isWhitespace(char).

Parameters:
s - the String to be trimmed.
Returns:
the trimmed String.
See Also:
Character.isWhitespace(char)

trimWS

public static java.lang.String trimWS(java.lang.String s)
Convenience synonym for trimWhitespace(String).

Parameters:
s - the String to be trimmed.
Returns:
the trimmed String.
See Also:
trimWhitespace(String)

csvStringToArray

public static java.lang.String[] csvStringToArray(java.lang.String s)
Tokenizes the argument String into several constituent Strings, separated by commas, and returns the tokens as an array.

Parameters:
s - the String to be tokenized.
Returns:
the array of token Strings.
See Also:
delimitedStringToArray(String, String)

delimitedStringToArray

public static java.lang.String[] delimitedStringToArray(java.lang.String s,
                                                        java.lang.String delimiters)
Tokenizes the argument String into several constituent Strings, separated by specified delimiters, and returns the tokens as an array. Every character of the delimiter String is treated as a token-separator individually.

Parameters:
s - the String to be tokenized.
delimiters - the String of delimiters.
Returns:
the array of token Strings.

isInArray

public static boolean isInArray(java.lang.Object obj,
                                java.lang.Object[] array)
Checks whether the specified Object exists in the given array. The Object.equals(Object) method is used to test for equality.

Parameters:
obj - the Object to be searched for.
array - the array which has to be searched.
Returns:
true if the Object exists in the array; false if the Object does not exist in the array, or the object, or the array is null.
See Also:
Object.equals(Object)

countInstances

public static int countInstances(java.lang.String s,
                                 char c)
Counts the number of times the specified character appears in the input String. For example, countInstances("abcabcdabcde",'a') returns 3.

Parameters:
s - the String to be counted in.
c - the character to be counted.
Returns:
the number of occurrences of c in s ; -1 if s is null.

countInstances

public static int countInstances(java.lang.String s,
                                 java.lang.String c)
Counts the number of times the specified String appears in the input String. For example, countInstances("abcabcdabcde","cd") returns 2.

Parameters:
s - the String to be counted in.
c - the String to be counted.
Returns:
the number of occurrences of c in s ; -1 if s or c is null.

isIPAddress

public static boolean isIPAddress(java.lang.String s)
Checks whether the input String is a valid IP address (quad).

Parameters:
s - the String to be checked.
Returns:
true if the String IS an IP address ; false if it is not, or s is null.
See Also:
InetAddress

convertToHttpDate

public static java.lang.String convertToHttpDate(java.util.Date d)
Converts the input Date into a String formatted as specified by RFC 1123 of the IETF. The output String is in the format "Sun, 06 Nov 1994 08:49:37 GMT".

Parameters:
d - the Date to be converted.
Returns:
the String representation of the date as specified by RFC1123 ; null if input is null.
See Also:
Date

parseHttpDateStringToDate

public static java.util.Date parseHttpDateStringToDate(java.lang.String date)
Parses the input date String into a Date object. The input date String is expected to be in one of the following formats (note the spaces) :-

Parameters:
date - the HTTP date String to be converted.
Returns:
the parsed Date object ; null if parsing was unsuccessful or input was null.

isEmpty

public static boolean isEmpty(java.lang.String s)
Returns true if argument is either null or it equals(""). It does not trim the input, unlike isNullOrWhiteSpace(String).

Parameters:
s - the String to be tested.
See Also:
isNullOrWhiteSpace(String)

replaceAll

public static java.lang.String replaceAll(java.lang.String source,
                                          java.lang.String find,
                                          java.lang.String replace,
                                          boolean bIgnoreCase)
                                   throws java.lang.IllegalArgumentException
Replaces all occurrences of a String in the input String with another String.

Parameters:
source - the String in which the replacements are to be carried out.
find - the String which must be searched for.
replace - the String which replaces the search String.
bIgnoreCase - determines whether case-sensitive search is done.
Returns:
the processed String if any replacements were made ; the original String if search String was not found or search String is empty.
Throws:
java.lang.IllegalArgumentException - Thrown if source String is empty.

matchQuotes

public static boolean matchQuotes(java.lang.String s)
Determines whether every double-quote has its closing partner in the input String.

Parameters:
s - the String to be tested.
Returns:
true if quotes match or no quotes exist ; false otherwise.

stripQuotes

public static java.lang.String stripQuotes(java.lang.String s)
Removes the outermost double-quotes pair from the input String. Trims the input String before processing.

Parameters:
s - the String to be stripped.
Returns:
the stripped String ; the original String if it is not quoted or quotes don't match.

isQuoted

public static boolean isQuoted(java.lang.String s)
Determines whether the input String is enclosed in double-quotes. Trims the input String first.

Parameters:
s - the String to be tested.
Returns:
true if input String is quoted ; false if it isn't or it is empty.
See Also:
trimWhitespace(String)

commaFormatInteger

public static java.lang.String commaFormatInteger(int i)
Converts the input integer into a more readable comma-separated String. A comma is inserted after every three digits, starting from the unit's place. For example,

        12345           is converted to         12,345
        987654321       is converted to         987,654,321
 

Parameters:
i - the integer to be converted.
Returns:
the String representation of the formatted integer ; if the input lies between -1000 & 1000 (not inclusive) , the returned String is the same as String.valueOf(int)
See Also:
String.valueOf(int)

findOrderedMax

public static int findOrderedMax(int[] inputlist,
                                 int n)
Returns the number which would be in the nth place if the input array were sorted in descending order. For arrays with distinct elements, this returns the nth largest number. For example, in the array {1,2,3,4,5,6}, findOrderedMax(list, 3) would return 4. However, the result is quite different in arrays with repeated elements. For example, in the array {1,2,6,2}, both findOrderedMax(list,2) and findOrderedMax(list,3) return 2.

Parameters:
inputlist - the array of numbers.
n - the desired position of sorted array. For example, 1 indicates first from top of sorted array; 2 indicates second from top, and so on.
Throws:
java.lang.IllegalArgumentException - Thrown when the number of elements in the input array is less than the desired order, that is, when n exceeds inputlist.length. Or, when the order n is less than or equal to 0.
See Also:
findMax(int[],int)

findMax

public static int findMax(int[] inputlist,
                          int n)
Returns the nth largest number in the input array. This method disregards duplicate elements unlike the findOrderedMax method. For example, in the array {1,2,3,4,5,6}, findMax(list, 3) would return 4. In the array {1,2,6,2}, findMax(list,2) would return 2, and and findMax(list,3) would return 1.

Note that in an array with repeating elements, it is possible that the required order may not exist. For example, in the array {1,2,2}, findMax(list,3), there is no third-largest number. In such cases, this method returns the number with highest order less than or equal to n. In other words, in the above example, this method would return 1, which is the same value as would be returned by findMax(list,2).

Parameters:
inputlist - the array of numbers.
n - the desired order. For example, 1 indicates largest number; 2 indicates second-largest number, and so on.
Throws:
java.lang.IllegalArgumentException - Thrown when the number of elements in the input array is less than the desired order, that is, when n exceeds inputlist.length. Or, when the order n is less than or equal to 0.
See Also:
findOrderedMax(int[],int)

findOrderedMin

public static int findOrderedMin(int[] inputlist,
                                 int n)
Returns the number which would be in the nth place if the input array were sorted in ascending order. For arrays with distinct elements, this returns the nth smallest number. For example, in the array {1,2,3,4,5,6}, findOrderedMin(list, 3) would return 3. However, the result is quite different in arrays with repeated elements. For example, in the array {1,2,6,2}, both findOrderedMin(list,2) and findOrderedMin(list,3) return 2.

Parameters:
inputlist - the array of numbers.
n - the desired position of sorted array. For example, 1 indicates first from top of sorted array; 2 indicates second from top, and so on.
Throws:
java.lang.IllegalArgumentException - Thrown when the number of elements in the input array is less than the desired order, that is, when n exceeds inputlist.length. Or, when the order n is less than or equal to 0.
See Also:
findMin(int[],int)

findMin

public static int findMin(int[] inputlist,
                          int n)
Returns the nth smallest number in the input array. This method disregards duplicate elements unlike the findOrderedMin method. For example, in the array {1,2,3,4,5,6}, findMin(list, 3) would return 3. In the array {1,2,6,2}, findMin(list,2) would return 2, and and findMin(list,3) would return 6.

Note that in an array with repeating elements, it is possible that the required order may not exist. For example, in the array {1,2,2}, findMin(list,3), there is no third-smallest number. In such cases, this method returns the number with highest order less than or equal to n. In other words, in the above example, this method would return 2, which is the same value as would be returned by findMax(list,2).

Parameters:
inputlist - the array of numbers.
n - the desired order. For example, 1 indicates smallest number; 2 indicates second-smallest number, and so on.
Throws:
java.lang.IllegalArgumentException - Thrown when the number of elements in the input array is less than the desired order, that is, when n exceeds inputlist.length. Or, when the order n is less than or equal to 0.
See Also:
findOrderedMin(int[],int)

findOrderedMax

public static java.lang.Object findOrderedMax(java.lang.Comparable[] inputlist,
                                              int n)
Returns the object which would be in the nth place if the input array were sorted in descending order. This method allows comparison of Objects. It follows similar semantics as the findOrderedMax(int[], int) method.

Parameters:
inputlist - the array of Comparable objects. All elements must be mutually Comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
n - the desired position of sorted array. For example, 1 indicates first from top of sorted array; 2 indicates second from top, and so on.
Throws:
java.lang.IllegalArgumentException - Thrown when the number of elements in the input array is less than the desired order, that is, when n exceeds inputlist.length. Or, when the order n is less than or equal to 0.
See Also:
findMax(Comparable[],int), findOrderedMax(int[],int)

findMax

public static java.lang.Object findMax(java.lang.Comparable[] inputlist,
                                       int n)
Returns the nth largest object in the input array. This method allows comparison of Objects. It follows similar semantics as the findMax(int[], int) method.

Parameters:
inputlist - the array of Comparable objects. All elements must be mutually Comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
n - the desired order. For example, 1 indicates largest object; 2 indicates second-largest object, and so on.
Throws:
java.lang.IllegalArgumentException - Thrown when the number of elements in the input array is less than the desired order, that is, when n exceeds inputlist.length. Or, when the order n is less than or equal to 0.
See Also:
findOrderedMax(Comparable[],int), findMax(int[],int)

findOrderedMin

public static java.lang.Object findOrderedMin(java.lang.Comparable[] inputlist,
                                              int n)
Returns the object which would be in the nth place if the input array were sorted in ascending order. This method allows comparison of Objects. It follows similar semantics as the findOrderedMin(int[], int) method.

Parameters:
inputlist - the array of Comparable objects. All elements must be mutually Comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
n - the desired position of sorted array. For example, 1 indicates first from top of sorted array; 2 indicates second from top, and so on.
Throws:
java.lang.IllegalArgumentException - Thrown when the number of elements in the input array is less than the desired order, that is, when n exceeds inputlist.length. Or, when the order n is less than or equal to 0.
See Also:
findMin(Comparable[],int), findOrderedMin(int[],int)

findMin

public static java.lang.Object findMin(java.lang.Comparable[] inputlist,
                                       int n)
Returns the nth smallest object in the input array. This method allows comparison of Objects. It follows similar semantics as the findMin(int[], int) method.

Parameters:
inputlist - the array of Comparable objects. All elements must be mutually Comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
n - the desired order. For example, 1 indicates smallest object; 2 indicates second-smallest object, and so on.
Throws:
java.lang.IllegalArgumentException - Thrown when the number of elements in the input array is less than the desired order, that is, when n exceeds inputlist.length. Or, when the order n is less than or equal to 0.
See Also:
findOrderedMin(Comparable[],int), findMin(int[],int)

findOrderedMax

public static java.lang.Object findOrderedMax(java.lang.Object[] inputlist,
                                              int n,
                                              java.util.Comparator c)
Returns the object which would be in the nth place if the input array were sorted in descending order. This method allows comparison of Objects. The comparison logic is provided by the Comparator argument. It follows similar semantics as the findOrderedMax(int[], int) method.

Parameters:
c - the Comparator that provides the ordering logic.
inputlist - the array of Objects.
n - the desired position of sorted array. For example, 1 indicates first from top of sorted array; 2 indicates second from top, and so on.
Throws:
java.lang.IllegalArgumentException - Thrown when the number of elements in the input array is less than the desired order, that is, when n exceeds inputlist.length. Or, when the order n is less than or equal to 0.
See Also:
findMax(Object[],int,Comparator), findOrderedMax(int[],int)

findMax

public static java.lang.Object findMax(java.lang.Object[] inputlist,
                                       int n,
                                       java.util.Comparator c)
Returns the nth largest object in the input array. This method allows comparison of Objects. The comparison logic is provided by the Comparator argument. It follows similar semantics as the findMax(int[], int) method.

Parameters:
c - the Comparator that provides the ordering logic.
inputlist - the array of Objects.
n - the desired order. For example, 1 indicates largest object; 2 indicates second-largest object, and so on.
Throws:
java.lang.IllegalArgumentException - Thrown when the number of elements in the input array is less than the desired order, that is, when n exceeds inputlist.length. Or, when the order n is less than or equal to 0.
See Also:
findOrderedMax(Object[],int,Comparator), findMax(int[],int)

findOrderedMin

public static java.lang.Object findOrderedMin(java.lang.Object[] inputlist,
                                              int n,
                                              java.util.Comparator c)
Returns the object which would be in the nth place if the input array were sorted in ascending order. This method allows comparison of Objects. The comparison logic is provided by the Comparator argument. It follows similar semantics as the findOrderedMin(int[], int) method.

Parameters:
c - the Comparator that provides the ordering logic.
inputlist - the array of Objects.
n - the desired position of sorted array. For example, 1 indicates first from top of sorted array; 2 indicates second from top, and so on.
Throws:
java.lang.IllegalArgumentException - Thrown when the number of elements in the input array is less than the desired order, that is, when n exceeds inputlist.length. Or, when the order n is less than or equal to 0.
See Also:
findMin(Object[],int,Comparator), findOrderedMin(int[],int)

findMin

public static java.lang.Object findMin(java.lang.Object[] inputlist,
                                       int n,
                                       java.util.Comparator c)
Returns the nth smallest object in the input array. This method allows comparison of Objects. The comparison logic is provided by the Comparator argument. It follows similar semantics as the findMin(int[], int) method.

Parameters:
c - the Comparator that provides the ordering logic.
inputlist - the array of Objects.
n - the desired order. For example, 1 indicates smallest object; 2 indicates second-smallest object, and so on.
Throws:
java.lang.IllegalArgumentException - Thrown when the number of elements in the input array is less than the desired order, that is, when n exceeds inputlist.length. Or, when the order n is less than or equal to 0.
See Also:
findOrderedMin(Object[],int,Comparator), findMin(int[],int)