TUM CCSM Commons

edu.tum.cs.commons.options
Class Options

java.lang.Object
  extended by edu.tum.cs.commons.options.Options

public class Options
extends java.lang.Object

This class offers a safe and flexible interface to Java properties files. Property files must follow the format for Java properties files. See Javadoc of Properties for details.

Version:
$Rev: 26268 $
Author:
Florian Deissenboeck, Axel Gerster, $Author: juergens $
See Also:
Properties
Rating:
GREEN Hash: 9DD87B7C40B4E1B84D1F15B00CA3798A

Nested Class Summary
static class Options.ValueConversionException
          Exception objects of this class are possibly returned by getBooleanValue(String)and getIntValue(String), if corresponding options don't have a boolean respectively integer value.
 
Field Summary
static int OPTION_NOT_PRESENT
          Returned by countValues when trying to count values of a non-present option.
 
Constructor Summary
Options()
          Construct a new Option object holding now options.
 
Method Summary
 int countValues(java.lang.String option)
          Count the space separated values of an option.
 boolean getBooleanValue(java.lang.String option)
          Get the value for an option as boolean.
 boolean getBooleanValue(java.lang.String option, boolean defaultValue)
          Same as getBooleanValue(String)but allows to specify a default value.
<T extends java.lang.Enum<T>>
T
getEnumValue(java.lang.String option, java.lang.Class<T> enumType)
          Get the value for an option as instance of an enumeration.
<T extends java.lang.Enum<T>>
T
getEnumValue(java.lang.String option, T defaultValue, java.lang.Class<T> enumType)
          Same as getEnumValue(String, Class) but allows to specify default value.
 float getFloatValue(java.lang.String option)
          Get the value for an option as float.
 int getIntValue(java.lang.String option)
          Get the value for an option as int.
 int getIntValue(java.lang.String option, int defaultValue)
          Same as getIntValue(String)but allows to specify a default value.
 java.lang.String getValue(java.lang.String option)
          Gets the value for a specified option.
 java.lang.String getValue(java.lang.String option, java.lang.String defaultValue)
          Return the value for a specified option or a default value if option is not present.
 java.lang.String[] getValues(java.lang.String option)
          Returns the space separated value of an option as array.
 boolean hasBooleanValue(java.lang.String option)
          Checks if the specified option is present and has a boolean value.
<T extends java.lang.Enum<T>>
boolean
hasEnumValue(java.lang.String option, java.lang.Class<T> enumType)
          Checks if the specified option is present and has a legal value.
 boolean hasFloatValue(java.lang.String option)
          Checks if the specified option is present and has a float value.
 boolean hasIntValue(java.lang.String option)
          Checks if the specified option is present and has a int value.
 boolean hasOption(java.lang.String option)
          Checks if a specified option is present.
 boolean hasValue(java.lang.String option)
          Checks if specified option has a value.
 void init()
          Init empty Options object.
 void init(java.lang.String filename)
          This initalizes the Options object by reading a properties file.
 boolean setOption(java.lang.String option, java.lang.String value)
          Sets and option.
 java.lang.String toString()
          Returns a list with key-value-pairs as string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

OPTION_NOT_PRESENT

public static final int OPTION_NOT_PRESENT
Returned by countValues when trying to count values of a non-present option.

See Also:
Constant Field Values
Constructor Detail

Options

public Options()
Construct a new Option object holding now options. Use methods init(String)or setOption(String, String)to store options.

Method Detail

init

public void init(java.lang.String filename)
          throws java.io.IOException
This initalizes the Options object by reading a properties file.

Parameters:
filename - full-qualified name of the properties file
Throws:
java.io.IOException - Thrown if an I/O problem is encountered while reading properties file.

init

public void init()
Init empty Options object. Existing options are cleared.


setOption

public boolean setOption(java.lang.String option,
                         java.lang.String value)
Sets and option. Setting an already existing option overwrites current value.

Parameters:
option - name of the option
value - option's value, must have same format as defined in the properties file
Returns:
true if option was alreay present, false otherwise

getValue

public java.lang.String getValue(java.lang.String option)
Gets the value for a specified option.

Parameters:
option - the name of the option
Returns:
the option's value, if the option is not present or has a null value null is returned. If the option has a space separated value list, the whole list is returned. Use getValues(String)to access single values.

getValue

public java.lang.String getValue(java.lang.String option,
                                 java.lang.String defaultValue)
Return the value for a specified option or a default value if option is not present.

Parameters:
option - name of the option
defaultValue - default value to use, if option is not present
Returns:
the option's value or the default value

getValues

public java.lang.String[] getValues(java.lang.String option)
Returns the space separated value of an option as array. A option might have more the one space separated value. This method returns them as an array. To allow values containing spaces use double quotes.

Example: For the following line in a properties file

option=value1 value2 "value 3" value4

the method returns this array

a[0] = "value1"
a[1] = "value2"
a[2] = "value 3"
a[3] = "value4"

Parameters:
option - name of the option
Returns:
the array as desribed above

hasBooleanValue

public boolean hasBooleanValue(java.lang.String option)
Checks if the specified option is present and has a boolean value. Boolean values are true,false, yes and no

Parameters:
option - name of the option
Returns:
if the option is present and has a boolean value true is returned, otherwise false

getBooleanValue

public boolean getBooleanValue(java.lang.String option)
                        throws Options.ValueConversionException
Get the value for an option as boolean.

Parameters:
option - name of the option
Returns:
the value of this option
Throws:
Options.ValueConversionException - if the option doesn't have a boolean value. Use hasBooleanValue(String)method or default value enabled version getBooleanValue(String, boolean)of this method to avoid conversion problems.

getEnumValue

public <T extends java.lang.Enum<T>> T getEnumValue(java.lang.String option,
                                                    java.lang.Class<T> enumType)
                                         throws Options.ValueConversionException
Get the value for an option as instance of an enumeration. Enumeration names are matched in non case-sensitive way. Dashes in values are replaced by underscores.

Typical usage is:

 Colors color = options.getEnumValue("enum1", Colors.class);
 
where Colors is an enumeration.

Type Parameters:
T - the enumeration
Parameters:
option - the name of the option
enumType - the enumeration type
Returns:
the enumeration entry
Throws:
Options.ValueConversionException - if the option doesn't have a value of the specified enumeration. Use hasEnumValue(String, Class)method or default value enabled version getEnumValue(String, Enum, Class)of this method to avoid conversion problems.

getEnumValue

public <T extends java.lang.Enum<T>> T getEnumValue(java.lang.String option,
                                                    T defaultValue,
                                                    java.lang.Class<T> enumType)
Same as getEnumValue(String, Class) but allows to specify default value.

Type Parameters:
T - the enumeration
Parameters:
option - the name of the option
enumType - the enumeration type
Returns:
the enumeration entry

hasEnumValue

public <T extends java.lang.Enum<T>> boolean hasEnumValue(java.lang.String option,
                                                          java.lang.Class<T> enumType)
Checks if the specified option is present and has a legal value.

Parameters:
option - name of the option
Returns:
if the option is present and has a legal value true is returned, otherwise false

getIntValue

public int getIntValue(java.lang.String option)
                throws Options.ValueConversionException
Get the value for an option as int.

Parameters:
option - name of the option
Returns:
the value of this option
Throws:
Options.ValueConversionException
Options.ValueConversionException - if the option doesn't have a int value. Use hasIntValue(String)method or default value enabled version getIntValue(String, int)of this method to avoid conversion problems.

hasIntValue

public boolean hasIntValue(java.lang.String option)
Checks if the specified option is present and has a int value.

Parameters:
option - name of the option
Returns:
if the option is present and has a int value true is returned, otherwise false

getBooleanValue

public boolean getBooleanValue(java.lang.String option,
                               boolean defaultValue)
Same as getBooleanValue(String)but allows to specify a default value.

Parameters:
option - name of the option
defaultValue - default value
Returns:
return the value of the option if option is present and has a boolean value, otherwise the default value is returned

getIntValue

public int getIntValue(java.lang.String option,
                       int defaultValue)
Same as getIntValue(String)but allows to specify a default value.

Parameters:
option - name of the option
defaultValue - default value
Returns:
return the value of the option if option is present and has an integer value, otherwise the default value is returned

hasOption

public boolean hasOption(java.lang.String option)
Checks if a specified option is present.

Parameters:
option - name of the option
Returns:
true if option is present, false otherwise

hasValue

public boolean hasValue(java.lang.String option)
Checks if specified option has a value.

Parameters:
option - name of the option
Returns:
true if option is present and has a value, false otherwise (even if option is present but doesn't have a value)

countValues

public int countValues(java.lang.String option)
Count the space separated values of an option. Double quotes are taken into account.

Parameters:
option - name of the option
Returns:
value count

toString

public java.lang.String toString()
Returns a list with key-value-pairs as string.

Overrides:
toString in class java.lang.Object
Returns:
key-value-pairs as string

getFloatValue

public float getFloatValue(java.lang.String option)
                    throws Options.ValueConversionException
Get the value for an option as float.

Parameters:
option - name of the option
Returns:
the value of this option
Throws:
Options.ValueConversionException - if the option doesn't have a float value.

hasFloatValue

public boolean hasFloatValue(java.lang.String option)
Checks if the specified option is present and has a float value.

Parameters:
option - name of the option
Returns:
if the option is present and has a float value true is returned, otherwise false

TUM CCSM Commons

TUM CCSM Commons - 2.7