繁体   English   中英

JDK中此代码的用途是什么?

[英]What is the purpose of this code in the JDK?

以下代码取自Oracle jdk1.8.0_40 AbstractListModel类。

   /**
     * <code>AbstractListModel</code> subclasses must call this method
     * <b>after</b>
     * one or more elements of the list change.  The changed elements
     * are specified by the closed interval index0, index1 -- the endpoints
     * are included.  Note that
     * index0 need not be less than or equal to index1.
     *
     * @param source the <code>ListModel</code> that changed, typically "this"
     * @param index0 one end of the new interval
     * @param index1 the other end of the new interval
     * @see EventListenerList
     * @see DefaultListModel
     */
    protected void fireContentsChanged(Object source, int index0, int index1)
    {
        Object[] listeners = listenerList.getListenerList();
        ListDataEvent e = null;

        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == ListDataListener.class) {
                if (e == null) {
                    e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1);
                }
                ((ListDataListener)listeners[i+1]).contentsChanged(e);
            }
        }
    }

我的问题是

  • 为什么迭代开始于listeners.length - 2 listeners.length - 1元素呢?
  • 为什么每个其他元素都触发事件( i -= 2 )?
  • 为什么迭代的顺序相反?

也是openjdk中代码的链接。

listeners数组包含偶数索引中的侦听器Class对象和奇数索引中的侦听器实例。

因此,循环检查listeners数组中每个偶数索引的类型

if (listeners[i] == ListDataListener.class

但仅针对奇数指数触发事件:

((ListDataListener)listeners[i+1]).contentsChanged(e);

listeners.length - 1不会被跳过。 因为当i == listeners.length - 2i+1 == listeners.length - 1

我不确定逆序迭代的原因。

根据添加新侦听器的代码,如下所示:

public void addListDataListener(ListDataListener l) {
    listenerList.add(ListDataListener.class, l);
}

该列表实际上包含一对Class实例和对象实例。

关于迭代顺序,或许首先是通知新的听众的故意方法?

如果查看EventListenerList:列表是使用两个元素构建的,其中一个是侦听器对象,另一个是对象的类。 这解释了2乘2的迭代以及与其他元素对类的检查。

我发现这段代码非常难看,很多重复这个奇怪的迭代循环。 我没有关于这个相当严格的实现的答案,但IMO的原因可能是保持与传统JAVA的兼容性 - 保持API相同,也许是性能,可能是在Java 1.5中引入泛型。

Wy以相反的顺序迭代? 不知道为什么会这样实现。 可能是实现决策或需要首先调用最新添加的侦听器的规范。 我试图找到另一个很好的理由,但我不能......此外它曾经反向迭代,因为至少1.6(我没有检查旧版本)。

他们以相反顺序迭代的原因是他们不必每次都评估.length 他们将其作为性能优化。 这是一个相关的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM