Why isn't method String.indexOf part of interface CharSequence?
Asked Answered
K

3

9

I can't see any drawbacks in making String.indexOf part of the interface CharSequence. The benefit would be that other classes like StringBuffer or StringBuilder would need to implement the indexOf methods too.

So is there any design reason why indexOf should only be part of String?

Thank you.

Kiona answered 6/12, 2011 at 6:33 Comment(5)
So what you're asking is: Why doesn't CharSequence have an indexOf method?Surgical
StringBuffer and StringBuilder do have indexOf methods, though...Irra
@Irra - Which sort of begs the question of why doesn't CharSequence specify that behavior? (And although java.nio.CharBuffer and javax.swing.text.Segment don't implement indexOf, they easily could.)Disini
Why doesn't StringBuilder have contains? 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).Irra
@Shakedown Yes, that's my question, because indexOf suits to every class that currently implements CharSequence.Kiona
P
8

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.

Pulido answered 6/12, 2011 at 7:16 Comment(3)
That's putting the cart before the horse. You merely argued that CharBuffer shouldn't be a CharSequence, and not that indexof shouldn't be in CharSequence.Synsepalous
@Pacerier, thank you for your comment. Please take a look on addition to my answer that hopefully moves the horse forward. :)Pulido
It seems that there ought to be(but there isn't sadly) a separate interface that provides indexOf() methods that both String and StringBuilder implement, so that you can pass either into a method and easily call indexOf() off of it.Heigl
M
3

I think that's merely an oversight, as indexOf operation makes sense for any sort of sequence.

Marchellemarcher answered 6/12, 2011 at 7:12 Comment(0)
F
2

Java 8 may solve some of these problems. It will allow default implementations on interfaces. e.g.

interface List {
    void sort() default Collections.sort(this);
}

This allow additional methods to be added to interfaces without placing a burden on all implementers to implement that method.

Fund answered 6/12, 2011 at 8:14 Comment(2)
How would this work for for example java.nio.CharBuffer in the case that AlexR mentions in his answer? A default implementation of indexOf() wouldn't solve the problem he mentions.Ailurophobe
If you use a method on a CharBuffer or StringBuilder or any mutable object, you don't have a return code for its false now but might be true if you change something. It could equally be true now but false if you change something such as the data, position or limit is changed. You can only return true/false based on what is there now.Fund

© 2022 - 2024 — McMap. All rights reserved.