Skip to content

@BoundPropertySupport @BoundSetter

peichhorn edited this page Jul 19, 2012 · 5 revisions

@BoundPropertySupport/@BoundSetter

Overview

(Documentation pending)

As of 0.11.2 it is not longer necessary to use @BoundPropertySupport, since @BoundSetter works on its own. In fact @BoundPropertySupport is deprecated as of 0.11.2 and may be removed in the future.

Example

With Lombok

import lombok.AccessLevel;
import lombok.BoundSetter;

class BoundPropertySupportExample {
  @BoundSetter int i;
  @BoundSetter(AccessLevel.PUBLIC) String s;
  @BoundSetter(AccessLevel.PROTECTED) float f;
  @BoundSetter(AccessLevel.PACKAGE) Object o;
  @BoundSetter(AccessLevel.PRIVATE) double d;
}

Vanilla Java

class BoundPropertySupportExample {
  private final java.beans.PropertyChangeSupport propertySupport = new java.beans.PropertyChangeSupport(this);

  public static final java.lang.String PROP_I = new java.lang.String("i");
  public static final java.lang.String PROP_S = new java.lang.String("s");
  public static final java.lang.String PROP_F = new java.lang.String("f");
  public static final java.lang.String PROP_O = new java.lang.String("o");
  public static final java.lang.String PROP_D = new java.lang.String("d");

  int i;
  String s;
  float f;
  Object o;
  double d;

  public void addPropertyChangeListener(final java.beans.PropertyChangeListener listener) {
    this.propertySupport.addPropertyChangeListener(listener);
  }

  public void removePropertyChangeListener(final java.beans.PropertyChangeListener listener) {
    this.propertySupport.removePropertyChangeListener(listener);
  }

  public void setI(final int i) {
    final int old = this.i;
    this.i = i;
    this.propertySupport.firePropertyChange(PROP_I, old, this.i);
  }

  public void setS(final String s) {
    final String old = this.s;
    this.s = s;
    this.propertySupport.firePropertyChange(PROP_S, old, this.s);
  }

  protected void setF(final float f) {
    final float old = this.f;
    this.f = f;
    this.propertySupport.firePropertyChange(PROP_F, old, this.f);
  }

  void setO(final Object o) {
    final Object old = this.o;
    this.o = o;
    this.propertySupport.firePropertyChange(PROP_O, old, this.o);
  }

  private void setD(final double d) {
    final double old = this.d;
    this.d = d;
    this.propertySupport.firePropertyChange(PROP_D, old, this.d);
  }
}

Behind the Scenes

(Documentation pending)

Configuration

If you need to be able to veto against property changes, you may want to use @BoundSetter(vetoable = true) or @BoundSetter(throwVetoException = true). Their use result in the following setters:

  public void setName(final String name) {
    final String $old = this.name;
    try {
      getVetoableChangeSupport().fireVetoableChange(PROP_NAME, $old, name);
    } catch (final java.beans.PropertyVetoException $e) {
      return;
    }
    this.name = name;
    getPropertyChangeSupport().firePropertyChange(PROP_NAME, $old, name);
  }
  
  public void setSurname(final String surname) throws java.beans.PropertyVetoException {
    final String $old = this.surname;
    getVetoableChangeSupport().fireVetoableChange(PROP_SURNAME, $old, surname);
    this.surname = surname;
    getPropertyChangeSupport().firePropertyChange(PROP_SURNAME, $old, surname);
  }