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 and Description |
---|
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) |
Modifier and Type | Method and Description |
---|---|
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) |
getConfiguration, getDefaultDocumentation, getName, getType, setDocumentation, setName
public ListPropertyImpl(Configuration configuration, java.lang.String name, java.lang.Class<?> c, java.lang.reflect.Method listGetter)
configuration
- Configuration to which this Property is to belongname
- Name of the Property, eg X if it is accessed via getX(...)c
- Class of the PropertylistGetter
- 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.public ListPropertyImpl(Configuration configuration, java.lang.String name, java.lang.Class<?> c, java.lang.reflect.Method getter, java.lang.reflect.Method countGetter)
configuration
- Configuration to which this Property is to belongname
- Name of the Property, eg X if it is accessed via getX()c
- Class of the Propertygetter
- 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 propertypublic static ListProperty getListProperty(Configuration configuration, java.lang.String name, java.lang.Class<?> type)
configuration
- Configuration to which this Property belongsname
- Parameter nametype
- Parameter typepublic static java.lang.reflect.Method getMethod(java.lang.Class<?> c, java.lang.String[] names, java.lang.Class<?>[] argTypes, java.lang.Class<?> returnType)
c
- Class to searchnames
- Methods to find?argTypes
- Argument typesreturnType
- Return typepublic void setSetter(java.lang.reflect.Method setter)
setter
- A method on the underlying class acts as a setter for a single
value of the property, eg setX(int, Object)public void setArraySetter(java.lang.reflect.Method arraySetter)
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[])public void setInserter(java.lang.reflect.Method inserter, java.lang.reflect.Method adder, java.lang.reflect.Method remover)
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)public java.lang.Object getDefaultValue()
getDefaultValue
in interface ListProperty
ListProperty.getDefaultValue()
public int getNumValues()
getNumValues
in interface ListProperty
ListProperty.getNumValues()
public java.lang.Object getValue(int index) throws StructuralException
getValue
in interface ListProperty
index
- Index of a certain single value of a multi-valued propertyStructuralException
- if the given index is out of rangeListProperty.getValue(int)
public void setValue(int index, java.lang.Object value) throws StructuralException
setValue
in interface ListProperty
index
- Index of a certain single value of a multi-valued propertyvalue
- New value to replace that at the given indexStructuralException
- if the value is invalid (as in setValue) or the given index is
out of range or the Property is immutableListProperty.setValue(int, java.lang.Object)
public void addValue(java.lang.Object value) throws StructuralException
addValue
in interface ListProperty
value
- New value to be added to the end of the listStructuralException
- if the value is invalid (as in setValue) or the Property is
immutable or fixed-cardinalityListProperty.addValue(java.lang.Object)
public void insert(int index, java.lang.Object value) throws StructuralException
insert
in interface ListProperty
index
- Index at which new value is to be insertedvalue
- New valueStructuralException
- if the value is invalid (as in setValue) or the Property is
immutable or fixed-cardinality or the index is out of rangeListProperty.insert(int, java.lang.Object)
public void remove(int index) throws StructuralException
remove
in interface ListProperty
index
- Index of a single value of a multi-valued property that is to be removedStructuralException
- if the given index is out of range or the Property is immutable or fixed cardinalityListProperty.remove(int)
public boolean isFixedCardinality()
isFixedCardinality
in interface Property
Property.isFixedCardinality()
public boolean isMutable()
isMutable
in interface Property
isMutable
in class AbstractProperty
Property.isMutable()
public java.lang.String getDocumentation()
getDocumentation
in interface Property
getDocumentation
in class AbstractProperty
Property.getDocumentation()