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.

5
  • So what you're asking is: Why doesn't CharSequence have an indexOf method? Commented Dec 6, 2011 at 6:37
  • 1
    StringBuffer and StringBuilder do have indexOf methods, though... Commented Dec 6, 2011 at 6:37
  • @Thilo - 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.) Commented Dec 6, 2011 at 6:46
  • 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). Commented Dec 6, 2011 at 6:49
  • @Shakedown Yes, that's my question, because indexOf suits to every class that currently implements CharSequence. Commented Dec 6, 2011 at 6:57

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
@Pacerier, thank you for your comment. Please take a look on addition to my answer that hopefully moves the horse forward. :)
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.
3

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

Comments

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.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.