< prev index next >

src/java.desktop/share/classes/javax/swing/AbstractListModel.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -23,12 +23,15 @@
  * questions.
  */
 
 package javax.swing;
 
-import javax.swing.event.*;
+import javax.swing.event.EventListenerList;
+import javax.swing.event.ListDataListener;
+import javax.swing.event.ListDataEvent;
 import java.io.Serializable;
+import java.util.function.BiConsumer;
 import java.util.EventListener;
 
 /**
  * The abstract definition for the data model that provides
  * a <code>List</code> with its contents.

@@ -95,10 +98,32 @@
         return listenerList.getListeners(ListDataListener.class);
     }
 
 
     /**
+     * The workhorse function which does the job of calling the listeners
+     * interested in listening to the appropriate event that has occurred.
+     * Functions fireContentsChanged, fireIntervalAdded and fireIntervalRemoved
+     * call this function with appropriate arguments filled.
+     */
+    private void fireUpdates(Object source, int index0, int index1, int eventType, 
+                            BiConsumer<ListDataListener, ListDataEvent> func) {
+        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, eventType, index0, index1);
+                }
+                func.accept((ListDataListener)listeners[i+1], e);
+            }
+        }
+    }
+
+
+    /**
      * <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

@@ -110,21 +135,12 @@
      * @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);
-            }
-        }
+        fireUpdates(source, index0, index1, ListDataEvent.CONTENTS_CHANGED, 
+                (l, e) -> l.contentsChanged(e));
     }
 
 
     /**
      * <code>AbstractListModel</code> subclasses must call this method

@@ -140,21 +156,12 @@
      * @see EventListenerList
      * @see DefaultListModel
      */
     protected void fireIntervalAdded(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.INTERVAL_ADDED, index0, index1);
-                }
-                ((ListDataListener)listeners[i+1]).intervalAdded(e);
-            }
-        }
+        fireUpdates(source, index0, index1, ListDataEvent.INTERVAL_ADDED,
+            (l, e) -> l.intervalAdded(e));
     }
 
 
     /**
      * <code>AbstractListModel</code> subclasses must call this method

@@ -171,21 +178,12 @@
      * @see EventListenerList
      * @see DefaultListModel
      */
     protected void fireIntervalRemoved(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.INTERVAL_REMOVED, index0, index1);
-                }
-                ((ListDataListener)listeners[i+1]).intervalRemoved(e);
-            }
-        }
+        fireUpdates(source, index0, index1, ListDataEvent.INTERVAL_REMOVED,
+            (l, e) -> l.intervalRemoved(e));
     }
 
     /**
      * Returns an array of all the objects currently registered as
      * <code><em>Foo</em>Listener</code>s
< prev index next >