covariant returns for CharBuffer.subSequence

Alan Bateman Alan.Bateman at Sun.COM
Fri Aug 1 08:39:45 UTC 2008


Martin Buchholz wrote:
> We would like to have return types of methods be the most covariant
> as is reasonable.  The only problem is compatibility.
> For classes that cannot be subclassed by users,
> changing covariant returns is almost 100% compatible.
> (We all know that no change is 100.000000% compatible)
>
> The spec for CharBuffer.CharSequence appears to guarantee that
> the returned object is itself a CharBuffer, so all we need to
> change is the return type:
>   
Right, buffers are not extensible (no public or protected constructors, 
etc.) so it does seem safe to take advantage of covariant returns.  
There are a number of other "opportunities" in this package that Iris 
and I have chatted about for jdk7. In particular the Buffer flip/etc. 
methods come up quite often as the more specific return type would 
facilitate better method invocation chaining. Are you interested in 
doing those too? I ask because there are a couple of existing RFEs for 
this (4774077 is the main one)? If not, then we can create a specific 
bug for subSequence to let you get this done.

I see your String updates have a dependency on this. I've only glanced 
at it so far but I assume by moving the casts you can separate this work 
if required.

-Alan.


> Hence:
>
> diff --git a/src/share/classes/java/nio/ByteBufferAs-X-Buffer.java
> b/src/share/classes/java/nio/ByteBufferAs-X-Buffer.java
> --- a/src/share/classes/java/nio/ByteBufferAs-X-Buffer.java
> +++ b/src/share/classes/java/nio/ByteBufferAs-X-Buffer.java
> @@ -186,7 +186,7 @@
>
>      // --- Methods to support CharSequence ---
>
> -    public CharSequence subSequence(int start, int end) {
> +    public CharBuffer subSequence(int start, int end) {
>          int pos = position();
>          int lim = limit();
>          assert (pos <= lim);
> diff --git a/src/share/classes/java/nio/Direct-X-Buffer.java
> b/src/share/classes/java/nio/Direct-X-Buffer.java
> --- a/src/share/classes/java/nio/Direct-X-Buffer.java
> +++ b/src/share/classes/java/nio/Direct-X-Buffer.java
> @@ -391,7 +391,7 @@
>
>      // --- Methods to support CharSequence ---
>
> -    public CharSequence subSequence(int start, int end) {
> +    public CharBuffer subSequence(int start, int end) {
>          int pos = position();
>          int lim = limit();
>          assert (pos <= lim);
> diff --git a/src/share/classes/java/nio/Heap-X-Buffer.java
> b/src/share/classes/java/nio/Heap-X-Buffer.java
> --- a/src/share/classes/java/nio/Heap-X-Buffer.java
> +++ b/src/share/classes/java/nio/Heap-X-Buffer.java
> @@ -566,7 +566,7 @@
>
>      // --- Methods to support CharSequence ---
>
> -    public CharSequence subSequence(int start, int end) {
> +    public CharBuffer subSequence(int start, int end) {
>          if ((start < 0)
>              || (end > length())
>              || (start > end))
> diff --git a/src/share/classes/java/nio/StringCharBuffer.java
> b/src/share/classes/java/nio/StringCharBuffer.java
> --- a/src/share/classes/java/nio/StringCharBuffer.java
> +++ b/src/share/classes/java/nio/StringCharBuffer.java
> @@ -99,7 +99,7 @@
>          return str.toString().substring(start + offset, end + offset);
>      }
>
> -    public final CharSequence subSequence(int start, int end) {
> +    public final CharBuffer subSequence(int start, int end) {
>          try {
>              int pos = position();
>              return new StringCharBuffer(str, -1,
> diff --git a/src/share/classes/java/nio/X-Buffer.java
> b/src/share/classes/java/nio/X-Buffer.java
> --- a/src/share/classes/java/nio/X-Buffer.java
> +++ b/src/share/classes/java/nio/X-Buffer.java
> @@ -1245,7 +1245,7 @@
>       *          If the preconditions on <tt>start</tt> and <tt>end</tt>
>       *          do not hold
>       */
> -    public abstract CharSequence subSequence(int start, int end);
> +    public abstract CharBuffer subSequence(int start, int end);
>
>
>      // --- Methods to support Appendable ---
>   




More information about the core-libs-dev mailing list