qflib 0.98.1

de.qfs.lib.option
Class OptionSet

java.lang.Object
  |
  +--java.util.Observable
        |
        +--de.qfs.lib.option.OptionSet
All Implemented Interfaces:
java.lang.Cloneable, Configurable, java.util.Observer, java.io.Serializable

public class OptionSet
extends java.util.Observable
implements java.io.Serializable, java.lang.Cloneable, Configurable, java.util.Observer

An OptionSet bundles Options together, offering convenience methods for easy, typed access to their values. Default values must be provided in many cases, to avoid exception handling.

New in qflib 0.97.0:
Options from nested OptionSets can be queried and set by building a kind of Option path name with a slash '/' as separator.
Example:
If OptionSet options has a nested OptionSet in an Option named 'a', which in turn has a StringOption named 'b', then
options.getString("a/b", null)
will get the string value from the nested Option 'b'. If options contains an Option named a/b' however, it will be accessed instead.

Version:
$Revision: 1.19 $
Author:
Gregor Schmid
See Also:
Serialized Form

Field Summary
protected  java.util.Map options
          The Options of the OptionSet.
 
Constructor Summary
OptionSet(java.lang.String name)
          Create a new OptionSet.
 
Method Summary
 void add(Option option)
          Add an option to the OptionSet.
 void addAll(OptionSet set)
          Add all options from another OptionSet to the OptionSet.
 java.lang.Object clone()
          Creates a deep copy of the OptionSet.
 boolean getBoolean(java.lang.String name, boolean def)
          Get the boolean value from an Option in the OptionSet.
 java.lang.String getConfigurableName()
          Get the Configurable's name.
 java.util.Properties getConfigurableState()
          Get the Configurable's state.
 java.io.File getFile(java.lang.String name, java.io.File def)
          Get the File value from an Option in the OptionSet.
 int getInt(java.lang.String name, int def)
          Get the int value from an Option in the OptionSet.
 Option getOption(java.lang.String name)
          Get an Option from the OptionSet.
 Option[] getOptions()
          Get all Options from the OptionSet.
 java.lang.String getString(java.lang.String name, java.lang.String def)
          Get the String value from an Option in the OptionSet.
 java.lang.Object getValue(java.lang.String name, java.lang.Object def)
          Get the value from an Option in the OptionSet.
 java.lang.String listOptions()
          Print the options in the OptionSet to a string.
 void registrationChanged(Configuration config, java.lang.String name, boolean registered, boolean valuesKept)
          This callback is used to register or unregister nested OptionSets.
 void remove(java.lang.String name)
          Remove an Option from the OptionSet.
 void setConfigurableName(java.lang.String configurableName)
          Set the Configurable's name.
 void setConfigurableState(java.util.Properties state)
          Set the Configurable's state.
 void update(java.util.Observable obs, java.lang.Object arg)
          Update the OptionSet.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

options

protected transient java.util.Map options
The Options of the OptionSet.
Constructor Detail

OptionSet

public OptionSet(java.lang.String name)
Create a new OptionSet.
Parameters:
name - The name to use as a Configurable.
Method Detail

add

public void add(Option option)
Add an option to the OptionSet.
Parameters:
option - The Option to add.

addAll

public void addAll(OptionSet set)
Add all options from another OptionSet to the OptionSet.
Parameters:
set - The source of the Options to add.

remove

public void remove(java.lang.String name)
Remove an Option from the OptionSet.
Parameters:
name - The name of the Option to remove.

getOption

public Option getOption(java.lang.String name)
Get an Option from the OptionSet.
Parameters:
name - The name of the Option.
Returns:
The requested Option or null, if the name is unknown.

getOptions

public Option[] getOptions()
Get all Options from the OptionSet.
Returns:
The Options.

getValue

public java.lang.Object getValue(java.lang.String name,
                                 java.lang.Object def)
Get the value from an Option in the OptionSet.
Parameters:
name - The name of the option.
def - A default value in case the name is unknown.
Returns:
The value.

getBoolean

public boolean getBoolean(java.lang.String name,
                          boolean def)
Get the boolean value from an Option in the OptionSet.
Parameters:
name - The name of the option.
def - A default value in case the name is unknown.
Returns:
The value.

getInt

public int getInt(java.lang.String name,
                  int def)
Get the int value from an Option in the OptionSet.
Parameters:
name - The name of the option.
def - A default value in case the name is unknown.
Returns:
The value.

getString

public java.lang.String getString(java.lang.String name,
                                  java.lang.String def)
Get the String value from an Option in the OptionSet.
Parameters:
name - The name of the option.
def - A default value in case the name is unknown.
Returns:
The value.

getFile

public java.io.File getFile(java.lang.String name,
                            java.io.File def)
Get the File value from an Option in the OptionSet.
Parameters:
name - The name of the option.
def - A default value in case the name is unknown.
Returns:
The value.

getConfigurableName

public final java.lang.String getConfigurableName()
Get the Configurable's name.
Specified by:
getConfigurableName in interface Configurable
Returns:
The Configurable's name.

setConfigurableName

public final void setConfigurableName(java.lang.String configurableName)
Set the Configurable's name.
Specified by:
setConfigurableName in interface Configurable
Parameters:
name - The name to set.

getConfigurableState

public java.util.Properties getConfigurableState()
Get the Configurable's state.
Specified by:
getConfigurableState in interface Configurable
Returns:
The Configurable's state.

setConfigurableState

public void setConfigurableState(java.util.Properties state)
Set the Configurable's state.
Specified by:
setConfigurableState in interface Configurable
Parameters:
state - The state to set.

registrationChanged

public void registrationChanged(Configuration config,
                                java.lang.String name,
                                boolean registered,
                                boolean valuesKept)
This callback is used to register or unregister nested OptionSets.
Specified by:
registrationChanged in interface Configurable
Parameters:
config - The affected Configuration.
name - The registration name of the Configurable which may be different from the Configurable's name.
registered - True if the Configurable was just registered, false if it was unregistered.
valuesKept - Whether the Configurable's values were kept, if it was just unregistered.

update

public void update(java.util.Observable obs,
                   java.lang.Object arg)
Update the OptionSet. An OptionSet observes its Options.
Specified by:
update in interface java.util.Observer
Parameters:
obs - The Option that has changed.
arg - Details about the change.

clone

public java.lang.Object clone()
Creates a deep copy of the OptionSet. The Option's of the OptionSet are cloned as well, but not their values, except for NestedOptions.

A shallow copy can be obtained via

 OptionSet newSet = new OptionSet("name");
 newSet.addAll(originalSet);
 
Overrides:
clone in class java.lang.Object
Returns:
The new OptionSet.

listOptions

public java.lang.String listOptions()
Print the options in the OptionSet to a string. Useful for logging.
Returns:
A String containing all options.
Since:
0.98.0

qflib 0.98.1