net.siefkes.nlstego.util
Class Util

java.lang.Object
  extended by net.siefkes.nlstego.util.Util

public final class Util
extends Object

A static class that provides some utility constands and methods. No instances of this class can be created, only the static members should be used.

Version:
$Revision: 1.11 $, $Date: 2005/07/12 17:02:22 $, $Author: siefkes $
Author:
Christian Siefkes

Field Summary
static char FALSE_CHAR
          Character representing the boolean value false: '-'.
static String LINE_SEPARATOR
          The line separator on the current operating system ("\n" on Unix).
static String NAN
          String recognized as Not-a-Number when parsing floating-point numbers: "NaN" (ignoring case).
static char TRUE_CHAR
          Character representing the boolean value true: '+'.
 
Method Summary
static boolean asBoolean(char character)
          Converts a character into a boolean primitive.
static boolean asBoolean(String str)
          Converts a String into a boolean primitive.
static char asChar(String str)
          Converts a String into a char primitive.
static double asDouble(String str)
          Converts a String into a double primitive using the Double.parseDouble(java.lang.String) method.
static float asFloat(String str)
          Converts a String into a float primitive using the Float.parseFloat(java.lang.String) method.
static int asInt(String str)
          Converts a String into an integer primitive using the Integer.decode(java.lang.String) method.
static long asLong(String str)
          Converts a String into a long primitive using the Long.decode(java.lang.String) method.
static short asShort(String str)
          Converts a String into a short primitive using the Short.decode(java.lang.String) method.
static List<String> configureFromArgs(Configuration config, String[] args)
          Modifies a configuration from -key[=value] pairs in a string array.
static String formatDouble(double number)
          Formats a double, using three decimal digits.
static String formatDouble(double number, int digits)
          Formats a double, using the specified number of decimal digits.
static String formatPercentage(double number)
          Formats a number as a percentage, using three decimal digits.
static String formatPercentage(double number, int digits)
          Formats a number as a percentage, using the specified number of decimal digits.
static Configuration loadConfiguration(String baseName)
          Loads a configuration in PropertiesConfiguration from the baseName.cfg in the user's home directory, if it exists and is a readable file.
static void modifyConfigProperty(Configuration config, String keyValue)
          Modifies a configuration property, parsing a key[=value] pair.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINE_SEPARATOR

public static final String LINE_SEPARATOR
The line separator on the current operating system ("\n" on Unix).


NAN

public static final String NAN
String recognized as Not-a-Number when parsing floating-point numbers: "NaN" (ignoring case).

See Also:
Constant Field Values

TRUE_CHAR

public static final char TRUE_CHAR
Character representing the boolean value true: '+'.

See Also:
Constant Field Values

FALSE_CHAR

public static final char FALSE_CHAR
Character representing the boolean value false: '-'.

See Also:
Constant Field Values
Method Detail

asBoolean

public static boolean asBoolean(char character)
                         throws IllegalArgumentException
Converts a character into a boolean primitive. TRUE_CHAR is converted to true; FALSE_CHAR is converted to false. An exception is thrown in all other cases.

Parameters:
character - the character to convert
Returns:
the converted boolean
Throws:
IllegalArgumentException - if the character cannot be converted into a boolean

asBoolean

public static boolean asBoolean(String str)
                         throws IllegalArgumentException
Converts a String into a boolean primitive. Calls BooleanUtils.toBooleanObject(java.lang.String), i.e. The strings "true", "on", and "yes" are accepted for a true value; "false", "off", and "no" are accepted for false; case and surrounding whitespace are ignored. If the string contains a single character, the result of asBoolean(char) is returned. An exception is thrown for all other strings.

If the specified String is null, false is returned.

Parameters:
str - the String to check
Returns:
the converted boolean
Throws:
IllegalArgumentException - if the String cannot be parsed as a boolean

asChar

public static char asChar(String str)
                   throws IndexOutOfBoundsException
Converts a String into a char primitive. The first non-whitespace character String is returned. An exception is thrown if the string is empty or contains only whitespace.

If the specified String is null, Character.MIN_VALUE (the smallest character value) is returned.

Parameters:
str - the String to check
Returns:
the converted char
Throws:
IndexOutOfBoundsException - if the String is empty (after trimming outer whitespace)

asDouble

public static double asDouble(String str)
                       throws NumberFormatException
Converts a String into a double primitive using the Double.parseDouble(java.lang.String) method. If the trimmed String to equal to NAN (ignoring case), Double.NaN is returned.

If the specified String is null, -1.0 is returned.

Parameters:
str - the String to check
Returns:
the converted double
Throws:
NumberFormatException - if the String does not contain a parsable double

asFloat

public static float asFloat(String str)
                     throws NumberFormatException
Converts a String into a float primitive using the Float.parseFloat(java.lang.String) method. If the trimmed String to equal to NAN (ignoring case), Float.NaN is returned.

If the specified String is null, -1 is returned.

Parameters:
str - the String to check
Returns:
the converted float
Throws:
NumberFormatException - if the String does not contain a parsable float

asInt

public static int asInt(String str)
                 throws NumberFormatException
Converts a String into an integer primitive using the Integer.decode(java.lang.String) method.

If the specified String is null, -1 is returned.

Parameters:
str - the String to check
Returns:
the converted int
Throws:
NumberFormatException - if the String does not contain a parsable int

asLong

public static long asLong(String str)
                   throws NumberFormatException
Converts a String into a long primitive using the Long.decode(java.lang.String) method.

If the specified String is null, -1 is returned.

Parameters:
str - the String to check
Returns:
the converted long
Throws:
NumberFormatException - if the String does not contain a parsable long

asShort

public static short asShort(String str)
                     throws NumberFormatException
Converts a String into a short primitive using the Short.decode(java.lang.String) method.

If the specified String is null, -1 is returned.

Parameters:
str - the String to check
Returns:
the converted short
Throws:
NumberFormatException - if the String does not contain a parsable short

configureFromArgs

public static List<String> configureFromArgs(Configuration config,
                                             String[] args)
                                      throws IllegalArgumentException
Modifies a configuration from -key[=value] pairs in a string array. Delegates to modifyConfigProperty(Configuration, String) for each array element starting with "-". Other array elements are collected and returned.

Parameters:
config - the configuration to modify
args - the array of strings to parse
Returns:
a list of all arguments that do not start with "-"
Throws:
IllegalArgumentException - if one of the [+|-]key[=value] pairs doesn't contain a key ("=" is second printable character)

formatDouble

public static String formatDouble(double number)
Formats a double, using three decimal digits.

Created (21.01.2002 12:09:23) by Christian Siefkes.

Parameters:
number - the double to format
Returns:
the formatted result

formatDouble

public static String formatDouble(double number,
                                  int digits)
Formats a double, using the specified number of decimal digits.

Created (21.01.2002 12:11:06) by Christian Siefkes.

Parameters:
number - the double to format
digits - how many decimal digits to use
Returns:
the formatted result

formatPercentage

public static String formatPercentage(double number)
Formats a number as a percentage, using three decimal digits.

Created (21.01.2002 12:09:23) by Christian Siefkes.

Parameters:
number - the double to format
Returns:
the formatted result

formatPercentage

public static String formatPercentage(double number,
                                      int digits)
Formats a number as a percentage, using the specified number of decimal digits.

Created (21.01.2002 12:11:06) by Christian Siefkes.

Parameters:
number - the double to format
digits - how many decimal digits to use
Returns:
the formatted result

loadConfiguration

public static Configuration loadConfiguration(String baseName)
                                       throws ConfigurationException
Loads a configuration in PropertiesConfiguration from the baseName.cfg in the user's home directory, if it exists and is a readable file. Otherwise an empty configuration will be returned. The file is assumed to use the platform's default character set.

Parameters:
baseName - the base name of the configuration
Returns:
the loaded configuration
Throws:
ConfigurationException - if an error occurs while loading the configuraiton

modifyConfigProperty

public static void modifyConfigProperty(Configuration config,
                                        String keyValue)
                                 throws IllegalArgumentException
Modifies a configuration property, parsing a key[=value] pair. Both key and value are trimmed. Boolean.TRUE is used as default value if the =value part is omitted. If "=" is the last character (empty value), the property is removed from the configuration (Configuration.clearProperty(java.lang.String)).

Parameters:
config - the configuration to modify
keyValue - the key=value pair to parse
Throws:
IllegalArgumentException - if keyValue doesn't contain a key ("=" is first character)


Copyright © 2003-2005 Christian Siefkes. All Rights Reserved.