org.mmbase.util.functions
Class Parameters

Package class diagram package Parameters
java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<Object>
          extended by org.mmbase.util.functions.Parameters
All Implemented Interfaces:
Serializable, Iterable<Object>, Collection<Object>, List<Object>
Direct Known Subclasses:
AutodefiningParameters

public class Parameters
extends AbstractList<Object>
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 41943 2010-04-16 17:18:19Z michiel $
Author:
Michiel Meeuwissen
See Also:
Parameter, Parameters(Parameter[]), Serialized Form

Nested Class Summary
protected static class Parameters.SerializableAbstractMap<K,V>
           
 
Field Summary
protected  boolean autoCasting
          If true, values are automatically cast to the right type (if possible) when set.
protected  Map<String,Object> backing
          The contents of this List are stored in this HashMap.
protected  Parameter<Object>[] definition
          This array maps integers (position in array) to map keys, making it possible to implement List.
protected  List<Map.Entry<String,Object>> patternBacking
           
protected  int patternLimit
           
protected  int toIndex
           
static Parameters VOID
          No need to bother for the functions with no parameters.
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
  Parameters(List<Map.Entry<String,Object>> list)
           
  Parameters(Map<String,Object> backing)
           
  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.
  Parameters(Parameter<?>[] def, Object... values)
           
  Parameters(Parameters params)
          Copy-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 check()
           
protected  void checkDef()
           
 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)
           
<F> F
get(Parameter<F> 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'.
 boolean isHavingPatterns()
           
 Object set(int i, Object value)
          implementation of (modifiable) List
<F> Parameters
set(Parameter<F> parameter, F 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<String,?> map)
          Copies all values of a map to the corresponding values of this Parameters Object.
 Parameters setAll(Object... values)
           
 Parameters setAll(Parameters params)
           
 Parameters setAllIfDefined(Parameters params)
           
 void setAutoCasting(boolean autocast)
          Sets the 'auto casting' property (which on default is false)
<F> Parameters
setIfDefined(Parameter<F> parameter, F 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()
           
 Parameters subList(int fromIndex, int toIndex)
           
 Class<?>[] toClassArray()
           
 List<Map.Entry<String,Object>> toEntryList()
          Returns the Parameters as an unmodifiable List of Map.Entrys with predictable iteration order (the same order of this Parameters, which is a List of the values only, itself)
 Map<String,Object> toMap()
          Gives the arguments back as a (unmodifiable) map.
 String toString()
           
 List<Map.Entry<String,Object>> toUndefaultEntryList()
          Returns the Parameters as an unmodifiable List of Map.Entrys with predictable iteration order (the same order of this Parameters, which is a List of the values only, itself) Values which are the same as the default value are returned as null.
 Map<String,Object> toUndefaultMap()
          Returns this parameters object as a (unmodifiable) Map, but all values which only have the default value are null
protected  Map<String,Object> undefaultBacking()
          Returns a view on the backing where every value wich is the default value is set to null.
 Collection<LocalizedString> validate()
          Validates all values in the Parameters object with their Parameter.getDataType().
 
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 final Map<String,Object> backing
The contents of this List are stored in this HashMap.


patternBacking

protected final List<Map.Entry<String,Object>> patternBacking

patternLimit

protected int patternLimit

definition

protected Parameter<Object>[] 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.


toIndex

protected int toIndex
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

public Parameters(Parameter<?>[] def,
                  Object... values)
Since:
MMBase-1.9

Parameters

public Parameters(Map<String,Object> backing)
Since:
MMBase-1.9

Parameters

public Parameters(List<Map.Entry<String,Object>> list)
Since:
MMBase-1.9

Parameters

public Parameters(Parameters params)
Copy-constructor

Since:
MMBase-1.9.1

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

isHavingPatterns

public boolean isHavingPatterns()
Since:
MMBase-1.9

checkDef

protected final void checkDef()

toString

public String toString()
Overrides:
toString in class AbstractCollection<Object>

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 Collection<Object>
Specified by:
size in interface List<Object>
Specified by:
size in class AbstractCollection<Object>

get

public Object get(int i)
Specified by:
get in interface List<Object>
Specified by:
get in class AbstractList<Object>

set

public Object set(int i,
                  Object value)
implementation of (modifiable) List

Specified by:
set in interface List<Object>
Overrides:
set in class AbstractList<Object>
Throws:
NullPointerException - if definition not set

checkRequiredParameters

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

See Also:
For complete datatype validation

validate

public Collection<LocalizedString> validate()
Validates all values in the Parameters object with their Parameter.getDataType(). You should call this method if you ready to pass it into some function or so, if you want validation. If the returned Collection is not empty, something is wrong, and you may want to not proceed.

Returns:
A collection with errors.
Since:
MMBase-1.9.2

check

public void check()
Throws:
IllegalArgumentException - if one or more of the the current values are not valid.
Since:
MMBase-2.0
See Also:
validate()

indexOfParameter

public int indexOfParameter(Parameter<?> parameter)
Returns the position of a parameter in the parameters list, using the Parameter as a qualifier. you can then 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 <F> Parameters set(Parameter<F> parameter,
                          F 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<String,?> 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.


setAll

public Parameters setAll(Object... values)
Since:
MMBase-1.9

setAllIfDefined

public Parameters setAllIfDefined(Parameters params)
Since:
MMBase-1.9

setAll

public Parameters setAll(Parameters params)
Since:
MMBase-1.8.7

subList

public Parameters subList(int fromIndex,
                          int toIndex)
Specified by:
subList in interface List<Object>
Overrides:
subList in class AbstractList<Object>

setIfDefined

public <F> Parameters setIfDefined(Parameter<F> parameter,
                                   F 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 <F> F get(Parameter<F> 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

undefaultBacking

protected Map<String,Object> undefaultBacking()
Returns a view on the backing where every value wich is the default value is set to null. If the default is not null itself, but the value is, than the value will be returned as an empty string. This can be used to generated keys and such, which are not polluted with all kind of default values.

Since:
MMBase-1.9.1

toMap

public Map<String,Object> toMap()
Gives the arguments back as a (unmodifiable) map.


toUndefaultMap

public Map<String,Object> toUndefaultMap()
Returns this parameters object as a (unmodifiable) Map, but all values which only have the default value are null

Since:
MMBase-1.9.1

toEntryList

public List<Map.Entry<String,Object>> toEntryList()
Returns the Parameters as an unmodifiable List of Map.Entrys with predictable iteration order (the same order of this Parameters, which is a List of the values only, itself)

Since:
MMBase-1.9

toUndefaultEntryList

public List<Map.Entry<String,Object>> toUndefaultEntryList()
Returns the Parameters as an unmodifiable List of Map.Entrys with predictable iteration order (the same order of this Parameters, which is a List of the values only, itself) Values which are the same as the default value are returned as null. If the default is not null but the value is, then it is returned as an empty string.

Since:
MMBase-1.9.1


MMBase2 Bridge 2.0-SNAPSHOT - 2014-09-22T23:05