Loading

PropertyChangeSupport类学习

PropertyChangeSupport主要用于监听属性变更。

  1. 在类里增加监听器
private final PropertyChangeSupport listener = new PropertyChangeSupport(this);
  1. 补充监听方法
public void addPropertyChangeListener(PropertyChangeListener listener){
    this.listener.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener){
    this.listener.removePropertyChangeListener(listener);
}
  1. 在属性set时补充监听
public void setDeployStatusType(TunnelStatusType deployStatusType) {
    TunnelStatusType oldValue = this.deployStatusType;
    this.deployStatusType = deployStatusType;
    this.listener.firePropertyChange("deployStatusType", oldValue, this.deployStatusType);
}
  1. 在具体的类里补充监听实现
srPolicyInfo.addPropertyChangeListener(evt -> {
    if (!"deployStatusType".equals(evt.getPropertyName())) {
        return;
    }
    if (evt.getOldValue() instanceof TunnelStatusType && evt.getNewValue() instanceof TunnelStatusType) {
        TunnelStatusType oldValue = (TunnelStatusType) evt.getOldValue();
        TunnelStatusType newValue = (TunnelStatusType) evt.getNewValue();
        if (TunnelStatusType.DELETE.equals(oldValue) && TunnelStatusType.NORMAL.equals(newValue)) {
            removePath(Lists.newArrayList(removeReq));
        }
    }
});
posted @ 2023-09-04 11:48  星流残阳  阅读(125)  评论(0编辑  收藏  举报