4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c)
authorkaddepalli
Wed, 02 May 2018 15:11:54 +0530
changeset 50008 c87a5690e394
parent 50007 70a63053cf68
child 50009 93cb8fb7a843
4842658: DefaultListModel and DefaultComboBoxModel should support addAll (Collection c) Reviewed-by: darcy, serb, prr
src/java.desktop/share/classes/javax/swing/DefaultComboBoxModel.java
src/java.desktop/share/classes/javax/swing/DefaultListModel.java
test/jdk/javax/swing/JComboBox/DefaultComboBoxModelAddAllElementsTest.java
test/jdk/javax/swing/JList/DefaultListModelAddAllTest.java
--- a/src/java.desktop/share/classes/javax/swing/DefaultComboBoxModel.java	Thu Apr 26 17:03:34 2018 -0700
+++ b/src/java.desktop/share/classes/javax/swing/DefaultComboBoxModel.java	Wed May 02 15:11:54 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 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
@@ -24,8 +24,8 @@
  */
 package javax.swing;
 
-import java.util.*;
-
+import java.util.Collection;
+import java.util.Vector;
 import java.io.Serializable;
 
 /**
@@ -177,4 +177,46 @@
             selectedObject = null;
         }
     }
+
+    /**
+     * Adds all of the elements present in the collection.
+     *
+     * @param c the collection which contains the elements to add
+     * @throws NullPointerException if {@code c} is null
+     */
+    public void addAll(Collection<? extends E> c) {
+        if (c.isEmpty()) {
+            return;
+        }
+
+        int startIndex = getSize();
+
+        objects.addAll(c);
+        fireIntervalAdded(this, startIndex, getSize() - 1);
+    }
+
+    /**
+     * Adds all of the elements present in the collection, starting
+     * from the specified index.
+     *
+     * @param index index at which to insert the first element from the
+     * specified collection
+     * @param c the collection which contains the elements to add
+     * @throws ArrayIndexOutOfBoundsException if {@code index} does not
+     * fall within the range of number of elements currently held
+     * @throws NullPointerException if {@code c} is null
+     */
+    public void addAll(int index, Collection<? extends E> c) {
+        if (index < 0 || index > getSize()) {
+            throw new ArrayIndexOutOfBoundsException("index out of range: " +
+                                                                       index);
+        }
+
+        if (c.isEmpty()) {
+            return;
+        }
+
+        objects.addAll(index, c);
+        fireIntervalAdded(this, index, index + c.size() - 1);
+    }
 }
--- a/src/java.desktop/share/classes/javax/swing/DefaultListModel.java	Thu Apr 26 17:03:34 2018 -0700
+++ b/src/java.desktop/share/classes/javax/swing/DefaultListModel.java	Wed May 02 15:11:54 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2014, 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
@@ -26,17 +26,16 @@
 package javax.swing;
 
 import java.util.Vector;
+import java.util.Collection;
 import java.util.Enumeration;
 
-import javax.swing.event.*;
-
 
 /**
- * This class loosely implements the <code>java.util.Vector</code>
+ * This class loosely implements the {@code java.util.Vector}
  * API, in that it implements the 1.1.x version of
- * <code>java.util.Vector</code>, has no collection class support,
- * and notifies the <code>ListDataListener</code>s when changes occur.
- * Presently it delegates to a <code>Vector</code>,
+ * {@code java.util.Vector}, has no collection class support,
+ * and notifies the {@code ListDataListener}s when changes occur.
+ * Presently it delegates to a {@code Vector},
  * in a future release it will be a real Collection implementation.
  * <p>
  * <strong>Warning:</strong>
@@ -45,7 +44,7 @@
  * appropriate for short term storage or RMI between applications running
  * the same version of Swing.  As of 1.4, support for long term storage
  * of all JavaBeans&trade;
- * has been added to the <code>java.beans</code> package.
+ * has been added to the {@code java.beans} package.
  * Please see {@link java.beans.XMLEncoder}.
  *
  * @param <E> the type of the elements of this model
@@ -61,10 +60,10 @@
     /**
      * Returns the number of components in this list.
      * <p>
-     * This method is identical to <code>size</code>, which implements the
-     * <code>List</code> interface defined in the 1.2 Collections framework.
-     * This method exists in conjunction with <code>setSize</code> so that
-     * <code>size</code> is identifiable as a JavaBean property.
+     * This method is identical to {@code size}, which implements the
+     * {@code List} interface defined in the 1.2 Collections framework.
+     * This method exists in conjunction with {@code setSize} so that
+     * {@code size} is identifiable as a JavaBean property.
      *
      * @return  the number of components in this list
      * @see #size()
@@ -77,12 +76,12 @@
      * Returns the component at the specified index.
      * <blockquote>
      * <b>Note:</b> Although this method is not deprecated, the preferred
-     *    method to use is <code>get(int)</code>, which implements the
-     *    <code>List</code> interface defined in the 1.2 Collections framework.
+     *    method to use is {@code get(int)}, which implements the {@code List}
+     *    interface defined in the 1.2 Collections framework.
      * </blockquote>
      * @param      index   an index into this list
      * @return     the component at the specified index
-     * @exception  ArrayIndexOutOfBoundsException  if the <code>index</code>
+     * @throws     ArrayIndexOutOfBoundsException  if the {@code index}
      *             is negative or greater than the current size of this
      *             list
      * @see #get(int)
@@ -94,7 +93,7 @@
     /**
      * Copies the components of this list into the specified array.
      * The array must be big enough to hold all the objects in this list,
-     * else an <code>IndexOutOfBoundsException</code> is thrown.
+     * else an {@code IndexOutOfBoundsException} is thrown.
      *
      * @param   anArray   the array into which the components get copied
      * @see Vector#copyInto(Object[])
@@ -164,9 +163,9 @@
     /**
      * Tests whether this list has any components.
      *
-     * @return  <code>true</code> if and only if this list has
+     * @return  {@code true} if and only if this list has
      *          no components, that is, its size is zero;
-     *          <code>false</code> otherwise
+     *          {@code false} otherwise
      * @see Vector#isEmpty()
      */
     public boolean isEmpty() {
@@ -187,7 +186,7 @@
      * Tests whether the specified object is a component in this list.
      *
      * @param   elem   an object
-     * @return  <code>true</code> if the specified object
+     * @return  {@code true} if the specified object
      *          is the same as a component in this list
      * @see Vector#contains(Object)
      */
@@ -196,11 +195,11 @@
     }
 
     /**
-     * Searches for the first occurrence of <code>elem</code>.
+     * Searches for the first occurrence of {@code elem}.
      *
      * @param   elem   an object
      * @return  the index of the first occurrence of the argument in this
-     *          list; returns <code>-1</code> if the object is not found
+     *          list; returns {@code -1} if the object is not found
      * @see Vector#indexOf(Object)
      */
     public int indexOf(Object elem) {
@@ -208,14 +207,14 @@
     }
 
     /**
-     * Searches for the first occurrence of <code>elem</code>, beginning
-     * the search at <code>index</code>.
+     * Searches for the first occurrence of {@code elem}, beginning
+     * the search at {@code index}.
      *
-     * @param   elem    an desired component
+     * @param   elem    the desired component
      * @param   index   the index from which to begin searching
-     * @return  the index where the first occurrence of <code>elem</code>
-     *          is found after <code>index</code>; returns <code>-1</code>
-     *          if the <code>elem</code> is not found in the list
+     * @return  the index where the first occurrence of {@code elem}
+     *          is found after {@code index}; returns {@code -1}
+     *          if the {@code elem} is not found in the list
      * @see Vector#indexOf(Object,int)
      */
      public int indexOf(Object elem, int index) {
@@ -223,11 +222,11 @@
     }
 
     /**
-     * Returns the index of the last occurrence of <code>elem</code>.
+     * Returns the index of the last occurrence of {@code elem}.
      *
      * @param   elem   the desired component
-     * @return  the index of the last occurrence of <code>elem</code>
-     *          in the list; returns <code>-1</code> if the object is not found
+     * @return  the index of the last occurrence of {@code elem}
+     *          in the list; returns {@code elem} if the object is not found
      * @see Vector#lastIndexOf(Object)
      */
     public int lastIndexOf(Object elem) {
@@ -235,14 +234,14 @@
     }
 
     /**
-     * Searches backwards for <code>elem</code>, starting from the
+     * Searches backwards for {@code elem}, starting from the
      * specified index, and returns an index to it.
      *
      * @param  elem    the desired component
      * @param  index   the index to start searching from
-     * @return the index of the last occurrence of the <code>elem</code>
-     *          in this list at position less than <code>index</code>;
-     *          returns <code>-1</code> if the object is not found
+     * @return the index of the last occurrence of the {@code elem}
+     *          in this list at position less than {@code index};
+     *          returns {@code -1} if the object is not found
      * @see Vector#lastIndexOf(Object,int)
      */
     public int lastIndexOf(Object elem, int index) {
@@ -251,16 +250,16 @@
 
     /**
      * Returns the component at the specified index.
-     * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
-     * is negative or not less than the size of the list.
      * <blockquote>
      * <b>Note:</b> Although this method is not deprecated, the preferred
-     *    method to use is <code>get(int)</code>, which implements the
-     *    <code>List</code> interface defined in the 1.2 Collections framework.
+     *    method to use is {@code get(int)}, which implements the
+     *    {@code List} interface defined in the 1.2 Collections framework.
      * </blockquote>
      *
      * @param      index   an index into this list
      * @return     the component at the specified index
+     * @throws     ArrayIndexOutOfBoundsException if the index
+     * is negative or not less than the size of the list
      * @see #get(int)
      * @see Vector#elementAt(int)
      */
@@ -270,10 +269,10 @@
 
     /**
      * Returns the first component of this list.
-     * Throws a <code>NoSuchElementException</code> if this
-     * vector has no components.
      * @return     the first component of this list
      * @see Vector#firstElement()
+     * @throws NoSuchElementException if this
+     * vector has no components
      */
     public E firstElement() {
         return delegate.firstElement();
@@ -281,31 +280,29 @@
 
     /**
      * Returns the last component of the list.
-     * Throws a <code>NoSuchElementException</code> if this vector
-     * has no components.
      *
      * @return  the last component of the list
      * @see Vector#lastElement()
+     * @throws NoSuchElementException if this vector
+     * has no components
      */
     public E lastElement() {
         return delegate.lastElement();
     }
 
     /**
-     * Sets the component at the specified <code>index</code> of this
+     * Sets the component at the specified {@code index} of this
      * list to be the specified element. The previous component at that
      * position is discarded.
-     * <p>
-     * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
-     * is invalid.
      * <blockquote>
      * <b>Note:</b> Although this method is not deprecated, the preferred
-     *    method to use is <code>set(int,Object)</code>, which implements the
-     *    <code>List</code> interface defined in the 1.2 Collections framework.
+     *    method to use is {@code set(int,Object)}, which implements the
+     *    {@code List} interface defined in the 1.2 Collections framework.
      * </blockquote>
      *
      * @param      element what the component is to be set to
      * @param      index   the specified index
+     * @throws     ArrayIndexOutOfBoundsException if the index is invalid
      * @see #set(int,Object)
      * @see Vector#setElementAt(Object,int)
      */
@@ -316,18 +313,16 @@
 
     /**
      * Deletes the component at the specified index.
-     * <p>
-     * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
-     * is invalid.
      * <blockquote>
      * <b>Note:</b> Although this method is not deprecated, the preferred
-     *    method to use is <code>remove(int)</code>, which implements the
-     *    <code>List</code> interface defined in the 1.2 Collections framework.
+     *    method to use is {@code remove(int)}, which implements the
+     *    {@code List} interface defined in the 1.2 Collections framework.
      * </blockquote>
      *
      * @param      index   the index of the object to remove
      * @see #remove(int)
      * @see Vector#removeElementAt(int)
+     * @throws ArrayIndexOutOfBoundsException if the index is invalid
      */
     public void removeElementAt(int index) {
         delegate.removeElementAt(index);
@@ -337,18 +332,15 @@
     /**
      * Inserts the specified element as a component in this list at the
      * specified <code>index</code>.
-     * <p>
-     * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
-     * is invalid.
      * <blockquote>
      * <b>Note:</b> Although this method is not deprecated, the preferred
-     *    method to use is <code>add(int,Object)</code>, which implements the
-     *    <code>List</code> interface defined in the 1.2 Collections framework.
+     *    method to use is {@code add(int,Object)}, which implements the
+     *    {@code List} interface defined in the 1.2 Collections framework.
      * </blockquote>
      *
      * @param      element the component to insert
      * @param      index   where to insert the new component
-     * @exception  ArrayIndexOutOfBoundsException  if the index was invalid
+     * @exception  ArrayIndexOutOfBoundsException if the index was invalid
      * @see #add(int,Object)
      * @see Vector#insertElementAt(Object,int)
      */
@@ -374,8 +366,8 @@
      * from this list.
      *
      * @param   obj   the component to be removed
-     * @return  <code>true</code> if the argument was a component of this
-     *          list; <code>false</code> otherwise
+     * @return  {@code true} if the argument was a component of this
+     *          list; {@code false} otherwise
      * @see Vector#removeElement(Object)
      */
     public boolean removeElement(Object obj) {
@@ -392,8 +384,8 @@
      * Removes all components from this list and sets its size to zero.
      * <blockquote>
      * <b>Note:</b> Although this method is not deprecated, the preferred
-     *    method to use is <code>clear</code>, which implements the
-     *    <code>List</code> interface defined in the 1.2 Collections framework.
+     *    method to use is {@code clear}, which implements the
+     *    {@code List} interface defined in the 1.2 Collections framework.
      * </blockquote>
      *
      * @see #clear()
@@ -438,13 +430,11 @@
 
     /**
      * Returns the element at the specified position in this list.
-     * <p>
-     * Throws an <code>ArrayIndexOutOfBoundsException</code>
-     * if the index is out of range
-     * (<code>index &lt; 0 || index &gt;= size()</code>).
      *
      * @param index index of element to return
      * @return the element at the specified position in this list
+     * @throws ArrayIndexOutOfBoundsException if the index is out of range
+     * ({@code index &lt; 0 || index &gt;= size()})
      */
     public E get(int index) {
         return delegate.elementAt(index);
@@ -453,14 +443,12 @@
     /**
      * Replaces the element at the specified position in this list with the
      * specified element.
-     * <p>
-     * Throws an <code>ArrayIndexOutOfBoundsException</code>
-     * if the index is out of range
-     * (<code>index &lt; 0 || index &gt;= size()</code>).
      *
      * @param index index of element to replace
      * @param element element to be stored at the specified position
      * @return the element previously at the specified position
+     * @throws ArrayIndexOutOfBoundsException if the index is out of range
+     * ({@code index &lt; 0 || index &gt;= size()})
      */
     public E set(int index, E element) {
         E rv = delegate.elementAt(index);
@@ -471,13 +459,11 @@
 
     /**
      * Inserts the specified element at the specified position in this list.
-     * <p>
-     * Throws an <code>ArrayIndexOutOfBoundsException</code> if the
-     * index is out of range
-     * (<code>index &lt; 0 || index &gt; size()</code>).
      *
      * @param index index at which the specified element is to be inserted
      * @param element element to be inserted
+     * @throws ArrayIndexOutOfBoundsException if the index is out of range
+     * ({@code index &lt; 0 || index &gt; size()})
      */
     public void add(int index, E element) {
         delegate.insertElementAt(element, index);
@@ -486,14 +472,12 @@
 
     /**
      * Removes the element at the specified position in this list.
-     * Returns the element that was removed from the list.
-     * <p>
-     * Throws an <code>ArrayIndexOutOfBoundsException</code>
-     * if the index is out of range
-     * (<code>index &lt; 0 || index &gt;= size()</code>).
+     * Returns the element that was removed from the list
      *
      * @param index the index of the element to removed
      * @return the element previously at the specified position
+     * @throws ArrayIndexOutOfBoundsException if the index is out of range
+     * ({@code index &lt; 0 || index &gt;= size()})
      */
     public E remove(int index) {
         E rv = delegate.elementAt(index);
@@ -519,14 +503,11 @@
      * The removal is inclusive, so specifying a range of (1,5)
      * removes the component at index 1 and the component at index 5,
      * as well as all components in between.
-     * <p>
-     * Throws an <code>ArrayIndexOutOfBoundsException</code>
-     * if the index was invalid.
-     * Throws an <code>IllegalArgumentException</code> if
-     * <code>fromIndex &gt; toIndex</code>.
      *
      * @param      fromIndex the index of the lower end of the range
      * @param      toIndex   the index of the upper end of the range
+     * @throws ArrayIndexOutOfBoundsException if the index was invalid
+     * @throws IllegalArgumentException if {@code fromIndex &gt; toIndex}
      * @see        #remove(int)
      */
     public void removeRange(int fromIndex, int toIndex) {
@@ -539,11 +520,45 @@
         fireIntervalRemoved(this, fromIndex, toIndex);
     }
 
-    /*
-    public void addAll(Collection c) {
+    /**
+     * Adds all of the elements present in the collection to the list.
+     *
+     * @param c the collection which contains the elements to add
+     * @throws NullPointerException if {@code c} is null
+     */
+    public void addAll(Collection<? extends E> c) {
+        if (c.isEmpty()) {
+            return;
+        }
+
+        int startIndex = getSize();
+
+        delegate.addAll(c);
+        fireIntervalAdded(this, startIndex, getSize() - 1);
     }
 
-    public void addAll(int index, Collection c) {
+    /**
+     * Adds all of the elements present in the collection, starting
+     * from the specified index.
+     *
+     * @param index index at which to insert the first element from the
+     * specified collection
+     * @param c the collection which contains the elements to add
+     * @throws ArrayIndexOutOfBoundsException if {@code index} does not
+     * fall within the range of number of elements currently held
+     * @throws NullPointerException if {@code c} is null
+     */
+    public void addAll(int index, Collection<? extends E> c) {
+        if (index < 0 || index > getSize()) {
+            throw new ArrayIndexOutOfBoundsException("index out of range: " +
+                                                                       index);
+        }
+
+        if (c.isEmpty()) {
+            return;
+        }
+
+        delegate.addAll(index, c);
+        fireIntervalAdded(this, index, index + c.size() - 1);
     }
-    */
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/swing/JComboBox/DefaultComboBoxModelAddAllElementsTest.java	Wed May 02 15:11:54 2018 +0530
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 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.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 4842658
+ * @summary Tests the addAllElements, addAllElementsAt methods of DefaultComboBoxModel.
+ * @run main DefaultComboBoxModelAddAllElementsTest
+ */
+import javax.swing.DefaultComboBoxModel;
+import javax.swing.event.ListDataEvent;
+import javax.swing.event.ListDataListener;
+import java.util.ArrayList;
+import java.util.TreeSet;
+import java.util.Vector;
+import java.util.stream.IntStream;
+
+
+public class DefaultComboBoxModelAddAllElementsTest {
+    private static final int START = 0;
+    private static final int END = 50;
+    private static final Vector<Integer> vector =
+            IntStream.range(START, END).collect(Vector::new,
+                                                Vector::add,
+                                                Vector::addAll);
+
+    private static final TreeSet<Integer> set =
+            IntStream.range(START, END).collect(TreeSet::new,
+                                                TreeSet::add,
+                                                TreeSet::addAll);
+
+    private static final ArrayList<Integer> arrayList =
+            IntStream.range(START, END).collect(ArrayList::new,
+                                                ArrayList::add,
+                                                ArrayList::addAll);
+
+    public static void main(String[] args) {
+        checkAddAll();
+        checkAddAllWithIndex();
+        System.out.println("Test case passed.");
+    }
+
+    private static class MyListDataListener implements ListDataListener {
+        @Override public void intervalAdded(ListDataEvent e) {
+            if (e.getIndex1() - e.getIndex0() != END - START - 1) {
+                throw new RuntimeException("Test case failed. Expected " + (END - START) +
+                        " elements to be added, but only got " + (e.getIndex1() - e.getIndex0()));
+            }
+        }
+
+        @Override public void intervalRemoved(ListDataEvent e) {}
+        @Override public void contentsChanged(ListDataEvent e) {}
+    }
+
+    private static void checkAddAll() {
+        DefaultComboBoxModel<Integer> cm = new DefaultComboBoxModel<>();
+        cm.addListDataListener(new MyListDataListener());
+
+        try {
+            cm.addAll(arrayList);
+            System.out.println("Successfully added " + (END - START) + "elements.");
+        } catch (Exception e) {
+            throw new RuntimeException("Test case failed. " + e.getMessage());
+        }
+    }
+
+    private static void checkAddAllWithIndex() {
+        DefaultComboBoxModel<Integer> cm = new DefaultComboBoxModel<>();
+
+        cm.addListDataListener(new MyListDataListener());
+        cm.addAll(set);
+
+        try {
+            cm.addAll(START - 1, vector);
+            throw new RuntimeException("Test case failed. Expected failure not reported.");
+        } catch (ArrayIndexOutOfBoundsException e){
+            System.out.println("Encountered exception as expected, when trying to add elements" +
+                    "before the start of the list.");
+        }
+
+        try {
+            cm.addAll(15, vector);
+            System.out.println("Successfully added elements at a particular index");
+        } catch (Exception e) {
+            throw new RuntimeException("Unexpected failure: " + e.getMessage());
+        }
+
+        try {
+            cm.addAll(cm.getSize() + 1, vector);
+            throw new RuntimeException("Test case failed. Expected failure not reported.");
+        } catch (ArrayIndexOutOfBoundsException e){
+            System.out.println("Encountered exception as expected, when trying to add elements" +
+                    "after the end of the list.");
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/swing/JList/DefaultListModelAddAllTest.java	Wed May 02 15:11:54 2018 +0530
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 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.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 4842658
+ * @summary Tests the addAll methods of DefaultListModel.
+ * @run main DefaultListModelAddAllTest
+ */
+
+import javax.swing.DefaultListModel;
+import javax.swing.event.ListDataEvent;
+import javax.swing.event.ListDataListener;
+import java.util.ArrayList;
+import java.util.TreeSet;
+import java.util.Vector;
+import java.util.stream.IntStream;
+
+public class DefaultListModelAddAllTest {
+    private static final int START = 0;
+    private static final int END = 50;
+    private static final Vector<Integer> vector =
+            IntStream.range(START, END).collect(Vector::new,
+                                                Vector::add,
+                                                Vector::addAll);
+
+    private static final TreeSet<Integer> set =
+            IntStream.range(START, END).collect(TreeSet::new,
+                                                TreeSet::add,
+                                                TreeSet::addAll);
+
+    private static final ArrayList<Integer> arrayList =
+            IntStream.range(START, END).collect(ArrayList::new,
+                                                ArrayList::add,
+                                                ArrayList::addAll);
+
+    public static void main(String[] args) {
+        checkAddAll();
+        checkAddAllWithIndex();
+        System.out.println("Test case passed.");
+    }
+
+    private static class MyListDataListener implements ListDataListener {
+        @Override public void intervalAdded(ListDataEvent e) {
+            if (e.getIndex1() - e.getIndex0() != END - START - 1) {
+                throw new RuntimeException("Test case failed. Expected " + (END - START) +
+                        " elements to be added, but only got " + (e.getIndex1() - e.getIndex0()));
+            }
+        }
+
+        @Override public void intervalRemoved(ListDataEvent e) {}
+        @Override public void contentsChanged(ListDataEvent e) {}
+    }
+
+    private static void checkAddAll() {
+        DefaultListModel<Integer> lm = new DefaultListModel<>();
+        lm.addListDataListener(new MyListDataListener());
+
+        try {
+            lm.addAll(arrayList);
+            System.out.println("Successfully added " + (END - START) + "elements.");
+        } catch (Exception e) {
+            throw new RuntimeException("Test case failed. " + e.getMessage());
+        }
+    }
+
+    private static void checkAddAllWithIndex() {
+        DefaultListModel<Integer> lm = new DefaultListModel<>();
+
+        lm.addListDataListener(new MyListDataListener());
+        lm.addAll(set);
+
+        try {
+            lm.addAll(START - 1, vector);
+            throw new RuntimeException("Test case failed. Expected failure not reported.");
+        } catch (ArrayIndexOutOfBoundsException e){
+            System.out.println("Encountered exception as expected, when trying to add elements" +
+                    "before the start of the list.");
+        }
+
+        try {
+            lm.addAll(15, vector);
+            System.out.println("Successfully added elements at a particular index");
+        } catch (Exception e) {
+            throw new RuntimeException("Unexpected failure: " + e.getMessage());
+        }
+
+        try {
+            lm.addAll(lm.getSize() + 1, vector);
+            throw new RuntimeException("Test case failed. Expected failure not reported.");
+        } catch (ArrayIndexOutOfBoundsException e){
+            System.out.println("Encountered exception as expected, when trying to add elements" +
+                "after the end of the list.");
+        }
+    }
+}