Applying my patch from COLLECTIONS-265. TreeBag no longer accepts non-Comparable classes when it naturally ordered (ie: no comparator has been set)

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@641166 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2008-03-26 06:08:52 +00:00
parent 5a3a0041a0
commit 975baeba61
4 changed files with 38 additions and 0 deletions

View File

@ -62,6 +62,14 @@ public class TreeBag extends DefaultMapBag implements SortedBag {
addAll(coll);
}
public boolean add(Object o) {
if(comparator() == null && !(o instanceof Comparable)) {
throw new IllegalArgumentException("Objects of type " + o.getClass() + " cannot be added to " +
"a naturally ordered TreeBag as it does not implement Comparable");
}
return super.add(o);
}
public Object first() {
return ((SortedMap) getMap()).firstKey();
}

View File

@ -80,6 +80,15 @@ public class TreeBag
addAll(coll);
}
//-----------------------------------------------------------------------
public boolean add(Object o) {
if(comparator() == null && !(o instanceof Comparable)) {
throw new IllegalArgumentException("Objects of type " + o.getClass() + " cannot be added to " +
"a naturally ordered TreeBag as it does not implement Comparable");
}
return super.add(o);
}
//-----------------------------------------------------------------------
public Object first() {
return ((SortedMap) getMap()).firstKey();

View File

@ -70,4 +70,15 @@ public class TestTreeBag extends AbstractTestBag {
assertEquals("Should get last key",
"D", ((SortedBag)bag).last());
}
public void testCollections265() {
Bag bag = new TreeBag();
try {
bag.add(new Object());
fail("IllegalArgumentException expected");
} catch(IllegalArgumentException iae) {
// expected;
}
}
}

View File

@ -71,6 +71,16 @@ public class TestTreeBag extends AbstractTestBag {
assertEquals("Should get last key",
"D", ((SortedBag)bag).last());
}
public void testCollections265() {
Bag bag = new TreeBag();
try {
bag.add(new Object());
fail("IllegalArgumentException expected");
} catch(IllegalArgumentException iae) {
// expected;
}
}
public String getCompatibilityVersion() {
return "3";