Hi Peter, The initial implementation was only optimized to call into String.compare if the arguments were string [1]. I proposed the current code a general form to catch java.nio.CharBuffer and any new JDK implementations of CharSequence + Comparable.
Naively, I lean towards "- CharSequence interface specification should be extended to require Comparable CharSeqeunces to implement lexicographical ordering". Jason 1: https://mail.openjdk.java.net/pipermail/core-libs-dev/2018-January/051124.html ________________________________________ From: core-libs-dev <core-libs-dev-boun...@openjdk.java.net> on behalf of Peter Levart <peter.lev...@gmail.com> Sent: Monday, March 11, 2019 4:04 AM To: core-libs-dev Subject: java.lang.CharSequence#compare - lexicographical ordering of custom Comparable CharSequence(s) 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