You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2015/02/17 23:03:00 UTC

svn commit: r1660515 - in /commons/proper/collections/trunk/src: changes/changes.xml main/java/org/apache/commons/collections4/bag/TreeBag.java test/java/org/apache/commons/collections4/bag/TreeBagTest.java

Author: tn
Date: Tue Feb 17 22:02:59 2015
New Revision: 1660515

URL: http://svn.apache.org/r1660515
Log:
[COLLECTIONS-555] Added clarification to TreeBag#add(Object) wrt null arguments. Thanks to M Kim.

Modified:
    commons/proper/collections/trunk/src/changes/changes.xml
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/bag/TreeBag.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/bag/TreeBagTest.java

Modified: commons/proper/collections/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1660515&r1=1660514&r2=1660515&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Tue Feb 17 22:02:59 2015
@@ -22,6 +22,9 @@
   <body>
 
   <release version="4.1" date="TBD" description="">
+    <action issue="COLLECTIONS-555" dev="tn" type="fix" due-to="M Kim">
+      Added clarification to javadoc of "TreeBag#add(Object)" wrt null arguments.
+    </action>
     <action issue="COLLECTIONS-427" dev="tn" type="add" due-to="Gonçalo Marques">
       Added "toString(...)" methods to newly created "IteratorUtils" class to get a
       string representation of an Iterable instance similar to "Arrays#toString(...)".

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/bag/TreeBag.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/bag/TreeBag.java?rev=1660515&r1=1660514&r2=1660515&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/bag/TreeBag.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/bag/TreeBag.java Tue Feb 17 22:02:59 2015
@@ -80,10 +80,15 @@ public class TreeBag<E> extends Abstract
      *
      * @throws IllegalArgumentException if the object to be added does not implement
      * {@link Comparable} and the {@link TreeBag} is using natural ordering
+     * @throws NullPointerException if the specified key is null and this bag uses
+     * natural ordering, or its comparator does not permit null keys
      */
     @Override
     public boolean add(final E object) {
         if(comparator() == null && !(object instanceof Comparable)) {
+            if (object == null) {
+                throw new NullPointerException();
+            }
             throw new IllegalArgumentException("Objects of type " + object.getClass() + " cannot be added to " +
                                                "a naturally ordered TreeBag as it does not implement Comparable");
         }

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/bag/TreeBagTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/bag/TreeBagTest.java?rev=1660515&r1=1660514&r2=1660515&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/bag/TreeBagTest.java (original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/bag/TreeBagTest.java Tue Feb 17 22:02:59 2015
@@ -16,6 +16,8 @@
  */
 package org.apache.commons.collections4.bag;
 
+import java.util.Comparator;
+
 import junit.framework.Test;
 
 import org.apache.commons.collections4.Bag;
@@ -64,6 +66,32 @@ public class TreeBagTest<T> extends Abst
             // expected;
         }
     }
+
+    public void testCollections555() {
+        final Bag<Object> bag = new TreeBag<Object>();
+        try {
+            bag.add(null);
+            fail("NullPointerException expected");
+        } catch(final NullPointerException npe) {
+            // expected;
+        }
+        
+        final Bag<String> bag2 = new TreeBag<String>(new Comparator<String>() {
+            @Override
+            public int compare(String o1, String o2) {
+                return o1.compareTo(o2);
+            }
+        });
+        try {
+            // jdk bug: adding null to an empty TreeMap works
+            // thus ensure that the bag is not empty before adding null
+            bag2.add("a");
+            bag2.add(null);
+            fail("NullPointerException expected");
+        } catch(final NullPointerException npe) {
+            // expected;
+        }
+    }
 
     public void testOrdering() {
         final Bag<T> bag = setupBag();