|
TUM CCSM Commons | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectedu.tum.cs.commons.options.Options
public class Options
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.
Properties
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. |
|
|
getEnumValue(java.lang.String option,
java.lang.Class<T> enumType)
Get the value for an option as instance of an enumeration. |
|
|
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. |
|
|
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 |
---|
public static final int OPTION_NOT_PRESENT
countValues
when trying to count values of a
non-present option.
Constructor Detail |
---|
public Options()
Option
object holding now options. Use
methods init(String)
or setOption(String, String)
to
store options.
Method Detail |
---|
public void init(java.lang.String filename) throws java.io.IOException
Options
object by reading a properties
file.
filename
- full-qualified name of the properties file
java.io.IOException
- Thrown if an I/O problem is encountered while reading
properties file.public void init()
Options
object. Existing options are cleared.
public boolean setOption(java.lang.String option, java.lang.String value)
option
- name of the optionvalue
- option's value, must have same format as defined in the
properties file
true
if option was alreay present,
false
otherwisepublic java.lang.String getValue(java.lang.String option)
option
- the name of the option
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.public java.lang.String getValue(java.lang.String option, java.lang.String defaultValue)
option
- name of the optiondefaultValue
- default value to use, if option is not present
public java.lang.String[] getValues(java.lang.String option)
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"
option
- name of the option
public boolean hasBooleanValue(java.lang.String option)
true
,false
,
yes
and no
option
- name of the option
true
is returned, otherwise false
public boolean getBooleanValue(java.lang.String option) throws Options.ValueConversionException
boolean
.
option
- name of the option
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.public <T extends java.lang.Enum<T>> T getEnumValue(java.lang.String option, java.lang.Class<T> enumType) throws Options.ValueConversionException
Typical usage is:
Colors color = options.getEnumValue("enum1", Colors.class);where
Colors
is an enumeration.
T
- the enumerationoption
- the name of the optionenumType
- the enumeration type
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.public <T extends java.lang.Enum<T>> T getEnumValue(java.lang.String option, T defaultValue, java.lang.Class<T> enumType)
getEnumValue(String, Class)
but allows to specify
default value.
T
- the enumerationoption
- the name of the optionenumType
- the enumeration type
public <T extends java.lang.Enum<T>> boolean hasEnumValue(java.lang.String option, java.lang.Class<T> enumType)
option
- name of the option
true
is returned, otherwise false
public int getIntValue(java.lang.String option) throws Options.ValueConversionException
int
.
option
- name of the option
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.public boolean hasIntValue(java.lang.String option)
int
value.
option
- name of the option
int
value
true
is returned, otherwise false
public boolean getBooleanValue(java.lang.String option, boolean defaultValue)
getBooleanValue(String)
but allows to specify a default
value.
option
- name of the optiondefaultValue
- default value
public int getIntValue(java.lang.String option, int defaultValue)
getIntValue(String)
but allows to specify a default
value.
option
- name of the optiondefaultValue
- default value
public boolean hasOption(java.lang.String option)
option
- name of the option
true
if option is present, false
otherwisepublic boolean hasValue(java.lang.String option)
option
- name of the option
true
if option is present and has a value,
false
otherwise (even if option is present but
doesn't have a value)public int countValues(java.lang.String option)
option
- name of the option
public java.lang.String toString()
toString
in class java.lang.Object
public float getFloatValue(java.lang.String option) throws Options.ValueConversionException
float
.
option
- name of the option
Options.ValueConversionException
- if the option doesn't have a float value.public boolean hasFloatValue(java.lang.String option)
option
- name of the option
true
is returned, otherwise false
|
TUM CCSM Commons | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |