DoubleBuffer.compareTo is not anti-symmetric

Mark Thornton mthornton at optrak.co.uk
Fri Nov 20 12:04:21 PST 2009


Martin Buchholz wrote:
>
>
> On Fri, Nov 20, 2009 at 11:46, Mark Thornton <mthornton at optrak.co.uk 
> <mailto:mthornton at optrak.co.uk>> wrote:
>
>
>     Is the %b format specification really what you intended? Use %d
>     instead!
>
>
> Of course you're right.  Here's what I really meant:
>  
> import java.nio.*;
>
> public class BufferCompare {
>     public static void main(String[] args) {
>         DoubleBuffer b1 = DoubleBuffer.wrap(new double[] { Double.NaN });
>         DoubleBuffer b2 = DoubleBuffer.wrap(new double[] { 1.0 });
>         System.out.printf("%d%n", b1.compareTo(b2));
>         System.out.printf("%d%n", b2.compareTo(b1));
>     }
> }
> ==> javac -source 1.6 -Xlint:all BufferCompare.java
> ==> java -esa -ea BufferCompare
> 1
> 1
>
Right. This is no doubt the result of the difference between the 
behaviour of the comparison operators (<, >, etc) on double versus 
Double.compare(double, double). DoubleBuffer.compareTo should be 
implemented using Double.compare(). A nicer fix would be possible if all 
the primitive wrappers had static compare methods (e.g. 
Integer.compare(int,int), etc).

Mark Thornton



More information about the nio-dev mailing list