org.opensha.commons.param
Class ParameterList

java.lang.Object
  extended by org.opensha.commons.param.ParameterList
All Implemented Interfaces:
Serializable, Iterable<Parameter<?>>
Direct Known Subclasses:
Site

public class ParameterList
extends Object
implements Serializable, Iterable<Parameter<?>>

Title: ParameterList

Description: List container for parameters. Specialized version of Hashtable. The keys of the hashtable are the names of the parameters. Can add specialized iterators so that it returns only specific types of paramters, i.e. return all DoubleParameters.

This class assumes that two parameters are equal if they have the same name. This implies that parameters have unique names. This must be the case for all functions below that take a String name as an argument would fail if two or more parameters have the same name.

An additional complication is that Parameters can have a constraint with a different name. To handle this a mapping has to be generated such that constraint name can be mapped back to the original parameter name. This is accomplished via a hashtable. This is only performed in the occasional case when the constraint name differs. In most cases the parameter name and constraint name will be identical. Due to uniqueness of parameter names this implies that all constraint names must be unique also, when differing from the constraint name.

Note: Many of these functions are duplicated, where one form takes a Parameter as input, and the second takes the Parameter name as a String as input. THe first case can extract the parameter name, and proxy the method call to the String name form.

4/3/2002 SWR
WARNING - This class needs a little more work and a JUnit test case. I added constraint names but didn't fully implement updating and removing parameters with differing constraint names, only implemented addParameter fully.

Version:
1.0
Author:
Steven W. Rock
See Also:
Serialized Form

Field Summary
protected static String C
          Class name for debugging.
protected  Hashtable<String,String> constraintNameMap
          Internal list of constraint name mapped to parameter name.
protected static boolean D
          If true print out debug statements.
protected  ArrayList<Parameter<?>> params
          Internal vector list of parameters.
 
Constructor Summary
ParameterList()
          no arg constructor.
 
Method Summary
 void addParameter(int index, Parameter param)
          Adds the parameter to the internal sotrage of parameters if it doesn't exist, else throws exception.
 void addParameter(Parameter param)
          Adds the parameter to the internal sotrage of parameters if it doesn't exist, else throws exception.
 void addParameterList(ParameterList list2)
          Adds all parameters of the parameterlist to this one, if the named parameter is not already in the list.
 void clear()
          Removes all parameters from the list, making it empty, ready for new parameters.
 Object clone()
          Returns a copy of this list, therefore any changes to the copy cannot affect this original list.
 int compareTo(Object obj)
          compares 2 ParameterList to see if they are equal.
 boolean containsParameter(Parameter param)
          Checks if the parameter exists in the list.
 boolean containsParameter(String paramName)
          Checks if the parameter exists in the list.
 boolean equalNames(ParameterList list)
          Returns true if all the parameters have the same names.
 boolean equals(Object obj)
          Returns true if all the parameters have the same names and values.
<T> Parameter<T>
getByIndex(Class<T> type, int index)
          Returns a Parameter at the given index, cast to the given type.
 Parameter getByIndex(int index)
          Returns the parameter at the given index
<T> Parameter<T>
getParameter(Class<T> type, String name)
          Returns a Parameter of the given name, cast to the given type.
 Parameter getParameter(String name)
          Returns parameter if exist else throws exception.
 String getParameterListMetadataString()
          this function iterates over all the parameters in the parematerList and get their metadata in the string format.
 String getParameterListMetadataString(String delimiter)
           
 String getParameterName(String name)
          Maps back the constraint name to parameter name if this is a constraint name.
 ListIterator<String> getParameterNamesIterator()
          Returns an iterator of all parameter names of the paramters in the list.
 ListIterator<Parameter<?>> getParametersIterator()
          Returns an iterator of all parameters in the list.
 String getType(String name)
          Returns parameter type of named parameter in list, if not exist throws exception.
 Object getValue(String name)
          Returns parameter contained value object if the parameter exist, else throws exception.
 boolean isEmpty()
           
 Iterator<Parameter<?>> iterator()
           
 void removeParameter(Parameter param)
          Removes parameter if it exists, else throws exception.
 void removeParameter(String name)
          Removes parameter if it exists, else throws exception.
 void replaceParameter(String parameterName, Parameter param)
          Searches for the named parameter, then replaces the parameter it is currently editing.
static boolean setParamsInListFromXML(ParameterList paramList, Element paramListEl)
           
 void setValue(String name, Object value)
          Set's a new value to a Parameter in the list if it exists, else throws exception.
 int size()
          Returns the number of parameters in the list.
 String toString()
          Prints out all parameters in this list.
 void updateParameter(Parameter param)
          Updates an existing parameter with the new value.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

C

protected static final String C
Class name for debugging.

See Also:
Constant Field Values

D

protected static final boolean D
If true print out debug statements.

See Also:
Constant Field Values

params

protected ArrayList<Parameter<?>> params
Internal vector list of parameters.


constraintNameMap

protected Hashtable<String,String> constraintNameMap
Internal list of constraint name mapped to parameter name.

Constructor Detail

ParameterList

public ParameterList()
no arg constructor. Does nothing.

Method Detail

addParameterList

public void addParameterList(ParameterList list2)
                      throws ParameterException
Adds all parameters of the parameterlist to this one, if the named parameter is not already in the list. If a named parameter exists already a Parameter Exception is thrown.

Throws:
ParameterException

addParameter

public void addParameter(Parameter param)
                  throws ParameterException
Adds the parameter to the internal sotrage of parameters if it doesn't exist, else throws exception. If the constraint has a different name from the parameter, the constraint name is mapped to the parameter name.

Throws:
ParameterException

addParameter

public void addParameter(int index,
                         Parameter param)
                  throws ParameterException
Adds the parameter to the internal sotrage of parameters if it doesn't exist, else throws exception. If the constraint has a different name from the parameter, the constraint name is mapped to the parameter name.

Throws:
ParameterException

getParameterName

public String getParameterName(String name)
Maps back the constraint name to parameter name if this is a constraint name.

Parameters:
name - The value to check if it is a constraint
Returns:
Either the unmodified name if not constraint name, else the parameter name from the mappings if this is a constraint name.

getParameter

public Parameter getParameter(String name)
                       throws ParameterException
Returns parameter if exist else throws exception.

Throws:
ParameterException

getParameter

public <T> Parameter<T> getParameter(Class<T> type,
                                     String name)
Returns a Parameter of the given name, cast to the given type. For example, getParameter(String.class, "MyStringParameter") would return a Parameter instead of a raw typed Parameter instance.

Parameters:
type -
name -
Returns:

getValue

public Object getValue(String name)
                throws ParameterException
Returns parameter contained value object if the parameter exist, else throws exception.

Throws:
ParameterException

getByIndex

public Parameter getByIndex(int index)
Returns the parameter at the given index

Parameters:
index -
Returns:

isEmpty

public boolean isEmpty()

getByIndex

public <T> Parameter<T> getByIndex(Class<T> type,
                                   int index)
Returns a Parameter at the given index, cast to the given type.

Parameters:
type -
name -
Returns:

setValue

public void setValue(String name,
                     Object value)
              throws ParameterException,
                     ConstraintException
Set's a new value to a Parameter in the list if it exists, else throws exception.

Throws:
ParameterException
ConstraintException

getType

public String getType(String name)
               throws ParameterException
Returns parameter type of named parameter in list, if not exist throws exception.

Throws:
ParameterException

containsParameter

public boolean containsParameter(Parameter param)
Checks if the parameter exists in the list. Returns true if it does, else returns false.


containsParameter

public boolean containsParameter(String paramName)
Checks if the parameter exists in the list. Returns true if it does, else returns false.


removeParameter

public void removeParameter(Parameter param)
                     throws ParameterException
Removes parameter if it exists, else throws exception.

Throws:
ParameterException

removeParameter

public void removeParameter(String name)
                     throws ParameterException
Removes parameter if it exists, else throws exception.

Throws:
ParameterException

updateParameter

public void updateParameter(Parameter param)
                     throws ParameterException
Updates an existing parameter with the new value. Throws parameter exception if parameter doesn't exist.

Throws:
ParameterException

getParametersIterator

public ListIterator<Parameter<?>> getParametersIterator()
Returns an iterator of all parameters in the list. Returns the list in the order the elements were added.


replaceParameter

public void replaceParameter(String parameterName,
                             Parameter param)
Searches for the named parameter, then replaces the parameter it is currently editing.

Parameters:
parameterName - : Name of the parameter that is being removed
param - : New parameter that is replacing the old parameter

compareTo

public int compareTo(Object obj)
compares 2 ParameterList to see if they are equal. It compares them by checking each parameter with the passed argument obj is present in the parameterList, it is compared with. It also checks if both the parameterLists have same parameters and have the same values.

Parameters:
obj - instance of ParameterList
Returns:
int 0 if both object are same else return -1

getParameterNamesIterator

public ListIterator<String> getParameterNamesIterator()
Returns an iterator of all parameter names of the paramters in the list. Returns the list in the order the elements were added.


clear

public void clear()
Removes all parameters from the list, making it empty, ready for new parameters.


size

public int size()
Returns the number of parameters in the list.


equals

public boolean equals(Object obj)
Returns true if all the parameters have the same names and values. One use will be to determine if two DisctetizedFunctions are the same, i.e. set up with the same independent parameters.

Overrides:
equals in class Object

equalNames

public boolean equalNames(ParameterList list)
Returns true if all the parameters have the same names. One use will be to determine if two DisctetizedFunctions can be plotted on the same axis, i.e. set up with the same independent parameters.


clone

public Object clone()
Returns a copy of this list, therefore any changes to the copy cannot affect this original list.

Overrides:
clone in class Object

toString

public String toString()
Prints out all parameters in this list. For debugging purposes

Overrides:
toString in class Object

getParameterListMetadataString

public String getParameterListMetadataString()
this function iterates over all the parameters in the parematerList and get their metadata in the string format.

Returns:
the metadata string for the parameterList Metadata returned from this function can only be used to set the values of the parameters, cannot be used to recreate the parameters. Note: See getParameterListMetadataXML function which returns the metadata in the XML format and can used to recreate the parameterList from scratch.

getParameterListMetadataString

public String getParameterListMetadataString(String delimiter)

iterator

public Iterator<Parameter<?>> iterator()
Specified by:
iterator in interface Iterable<Parameter<?>>

setParamsInListFromXML

public static boolean setParamsInListFromXML(ParameterList paramList,
                                             Element paramListEl)