migrator / guava-libraries-2

Guava: Google Core Libraries for Java 1.6+
0 stars 0 forks source link

ImmutableSortedSet with 1 non-Comparable element #39

Open migrator opened 10 years ago

migrator commented 10 years ago

Creating a singleton immutable sorted set by calling of() with a non-Comparable object is not allowed (via "FauxverideShim"). However, one can be created by first instantiating a single element Collection and creating a copyOf() it.

Example (throws Exception): Object notComparable = new Object(); ImmutableSortedSet.of(notComparable);

Example (works): Object notComparable = new Object(); ImmutableSortedSet.copyOf(ImmutableSet.of(notComparable));

I submit that they should both work or both fail. While technically a singleton non-comparable SortedSet violates the SortedSet contract ("All elements inserted into a sorted set must implement the Comparable interface"), it make senses to me that an immutable SortedSet of 1 element could be an exception (and thus not throw an Exception). In fact, TreeSet does not fail with a single non-Comparable element:

SortedSet sortedSet = new TreeSet(); sortedSet.add(notComparable);

relevance: 2

migrator commented 10 years ago

summary: Not Defined

Nice find. I believe we should make copyOf fail.

I believe the TreeSet bug you mention is finally fixed for JDK 8.

status Not Defined creator: kevinb@google.com created at: Aug 20, 2014

migrator commented 10 years ago

summary: Not Defined

I checked and you're correct that the TreeSet bug is fixed (in a Java 7 release). For enterprise (read too boring to enumerate here) reasons, we're still on a late build of Java 6. I retract my final comment and agree with your assessment.

status Not Defined creator: tadam...@gmail.com created at: Aug 20, 2014