org.mmbase.util.functions
Class Parameters

java.lang.Object
  extended byjava.util.AbstractCollection
      extended byjava.util.AbstractList
          extended byorg.mmbase.util.functions.Parameters
All Implemented Interfaces:
Collection, List, Serializable
Direct Known Subclasses:
AutodefiningParameters

public class Parameters
extends AbstractList
implements Serializable

This class implemements the Parameters interface. It provides a convenient way to create a List that allows the use of 'named parameters'. This List is therefore backed by a HashMap, but it behaves as a list. So if you set a parameter with a certain name, it always appears in the same location of the List. This List is modifiable but not resizeable. It is always the size of the definition array.

Since:
MMBase-1.7
Version:
$Id: Parameters.java,v 1.23 2006/03/23 19:55:06 michiel Exp $
Author:
Michiel Meeuwissen
See Also:
Parameter, Parameters(Parameter[]), Serialized Form

Field Summary
protected  boolean autoCasting
          If true, values are automatically cast to the right type (if possible) when set.
protected  Map backing
          The contents of this List are stored in this HashMap.
protected  Parameter[] definition
          This array maps integers (position in array) to map keys, making it possible to implement List.
static Parameters VOID
          No need to bother for the functions with no parameters.
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
  Parameters(Parameter[] def)
          Constructor, taking an Parameter[] array argument.
  Parameters(Parameter[] def, Collection values)
          If you happen to have a List of parameters, then you can wrap it into an Parameters with this constructor.
protected Parameters(Parameters params, int from, int to)
          Used for nicer implemenation of subList (which we want to also be instanceof Parameters).
 
Method Summary
 void checkRequiredParameters()
          Throws an IllegalArgumentException if one of the required parameters was not entered.
 boolean containsParameter(Parameter parameter)
          Checks wether a certain parameter is available, using the Parameter as a qualifier.
 boolean containsParameter(String parameterName)
          Checks wether a certain parameter is available, using the parameter name as a qualifier.
 Object get(int i)
           
 Object get(Parameter parameter)
          Gets the value of a parameter.
 Object get(String parameterName)
          Gets the value of a parameter.
 Parameter[] getDefinition()
           
 String getString(Parameter parameter)
          Gets the value of a parameter, cast to a String.
 String getString(String parameterName)
          Gets the value of a parameter, cast to a String.
 int indexOfParameter(Parameter parameter)
          Returns the position of a parameter in the parameters list, using the Parameter as a qualifier.
 int indexOfParameter(String parameterName)
          Returns the position of a parameter in the parameters list, using the parameter name as a qualifier.
 boolean isAutoCasting()
          Whether this Parameters object is 'automaticly casting'.
 Object set(int i, Object value)
           
 Parameters set(Parameter parameter, Object value)
          Sets the value of a parameter.
 Parameters set(String parameterName, Object value)
          Sets the value of a parameter.
 Parameters setAll(Collection values)
          Copies all values of a collection to the corresponding values of this Parameters Object.
 Parameters setAll(Map map)
          Copies all values of a map to the corresponding values of this Parameters Object.
 void setAutoCasting(boolean autocast)
          Sets the 'auto casting' property (which on default is false)
 Parameters setIfDefined(Parameter parameter, Object value)
          Sets the value of an argument, if the argument is defined, otherwise do nothing.
 Parameters setIfDefined(String parameterName, Object value)
          Sets the value of an argument, if the argument is defined, otherwise do nothing.
 int size()
           
 List subList(int fromIndex, int toIndex)
           
 Class[] toClassArray()
           
 Map toMap()
          Gives the arguments back as a (unmodifiable) map.
 String toString()
           
 
Methods inherited from class java.util.AbstractList
add, add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Field Detail

VOID

public static final Parameters VOID
No need to bother for the functions with no parameters. This is a constant you could supply.


backing

protected Map backing
The contents of this List are stored in this HashMap.


definition

protected Parameter[] definition
This array maps integers (position in array) to map keys, making it possible to implement List.


autoCasting

protected boolean autoCasting
If true, values are automatically cast to the right type (if possible) when set.

Constructor Detail

Parameters

public Parameters(Parameter[] def)
Constructor, taking an Parameter[] array argument. The Parameter may also be Parameter.Wrapper (to implement overriding of functions). The idea is that these array arguments are defined as constants in the classes which define a function with variable arguments.
The Parameter[] array could e.g. be somewhere defined as a constant, like this:
   
     public final static Parameter[] MYFUNCTION_PARAMETERS = {
         new Parameter("type", Integer.class),
         new Parameter("text", String.class),
         Parameter.CLOUD,                                 // a predefined parameter
         new Parameter.Wrapper(OTHERFUNCTION_PARAMETERS)  // a way to include another definition in this one
     };
   
 


Parameters

public Parameters(Parameter[] def,
                  Collection values)
If you happen to have a List of parameters, then you can wrap it into an Parameters with this constructor.

Parameters:
values - Collection with values. This Collection should have a predictable iteration order.
Throws:
NullPointerException - if definition is null
See Also:
Parameters(Parameter[])

Parameters

protected Parameters(Parameters params,
                     int from,
                     int to)
Used for nicer implemenation of subList (which we want to also be instanceof Parameters).

Method Detail

toString

public String toString()

toClassArray

public Class[] toClassArray()

setAutoCasting

public void setAutoCasting(boolean autocast)
Sets the 'auto casting' property (which on default is false)

Parameters:
autocast - the new value for autocast
See Also:
isAutoCasting()

getDefinition

public Parameter[] getDefinition()

isAutoCasting

public boolean isAutoCasting()
Whether this Parameters object is 'automaticly casting'. If it is, that means that you can set e.g. an Integer by a String.

Returns:
true if autocasting is on

size

public int size()
Specified by:
size in interface List

get

public Object get(int i)
Specified by:
get in interface List

set

public Object set(int i,
                  Object value)
Specified by:
set in interface List

checkRequiredParameters

public void checkRequiredParameters()
Throws an IllegalArgumentException if one of the required parameters was not entered.


indexOfParameter

public int indexOfParameter(Parameter parameter)
Returns the position of a parameter in the parameters list, using the Parameter as a qualifier. you can tehn acecss that paramter with get(int).

Parameters:
parameter - the parameter
Returns:
the index of the parameter, or -1 if it doesn't exist

indexOfParameter

public int indexOfParameter(String parameterName)
Returns the position of a parameter in the parameters list, using the parameter name as a qualifier. you can then acecss that paramter with get(int).

Parameters:
parameterName - the name of the parameter
Returns:
the index of the parameter, or -1 if it doesn't exist

containsParameter

public boolean containsParameter(Parameter parameter)
Checks wether a certain parameter is available, using the Parameter as a qualifier.

Parameters:
parameter - the parameter
Returns:
true if a parameter exists.

containsParameter

public boolean containsParameter(String parameterName)
Checks wether a certain parameter is available, using the parameter name as a qualifier.

Parameters:
parameterName - the name of the parameter
Returns:
true if a parameter exists.

set

public Parameters set(Parameter parameter,
                      Object value)
Sets the value of a parameter.

Parameters:
parameter - the Parameter describing the parameter to set
value - the object value to set
Throws:
IllegalArgumentException - if either the argument name is unknown to this Parameters, or the value is of the wrong type.

set

public Parameters set(String parameterName,
                      Object value)
Sets the value of a parameter.

Parameters:
parameterName - the name of the parameter to set
value - the object value to set
Throws:
IllegalArgumentException - if either the argument name is unknown to this Parameters, or the value is of the wrong type.

setAll

public Parameters setAll(Map map)
Copies all values of a map to the corresponding values of this Parameters Object.


setAll

public Parameters setAll(Collection values)
Copies all values of a collection to the corresponding values of this Parameters Object.


subList

public List subList(int fromIndex,
                    int toIndex)
Specified by:
subList in interface List

setIfDefined

public Parameters setIfDefined(Parameter parameter,
                               Object value)
Sets the value of an argument, if the argument is defined, otherwise do nothing.

Parameters:
parameter - the parameter to set
value - the object value to set

setIfDefined

public Parameters setIfDefined(String parameterName,
                               Object value)
Sets the value of an argument, if the argument is defined, otherwise do nothing.

Parameters:
parameterName - the name of the parameter to set
value - the object value to set

get

public Object get(Parameter parameter)
Gets the value of a parameter.

Parameters:
parameter - the parameter to get
Returns:
value the parameter value

get

public Object get(String parameterName)
Gets the value of a parameter.

Parameters:
parameterName - the name of the parameter to get
Returns:
value the parameter value

getString

public String getString(Parameter parameter)
Gets the value of a parameter, cast to a String.

Parameters:
parameter - the parameter to get
Returns:
value the parameter value as a STring

getString

public String getString(String parameterName)
Gets the value of a parameter, cast to a String.

Parameters:
parameterName - the name of the parameter to get
Returns:
value the parameter value as a STring

toMap

public Map toMap()
Gives the arguments back as a (unmodifiable) map.



MMBase build 1.8.1.20060716