Creating an AttributeListener

Elliot Ford edited this page May 25, 2015 · 11 revisions

What is an AttributeListener?

An AttributeListener is a listener that is fired upon adding, changing or remove an attribute in an AttributeContainer (see Adding, getting and removing attributes). Like attributes an AttributeListener is assigned to an attribute name and type (see Creating an AttributeType). When a listener method is fired an AttributeEvent is created and supplied for the trigger.

What is an AttributeEvent?

An AttributeEvent contains all the information about the specific trigger event. This includes attribute name, type, value and replaced attribute. As well as this the event contains the container that this event was fired from (which could be an Entity).

Creating a listener

AttributeListener is an interface with each method signifying a different trigger situation.

void attributeAdded(final AttributeEvent<T> event);

void attributeChanged(final AttributeEvent<T> event);

void attributeRemoved(final AttributeEvent<T> event);

To avoid having to supply empty dummy implementations of each method AttributeListener methods are all default. The below listener outputs the attribute value to the console whenever an attribute is added.

AttributeListener<String> listener = new AttributeListener<String>() {

    @Override
    public void attributeAdded(final AttributeEvent<String> event) {
      System.out.println(event.getValue());
    }
}

Adding a listener

This will associate the listener to the supplied attribute name and type.

AttributeListener<String> listener = /* See above */

AttributeContainer container = new DefaultAttributeContainer();
container.addAttributeListener("username", Attributes.STRING_TYPE, listener);

There is a NamedAttributeType equivalent: container.addAttributeListener(NamedAttributeType, AttributeListener).

Triggering for an internal change

Some attributes are not immutable but it is still useful to know that they have changed. This has to be a manual process:

AttributeContainer container = /* See above */
container.fireAttributeChanged("username", Attributes.STRING_TYPE);

This will manually fire the attributeChanged(AttributeEvent) method in the listener.

There is a NamedAttributeType equivalent: container.fireAttributeChanged(NamedAttributeType, AttributeListener).

Removing a listener

This is disassociate the listener from the supplied name and type.

AttributeListener<String> listener = /* See above */

AttributeContainer container = /* See above */
container.removeAttributeListener("username", Attributes.STRING_TYPE, listener);

There is a NamedAttributeType equivalent: container.removeAttributeListener(NamedAttributeType, AttributeListener).

Using multiple listeners

AttributeContainer offers similar management of AttributeListeners as is does to attributes.

All listener names: container.getAttributeListenerNames()

All listener types: container.getAttributeListenerTypes("username")

All listeners for name and type: container.getAttributeListeners("username", Attributes.STRING_TYPE)

Remove all listeners for name and type: container.removeAttributeListeners("username", Attributes.STRING_TYPE)

Add all listeners from another container: container.addAllAttributeListeners(otherContainer)

Removes all listeners: container.removeAllAttributeListeners()

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.