I am not sure what is the reason for this but I can give an example of class that implements CharSequence
. It is java.nio.CharBuffer
.
Theoretically it could implement indexOf()
by calling charAt()
in loop. But it will not work as user expects. We cannot distinguish between 2 situations: character is not there yet and character is not there and will not be there. In second case indexOf()
should return -1 by contract. In first case it should wait until all bytes arrive. But CharBuffer belongs to non-blocking IO, so it cannot block.
I believe this explains at least one of the possible reasons.
EDIT:
Following very valuable comment by @Pacerier I want to add the following.
IMHO CharSequence
as a very generic interface that is used in different circumstances. The most well known implementors of this interface are String
, StringBuffer
and StringBuilder
that hold the whole content in data structure that allows direct access to any character. This is wrong however in general case. java.nio.CharBuffer
is an example of such case.
CharSequence
have anindexOf
method? – SurgicalindexOf
, they easily could.) – Disinicontains
? I guess only the API designers can answer these kind of questions (not voting to close, though, because that may have been discussed on some mailing list, and someone might know). – IrraindexOf
suits to every class that currently implementsCharSequence
. – Kiona