Author: bayard
Date: Tue Mar 25 23:08:52 2008
New Revision: 641166
URL: http://svn.apache.org/viewvc?rev=641166&view=rev
Log:
Applying my patch from COLLECTIONS-265. TreeBag no longer accepts
non-Comparable classes when it naturally ordered (ie: no comparator has been
set)
Modified:
commons/proper/collections/trunk/src/java/org/apache/commons/collections/TreeBag.java
commons/proper/collections/trunk/src/java/org/apache/commons/collections/bag/TreeBag.java
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestTreeBag.java
commons/proper/collections/trunk/src/test/org/apache/commons/collections/bag/TestTreeBag.java
Modified:
commons/proper/collections/trunk/src/java/org/apache/commons/collections/TreeBag.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/TreeBag.java?rev=641166&r1=641165&r2=641166&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/java/org/apache/commons/collections/TreeBag.java
(original)
+++
commons/proper/collections/trunk/src/java/org/apache/commons/collections/TreeBag.java
Tue Mar 25 23:08:52 2008
@@ -62,6 +62,14 @@
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();
}
Modified:
commons/proper/collections/trunk/src/java/org/apache/commons/collections/bag/TreeBag.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/bag/TreeBag.java?rev=641166&r1=641165&r2=641166&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/java/org/apache/commons/collections/bag/TreeBag.java
(original)
+++
commons/proper/collections/trunk/src/java/org/apache/commons/collections/bag/TreeBag.java
Tue Mar 25 23:08:52 2008
@@ -81,6 +81,15 @@
}
//-----------------------------------------------------------------------
+ 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();
}
Modified:
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestTreeBag.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestTreeBag.java?rev=641166&r1=641165&r2=641166&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestTreeBag.java
(original)
+++
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestTreeBag.java
Tue Mar 25 23:08:52 2008
@@ -70,4 +70,15 @@
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;
+ }
+ }
+
}
Modified:
commons/proper/collections/trunk/src/test/org/apache/commons/collections/bag/TestTreeBag.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/bag/TestTreeBag.java?rev=641166&r1=641165&r2=641166&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/test/org/apache/commons/collections/bag/TestTreeBag.java
(original)
+++
commons/proper/collections/trunk/src/test/org/apache/commons/collections/bag/TestTreeBag.java
Tue Mar 25 23:08:52 2008
@@ -71,6 +71,16 @@
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";