[COLLECTIONS-555] Added clarification to TreeBag#add(Object) wrt null arguments. Thanks to M Kim.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1660515 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
39513f9142
commit
583d96b089
|
@ -22,6 +22,9 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<release version="4.1" date="TBD" description="">
|
<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">
|
<action issue="COLLECTIONS-427" dev="tn" type="add" due-to="Gonçalo Marques">
|
||||||
Added "toString(...)" methods to newly created "IteratorUtils" class to get a
|
Added "toString(...)" methods to newly created "IteratorUtils" class to get a
|
||||||
string representation of an Iterable instance similar to "Arrays#toString(...)".
|
string representation of an Iterable instance similar to "Arrays#toString(...)".
|
||||||
|
|
|
@ -80,10 +80,15 @@ public class TreeBag<E> extends AbstractMapBag<E> implements SortedBag<E>, Seria
|
||||||
*
|
*
|
||||||
* @throws IllegalArgumentException if the object to be added does not implement
|
* @throws IllegalArgumentException if the object to be added does not implement
|
||||||
* {@link Comparable} and the {@link TreeBag} is using natural ordering
|
* {@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
|
@Override
|
||||||
public boolean add(final E object) {
|
public boolean add(final E object) {
|
||||||
if(comparator() == null && !(object instanceof Comparable)) {
|
if(comparator() == null && !(object instanceof Comparable)) {
|
||||||
|
if (object == null) {
|
||||||
|
throw new NullPointerException();
|
||||||
|
}
|
||||||
throw new IllegalArgumentException("Objects of type " + object.getClass() + " cannot be added to " +
|
throw new IllegalArgumentException("Objects of type " + object.getClass() + " cannot be added to " +
|
||||||
"a naturally ordered TreeBag as it does not implement Comparable");
|
"a naturally ordered TreeBag as it does not implement Comparable");
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.collections4.bag;
|
package org.apache.commons.collections4.bag;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
|
|
||||||
import org.apache.commons.collections4.Bag;
|
import org.apache.commons.collections4.Bag;
|
||||||
|
@ -65,6 +67,32 @@ public class TreeBagTest<T> extends AbstractSortedBagTest<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
public void testOrdering() {
|
||||||
final Bag<T> bag = setupBag();
|
final Bag<T> bag = setupBag();
|
||||||
assertEquals("Should get elements in correct order", "A", bag.toArray()[0]);
|
assertEquals("Should get elements in correct order", "A", bag.toArray()[0]);
|
||||||
|
|
Loading…
Reference in New Issue