java.lang.CharSequence#compare - lexicographical ordering of custom Comparable CharSequence(s)

Peter Levart peter.levart at gmail.com
Mon Mar 11 09:04:40 UTC 2019


Hello,

I stumbled on the specification of a static utility method 
CharSequence#compare introduced in Java 11 which says:

      * Compares two {@code CharSequence} instances lexicographically. 
Returns a
      * negative value, zero, or a positive value if the first sequence 
is lexicographically
      * less than, equal to, or greater than the second, respectively.

The implementation of this method does the following:

     public static int compare(CharSequence cs1, CharSequence cs2) {
         if (Objects.requireNonNull(cs1) == Objects.requireNonNull(cs2)) {
             return 0;
         }

         if (cs1.getClass() == cs2.getClass() && cs1 instanceof 
Comparable) {
             return ((Comparable<Object>) cs1).compareTo(cs2);
         }

         ... lexical comparison char-by-char ...


This means that if the method is called with two instances of the same 
class that also implements Comparable, the comparison is delegated to 
the .compareTo() method of that class. But CharSequence interface 
neither extends Comparable nor does it specify  what such CharSequence 
implementations should conform to when they also implement Comparable. 
There could be a perfectly valid custom CharSequence implementation that 
implemented Comparable which doesn't order instances lexicographically. 
The guarantee this method gives is not respected in this case.

So, what shall be done?

- nothing
- CharSequence interface specification should be extended to require 
Comparable CharSeqeunces to implement lexicographical ordering
- CharSequence#compare method specification should be extended to inform 
the users about that delegation to Comparable CharSequence(s)
- CharSequence#compare method implementation should be changed to not 
delegate to .compareTo() (at all or just for "unknown" Comparable 
CharSequence(s))


Regards, Peter



More information about the core-libs-dev mailing list