mkodekar / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

ImmutableSortedSet with 1 non-Comparable element #1835

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 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<Object> sortedSet = new TreeSet<Object>();
  sortedSet.add(notComparable);

Original issue reported on code.google.com by tadam...@gmail.com on 19 Aug 2014 at 11:20

GoogleCodeExporter commented 9 years ago
Nice find. I believe we should make copyOf fail.

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

Original comment by kevinb@google.com on 20 Aug 2014 at 1:18

GoogleCodeExporter commented 9 years ago
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.

Original comment by tadam...@gmail.com on 20 Aug 2014 at 5:30

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:08

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:07