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:
java.util.Collection, java.util.List
Direct Known Subclasses:
AutodefiningParameters

public class Parameters
extends java.util.AbstractList
implements java.util.List

Parameters for functions, a way to make variable arguments in Java. In fact this class does nothing more then providing a convenient way to create a List, by 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.7.2.1 2004/05/27 17:04:42 michiel Exp $
Author:
Michiel Meeuwissen
See Also:
Parameter, Parameters(Parameter[])

Field Summary
protected  boolean autoCasting
           
protected  java.util.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, java.util.List values)
          If you happen to have a List of parameters, then you can wrap it into an Parameters with this constructor.
 
Method Summary
 void checkRequiredParameters()
          Throws IllegalArgumentException if one of the required parameters is null.
protected static java.util.List define(Parameter[] def, java.util.List list)
          Adds the definitions to a List.
 java.lang.Object get(int i)
           
 java.lang.Object get(Parameter arg)
           
static Parameters get(Parameter[] def, java.util.List args)
          Converts a certain List to an Parameters if it is not already one.
 java.lang.Object get(java.lang.String arg)
           
 java.lang.String getString(Parameter arg)
           
 java.lang.String getString(java.lang.String arg)
           
 boolean hasParameter(Parameter arg)
          Checks wether a certain parameter is available.
 boolean isAutoCasting()
          Whether this Parameters object is 'automaticly casting'.
 java.lang.Object set(int i, java.lang.Object value)
           
 Parameters set(Parameter arg, java.lang.Object value)
           
 Parameters set(java.lang.String arg, java.lang.Object value)
          Sets the value of an argument.
 Parameters setAll(java.util.Map map)
           
 void setAutoCasting(boolean ac)
          Sets the 'auto casting' property (which on default is false)
 Parameters setIfDefined(java.lang.String arg, java.lang.Object value)
          Sets the value of an argument, if the argument is defined, otherwise do nothing.
 int size()
           
 java.lang.Class[] toClassArray()
          When using reflection, you might need the Parameters as a Class[].
 java.util.Map toMap()
          Gives the arguments back as a (unmodifiable) map.
 java.lang.String toString()
           
 
Methods inherited from class java.util.AbstractList
add, add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, subList
 
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
add, add, addAll, addAll, clear, contains, containsAll, equals, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, retainAll, subList, 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 java.util.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
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,
                  java.util.List values)
If you happen to have a List of parameters, then you can wrap it into an Parameters with this constructor.

Throws:
java.lang.NullPointerException - if definition is null
See Also:
Parameters(Parameter[])
Method Detail

get

public static Parameters get(Parameter[] def,
                             java.util.List args)
Converts a certain List to an Parameters if it is not already one.


toString

public java.lang.String toString()

toClassArray

public java.lang.Class[] toClassArray()
When using reflection, you might need the Parameters as a Class[]. This function provides it.

Throws:
java.lang.NullPointerException - if definition is null

define

protected static java.util.List define(Parameter[] def,
                                       java.util.List list)
Adds the definitions to a List. Resolves the Attribute.Wrapper's (recursively).

Returns:
List with only simple Parameter's.

setAutoCasting

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

See Also:
isAutoCasting()

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.


size

public int size()
Specified by:
size in interface java.util.List

get

public java.lang.Object get(int i)
Specified by:
get in interface java.util.List

set

public java.lang.Object set(int i,
                            java.lang.Object value)
Specified by:
set in interface java.util.List

checkRequiredParameters

public void checkRequiredParameters()
Throws IllegalArgumentException if one of the required parameters is null.


hasParameter

public boolean hasParameter(Parameter arg)
Checks wether a certain parameter is available.


set

public Parameters set(java.lang.String arg,
                      java.lang.Object value)
Sets the value of an argument.

Throws:
java.lang.IllegalArgumentException - if either the argument name is unknown to this Parameters, or the value is of the wrong type.
java.lang.NullPointerException - if definition not set

set

public Parameters set(Parameter arg,
                      java.lang.Object value)

setAll

public Parameters setAll(java.util.Map map)

setIfDefined

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

Throws:
java.lang.NullPointerException - if definition not set

get

public java.lang.Object get(java.lang.String arg)

get

public java.lang.Object get(Parameter arg)

getString

public java.lang.String getString(java.lang.String arg)

getString

public java.lang.String getString(Parameter arg)

toMap

public java.util.Map toMap()
Gives the arguments back as a (unmodifiable) map. Parameters cannot implement Map itself because 'remove' of List and Map exclude. Remark: Could return a modifiable map, but then need to make a wrapper implementation.



MMBase build 1.7.4.20050922