Java Beans

The IBean Interface

RS Library extensively uses Java Bean behaviour in order to implements its features. However, the Base Classes Library formalizes this specification by introducing an interface IBean for them:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
 * A general interface for bean support.
 * @author ralph
 *
 */
public interface IBean extends IPropertyChangeProvider, IDirtyable {
 
        /**
         * Set the property with given name to the value
         * @param name property name
         * @param value value
         */
        public void set(String name, Object value);
 
        /**
         * Gets the property with given name
         * @param name property name
         */
        public Object get(String name);
 
        /**
         * Returns the list of changes that this bean has performed since loading.
         * @return the list of changes so far
         */
        @Transient
        public Collection<PropertyChangeEvent> getChanges();
 
        /**
         * Copies all properties to the given object.
         * @param destination destination object
         */
        public void copyTo(Object destination);
 
        /**
         * Reset all changes.
         */
        public void reset();
 
}

The interface derives from two other interfaces: IPropertyChangeProvider defines the standard methods to register PropertyChangeListeners. IDirtyable enables an object to be marked as dirty. This means that changes have been made to this object and it needs to be persisted. In the default implementation AbstractBean, the state is controlled by PropertyChangeEvents that were fired or can simply be changed by a setter method.

The interface also defines four additional methods to make Java Bean changes more comfortable and trackable. get() and set() are there to enable a more generic access to properties, getChanges() delivers all changes made so far. reset() is defined to revert all changes made so far. That means that a call to getChanges() should return an empty collection and IDirtyable.isDirty() should return false afterwards.

copyTo() requests the bean to copy all its properties onto another bean. Be aware that this other bean should be of the same type as your original bean. The following sections describe to annotations, @Transient and @NoCopy that influence this method.

RS Library delivers an abstract implementation of an IBean, so you don’t need to implement it yourself but simply derive from it: AbstractBean.

Transient Properties

A bean can define transient properties. These properties are usually computed from other bean properties or do not need to be saved as they are irrelevant for persistence. The IBean interface marks getChanges() as transient (annotation @Transient) so the property is never be saved somewhere and exists at runtime only.

Transient properties must not be copied by IBean.copyTo() implementations.

No Copy Properties

Some properties are not meant for copying onto other beans although they would be needed to persist (not transient). A creation or change date is such a property. That’s why the RS Library also defines a @NoCopy annotation which can be used to mark such properties.

1
2
3
4
5
6
7
8
   /**
    * Returns the creation date.
    * @return the creation date
    */
   @NoCopy
   public Date getCreationDate() {
      ...
   }

The annotation can be either on the interface or its implementation.

Leave a Reply