net.sf.xenqtt
Class XenqttUtil

java.lang.Object
  extended by net.sf.xenqtt.XenqttUtil

public final class XenqttUtil
extends Object

Provides disparate utility methods useful across the Xenqtt application ecosystem.


Method Summary
static List<String> findFilesOnClassPath(String packageName, String extension)
          Finds files on the xenqtt class path optionally limited to package and/or extension.
static File getXenqttClassPathRoot()
           
static File getXenqttInstallDirectory()
           
static boolean isBlank(String str)
          Determine if a particular string is blank.
static boolean isNull(String str)
          Determine if a particular string is null.
static String loadResourceFile(String resourceName)
          Loads the specified file as a class path resource and returns it as a string.
static void prettyPrint(String text, boolean wrap)
          Converts tabs to 4 spaces and optionally word wraps text at about 100 characters then prints to system.out.
static void prettyPrintln(String text, boolean wrap)
          Converts tabs to 4 spaces and optionally word wraps text at about 100 characters then prints to system.out and appends a newline character.
static String[] quickSplit(String value, char delimiter)
           Split a string using a single delimiter.
static
<T extends Number>
T
validateGreaterThan(String name, T value, T min)
          Validate that a number is greater than a defined minimum.
static
<T extends Number>
T
validateGreaterThanOrEqualTo(String name, T value, T min)
          Validate that a number is greater than or equal to a defined minimum.
static
<T extends Number>
T
validateInRange(String name, T value, T start, T end)
          Validate that a number falls within a specific range.
static
<T extends Number>
T
validateLessThan(String name, T value, T max)
          Validate that a number is less than a defined maximum.
static
<T extends Number>
T
validateLessThanOrEqualTo(String name, T value, T max)
          Validate that a number is less than or equal to a defined maximum.
static
<T> Collection<T>
validateNotEmpty(String name, Collection<T> value)
          Validate that an arbitrary collection of objects of type T is not empty.
static String validateNotEmpty(String name, String value)
          Validate that a string is not empty.
static
<T> T[]
validateNotEmpty(String name, T[] value)
          Validate that an arbitrary array of objects of type T is not empty.
static
<T> T
validateNotNull(String name, T value)
          Validates that an object is not null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

quickSplit

public static String[] quickSplit(String value,
                                  char delimiter)

Split a string using a single delimiter. This method is useful in cases where one needs to split a string on a single character and desires optimal performance. The String.split(String) method builds a regular expression under the covers and, as a result, performs worse than this method does for the single character use case.

With respect to this method and the given input parameters, the following holds:

Parameters:
value - The string value that should be split
delimiter - The delimiter to split the specified value on
Returns:
An array of strings derived by splitting the value on the specified delimiter

validateNotNull

public static <T> T validateNotNull(String name,
                                    T value)
Validates that an object is not null.

Parameters:
name - The name of the object being validated. Included in the exception message if one is thrown
value - The object to validate
Returns:
The specified value
Throws:
IllegalArgumentException - If value is null

validateNotEmpty

public static String validateNotEmpty(String name,
                                      String value)
Validate that a string is not empty. An empty string is either null or the empty string. Having only whitespace in the string ( \r \n \t ' ') constitutes an empty string as well.

Parameters:
name - The name of the object being validated. Included in the exception message if one is thrown
value - The string to validate
Returns:
The specified value
Throws:
IllegalArgumentException - If value is an empty string

validateNotEmpty

public static <T> T[] validateNotEmpty(String name,
                                       T[] value)
Validate that an arbitrary array of objects of type T is not empty. The array is considered empty if it is null or contains zero elements.

Parameters:
name - The name of the object being validated. Included in the exception message if one is thrown
value - The array to validate
Returns:
The specified value
Throws:
IllegalArgumentException - If value is null or contains no elements

validateNotEmpty

public static <T> Collection<T> validateNotEmpty(String name,
                                                 Collection<T> value)
Validate that an arbitrary collection of objects of type T is not empty. The collection is considered empty if it is null or contains zero elements.

Parameters:
name - The name of the object being validated. Included in the exception message if one is thrown
value - The collection to validate
Returns:
The specified value
Throws:
IllegalArgumentException - If value is null or contains no elements

validateLessThan

public static <T extends Number> T validateLessThan(String name,
                                                    T value,
                                                    T max)
Validate that a number is less than a defined maximum.

Parameters:
name - The name of the object being validated. Included in the exception message if one is thrown
value - The value of the number
max - The maximum that the specified value must be under
Returns:
The specified value
Throws:
IllegalArgumentException - If value is null or is greater than or equal to max

validateLessThanOrEqualTo

public static <T extends Number> T validateLessThanOrEqualTo(String name,
                                                             T value,
                                                             T max)
Validate that a number is less than or equal to a defined maximum.

Parameters:
name - The name of the object being validated. Included in the exception message if one is thrown
value - The value of the number
max - The maximum that the specified value must be under
Returns:
The specified value
Throws:
IllegalArgumentException - If value is null or is greater than max

validateGreaterThan

public static <T extends Number> T validateGreaterThan(String name,
                                                       T value,
                                                       T min)
Validate that a number is greater than a defined minimum.

Parameters:
name - The name of the object being validated. Included in the exception message if one is thrown
value - The value of the number
min - The minimum that the specified value must be over
Returns:
The specified value
Throws:
IllegalArgumentException - If value is null or is less than or equal to max

validateGreaterThanOrEqualTo

public static <T extends Number> T validateGreaterThanOrEqualTo(String name,
                                                                T value,
                                                                T min)
Validate that a number is greater than or equal to a defined minimum.

Parameters:
name - The name of the object being validated. Included in the exception message if one is thrown
value - The value of the number
min - The minimum that the specified value must be over
Returns:
The specified value
Throws:
IllegalArgumentException - If value is null or is less than or equal to max

validateInRange

public static <T extends Number> T validateInRange(String name,
                                                   T value,
                                                   T start,
                                                   T end)
Validate that a number falls within a specific range. The start and end of the range are inclusive.

Parameters:
name - The name of the object being validated. Included in the exception message if one is thrown
value - The value of the number
start - The start of the range
end - The end of the range
Returns:
The specified value
Throws:
IllegalArgumentException - If value is null or falls outside of the range specified by start and end

isBlank

public static boolean isBlank(String str)
Determine if a particular string is blank. A blank string is one that is either null, the empty string, or contains only whitespace characters.

Parameters:
str - The string to check
Returns:
true if str is blank, false if it is not

isNull

public static boolean isNull(String str)
Determine if a particular string is null.

Parameters:
str - The string to check
Returns:
true if str is null, false if it is not

prettyPrintln

public static void prettyPrintln(String text,
                                 boolean wrap)
Converts tabs to 4 spaces and optionally word wraps text at about 100 characters then prints to system.out and appends a newline character. Lines will be continued past the specified length to avoid breaking in the middle of a word.


loadResourceFile

public static String loadResourceFile(String resourceName)
Loads the specified file as a class path resource and returns it as a string.

Returns:
The contents of the specified resource

prettyPrint

public static void prettyPrint(String text,
                               boolean wrap)
Converts tabs to 4 spaces and optionally word wraps text at about 100 characters then prints to system.out. Lines will be continued past the specified length to avoid breaking in the middle of a word.


getXenqttClassPathRoot

public static File getXenqttClassPathRoot()
Returns:
The xenqtt class path root. If xenqtt is running from a JAR it will be the JAR. Otherwise it will be the class path directory that contains the net/sf/xenqtt package. The latter is to support running in development which typically means it will be the 'project/target/classes' directory.

getXenqttInstallDirectory

public static File getXenqttInstallDirectory()
Returns:
The directory where xenqtt is currently installed. If xenqtt is running from a JAR it will be the directory containing the JAR. Otherwise it will be the parent directory of the class path directory that contains the net/sf/xenqtt package. The latter is to support running in development which typically means it will be the 'project/target' directory.

findFilesOnClassPath

public static List<String> findFilesOnClassPath(String packageName,
                                                String extension)
Finds files on the xenqtt class path optionally limited to package and/or extension.

Parameters:
packageName - Name of package to limit the search to. Sub packages are not included. Null to include all packages.
extension - File extension to limit search to. This should include the dot as in ".class". Null to include files with any extension.
Returns:
The list of files found. Each entry will be '/' separated, relative to the class path, and will have no leading '/'.


Copyright © 2013. All Rights Reserved.