PUBLIC OBJECT

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();
}`</pre>
Naturally for binding, properties can be observable:
<pre>`/**
 * Mixin for properties that can notify observers when
 * they change.
 */
public interface ObservableProperty&lt;B,V&gt; extends Property&lt;B,V&gt; {
  /**
   * 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:

  • Should properties be type-specific or instance-specific? If they're type-specific, properties need an instance to be passed to the `get()` method (as above). Instance-specific properties are bound to a particular baseObject which does not need to be passed in.
  • Should `Property` be an interface or an abstract class?
  • What's a better name for _baseObject_, the thing that has the property?

    If you have any ideas, please join the discussion!