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:
parent
5a3a0041a0
commit
975baeba61
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -72,6 +72,16 @@ public class TestTreeBag extends AbstractTestBag {
|
|||
"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";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue