Nengo.ca

ca.nengo.config.impl
Class ListPropertyImpl

java.lang.Object
  extended by ca.nengo.config.impl.AbstractProperty
      extended by ca.nengo.config.impl.ListPropertyImpl
All Implemented Interfaces:
ListProperty, Property

public class ListPropertyImpl
extends AbstractProperty
implements ListProperty

Default implementation of ListProperty. This implementation uses reflection to call methods on an underlying configurable object in order to get and set multiple property values.

The easiest way to use this class is via the factory method getListProperty(...). In this case, the class of configuration.getConfigurable() is searched for methods that appear to be getters, setters, etc. of a named parameter. For example if the parameter is named "X" and has type Foo, then a method getX(int) with return type Foo is taken as the getter. Methods to get, set, insert, add, remove values, get/set arrays, and get lists are searched based on return and argument types, and a variety of probable names (including bean patterns). If there are not methods available for at least getting and counting values, null is returned. The set of other methods found determines whether the property is mutable and has fixed cardinality (i.e. # of values).

If customization is needed, there are two alternative public constructors that accept user-specified methods, and additional functionality can then be imparted via setSetter(...), setArraySetter(...), and setInserter(...). This allows use of methods with unexpected names, although the expected arguments and return types are still required.

If further customization is needed (e.g. using an Integer index argument instead of an int), then the methods of this class must be overridden.


Constructor Summary
ListPropertyImpl(Configuration configuration, java.lang.String name, java.lang.Class<?> c, java.lang.reflect.Method listGetter)
           
ListPropertyImpl(Configuration configuration, java.lang.String name, java.lang.Class<?> c, java.lang.reflect.Method getter, java.lang.reflect.Method countGetter)
           
 
Method Summary
 void addValue(java.lang.Object value)
           
 java.lang.Object getDefaultValue()
           
 java.lang.String getDocumentation()
           
static ListProperty getListProperty(Configuration configuration, java.lang.String name, java.lang.Class<?> type)
           
static java.lang.reflect.Method getMethod(java.lang.Class<?> c, java.lang.String[] names, java.lang.Class<?>[] argTypes, java.lang.Class<?> returnType)
          Looks for defined method
 int getNumValues()
           
 java.lang.Object getValue(int index)
           
 void insert(int index, java.lang.Object value)
           
 boolean isFixedCardinality()
           
 boolean isMutable()
           
 void remove(int index)
           
 void setArraySetter(java.lang.reflect.Method arraySetter)
           
 void setInserter(java.lang.reflect.Method inserter, java.lang.reflect.Method adder, java.lang.reflect.Method remover)
           
 void setSetter(java.lang.reflect.Method setter)
           
 void setValue(int index, java.lang.Object value)
           
 
Methods inherited from class ca.nengo.config.impl.AbstractProperty
getConfiguration, getDefaultDocumentation, getName, getType, setDocumentation, setName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface ca.nengo.config.Property
getName, getType, setName
 

Constructor Detail

ListPropertyImpl

public ListPropertyImpl(Configuration configuration,
                        java.lang.String name,
                        java.lang.Class<?> c,
                        java.lang.reflect.Method listGetter)
Parameters:
configuration - Configuration to which this Property is to belong
name - Name of the Property, eg X if it is accessed via getX(...)
c - Class of the Property
listGetter - A method on the underlying class (the configuration target; not c) that returns multiple values of the property, as either an array of c or a list of c.

ListPropertyImpl

public ListPropertyImpl(Configuration configuration,
                        java.lang.String name,
                        java.lang.Class<?> c,
                        java.lang.reflect.Method getter,
                        java.lang.reflect.Method countGetter)
Parameters:
configuration - Configuration to which this Property is to belong
name - Name of the Property, eg X if it is accessed via getX()
c - Class of the Property
getter - A method on the underlying class (the configuration target; not c) that returns a single indexed value of the property, eg getX(int).
countGetter - A method on the underlying class that returns the number of values of the property
Method Detail

getListProperty

public static ListProperty getListProperty(Configuration configuration,
                                           java.lang.String name,
                                           java.lang.Class<?> type)
Parameters:
configuration - Configuration to which this Property belongs
name - Parameter name
type - Parameter type
Returns:
Property or null if the necessary methods don't exist on the underlying class

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class<?> c,
                                                 java.lang.String[] names,
                                                 java.lang.Class<?>[] argTypes,
                                                 java.lang.Class<?> returnType)
Looks for defined method

Parameters:
c - Class to search
names - Methods to find?
argTypes - Argument types
returnType - Return type
Returns:
The Method, or null if it doesn't exist

setSetter

public void setSetter(java.lang.reflect.Method setter)
Parameters:
setter - A method on the underlying class acts as a setter for a single value of the property, eg setX(int, Object)

setArraySetter

public void setArraySetter(java.lang.reflect.Method arraySetter)
Parameters:
arraySetter - A method on the underlying class that acts as a setter for all values of the property using an array argument, eg setX(Object[])

setInserter

public void setInserter(java.lang.reflect.Method inserter,
                        java.lang.reflect.Method adder,
                        java.lang.reflect.Method remover)
Parameters:
inserter - A method on the underlying class that inserts a value, eg insertX(int, Object)
adder - A method on the underlying class that adds a value to the end of the list, eg addX(Object)
remover - A method on the underlying class that removes a value, eg removeX(int)

getDefaultValue

public java.lang.Object getDefaultValue()
Specified by:
getDefaultValue in interface ListProperty
Returns:
Default value for insertions TODO: remove; use default from NewConfigurableDialog (move to ConfigUtil)
See Also:
ListProperty.getDefaultValue()

getNumValues

public int getNumValues()
Specified by:
getNumValues in interface ListProperty
Returns:
Number of repeated values of this Property
See Also:
ListProperty.getNumValues()

getValue

public java.lang.Object getValue(int index)
                          throws StructuralException
Specified by:
getValue in interface ListProperty
Parameters:
index - Index of a certain single value of a multi-valued property
Returns:
The value at the given index
Throws:
StructuralException - if the given index is out of range
See Also:
ListProperty.getValue(int)

setValue

public void setValue(int index,
                     java.lang.Object value)
              throws StructuralException
Specified by:
setValue in interface ListProperty
Parameters:
index - Index of a certain single value of a multi-valued property
value - New value to replace that at the given index
Throws:
StructuralException - if the value is invalid (as in setValue) or the given index is out of range or the Property is immutable
See Also:
ListProperty.setValue(int, java.lang.Object)

addValue

public void addValue(java.lang.Object value)
              throws StructuralException
Specified by:
addValue in interface ListProperty
Parameters:
value - New value to be added to the end of the list
Throws:
StructuralException - if the value is invalid (as in setValue) or the Property is immutable or fixed-cardinality
See Also:
ListProperty.addValue(java.lang.Object)

insert

public void insert(int index,
                   java.lang.Object value)
            throws StructuralException
Specified by:
insert in interface ListProperty
Parameters:
index - Index at which new value is to be inserted
value - New value
Throws:
StructuralException - if the value is invalid (as in setValue) or the Property is immutable or fixed-cardinality or the index is out of range
See Also:
ListProperty.insert(int, java.lang.Object)

remove

public void remove(int index)
            throws StructuralException
Specified by:
remove in interface ListProperty
Parameters:
index - Index of a single value of a multi-valued property that is to be removed
Throws:
StructuralException - if the given index is out of range or the Property is immutable or fixed cardinality
See Also:
ListProperty.remove(int)

isFixedCardinality

public boolean isFixedCardinality()
Specified by:
isFixedCardinality in interface Property
Returns:
True if the property has a fixed number of values
See Also:
Property.isFixedCardinality()

isMutable

public boolean isMutable()
Specified by:
isMutable in interface Property
Overrides:
isMutable in class AbstractProperty
Returns:
True if values can be changed after construction of the Configurable
See Also:
Property.isMutable()

getDocumentation

public java.lang.String getDocumentation()
Specified by:
getDocumentation in interface Property
Overrides:
getDocumentation in class AbstractProperty
Returns:
Text describing the property semantics (plain text or HTML)
See Also:
Property.getDocumentation()

Nengo.ca