Modelling Properties in Java
We're having a properties discussion over on the beansbinding (JSR-295) mailing list. Here's a recent revision of the API:/**
* Models the relationship between two related types, such as
* the address of a customer or the size of a List.
*
* <p>A property is not bound to a particular base object - rather it
* is shared by all instances of a class.
*
* @param <B> the base object type, such as Customer
* @param <V> the value type, such as an Address or String
*/
public interface Property<B,V> {
/**
* Returns the value of this property on the specified object.
* @throws PropertyNotReadableException
*/
V get(B baseObject);
/**
* Returns the value of this property on the specified object,
* or the specified default if this property cannot be resolved.
*/
V get(B baseObject, V defaultValue);
/**
* Sets the value of this property on baseObject to newValue.
* @return the previous value of the property
* @throws PropertyNotWritableException
*/
V set(B baseObject, V newValue);
Class<B> getBaseObjectType();
Class<V> getValueType();
/**
* Returns a brief description of this property such as
* "address" or "size".
*/
String toString();
}
Naturally for binding, properties can be observable:
/**
* Mixin for properties that can notify observers when
* they change.
*/
public interface ObservableProperty<B,V> extends Property<B,V> {
/**
* Register the specified listener to receive notification when
* this property is changed.
*/
void addPropertyChangeListener(B baseObject, PropertyChangeListener listener);
/**
* Deregister the specified listener from receiving notification
* when this property is changed.
*/
void removePropertyChangeListener(B baseObject, PropertyChangeListener listener);
}
There's still many unresolved issues:
get()
method (as above). Instance-specific properties are bound to a particular baseObject which does not need to be passed in.Property
be an interface or an abstract class?If you have any ideas, please join the discussion!