java 中的触发事件

发布于 2024-12-17 10:43:44 字数 135 浏览 2 评论 0原文

为什么 PropertyChangeSupport 类中的方法 firePropertyChange (String propertyName, Object oldValue, Object newValue) 不检查旧值和新值是否可以同时为 null?

Why method firePropertyChange (String propertyName, Object oldValue, Object newValue) in class PropertyChangeSupport don't check that old and new value can be null at the same time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拍不死你 2024-12-24 10:43:44

PropertyChangeEvent javadoc 可能会提供一些线索:

可以为旧值和新值提供空值,如果它们
真实值未知。

事件源可以发送一个空对象作为名称来指示
任意集合(如果其属性已更改)。在这种情况下,旧
新值也应该为空。

因此,当 source==null 时,oldValue==nullnewValue==null 看起来也有一些特殊含义。因此,当两个值都为 null 时,即使它们相同,也可能希望始终传播更改。

PropertyChangeEvent javadoc may give some clue about it:

Null values may be provided for the old and the new values if their
true values are not known.

An event source may send a null object as the name to indicate that an
arbitrary set of if its properties have changed. In this case the old
and new values should also be null.

So it looks like having oldValue==null and newValue==null can have some special meaning when source==null as well. Because of this is may want to always propagate the change when both values are nulls even though they are the same.

无力看清 2024-12-24 10:43:44

我不确定你的意思是什么,但这里的实际代码肯定比我试图解释的更干净:)

/**
     * Reports a bound property update to listeners
     * that have been registered to track updates of
     * all properties or a property with the specified name.
     * <p>
     * No event is fired if old and new values are equal and non-null.
     * <p>
     * This is merely a convenience wrapper around the more general
     * {@link #firePropertyChange(PropertyChangeEvent)} method.
     *
     * @param propertyName  the programmatic name of the property that was changed
     * @param oldValue      the old value of the property
     * @param newValue      the new value of the property
     */
    public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
        if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
            firePropertyChange(new PropertyChangeEvent(this.source, propertyName, oldValue, newValue));
        }
    }

I am not sure what do you mean, but here is actual code that is definitelly more clean than me trying to explain it :)

/**
     * Reports a bound property update to listeners
     * that have been registered to track updates of
     * all properties or a property with the specified name.
     * <p>
     * No event is fired if old and new values are equal and non-null.
     * <p>
     * This is merely a convenience wrapper around the more general
     * {@link #firePropertyChange(PropertyChangeEvent)} method.
     *
     * @param propertyName  the programmatic name of the property that was changed
     * @param oldValue      the old value of the property
     * @param newValue      the new value of the property
     */
    public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
        if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
            firePropertyChange(new PropertyChangeEvent(this.source, propertyName, oldValue, newValue));
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文