This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch: java.nio - compareTo fixed in all buffer classes


Hi list,


I just merged the attached patch from GNU classpath to fix the compareTo 
methods in all buffer classes.They were totally bogus before. I wonder 
what the one who wrote them had drunken ...


Michael


2004-05-04  Michael Koch  <konqueror@gmx.de>

	* java/nio/ByteBuffer.java,
	java/nio/CharBuffer.java,
	java/nio/DoubleBuffer.java,
	java/nio/FloatBuffer.java,
	java/nio/IntBuffer.java,
	java/nio/LongBuffer.java,
	java/nio/ShortBuffer.java:
	(compareTo): Fixed bogus implementation in all buffer classes.

Index: java/nio/ByteBuffer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/ByteBuffer.java,v
retrieving revision 1.16
diff -u -r1.16 ByteBuffer.java
--- java/nio/ByteBuffer.java	16 Feb 2004 20:00:33 -0000	1.16
+++ java/nio/ByteBuffer.java	4 May 2004 21:29:11 -0000
@@ -293,32 +293,27 @@
    */
   public int compareTo (Object obj)
   {
-    ByteBuffer a = (ByteBuffer) obj;
+    ByteBuffer other = (ByteBuffer) obj;
 
-    if (a.remaining () != remaining ())
-      return 1;
-
-    if (! hasArray () ||
-        ! a.hasArray ())
+    int num = Math.min(remaining(), other.remaining());
+    int pos_this = position();
+    int pos_other = other.position();
+    
+    for (int count = 0; count < num; count++)
       {
-        return 1;
+        byte a = get(pos_this++);
+	byte b = other.get(pos_other++);
+      	 
+	if (a == b)
+	  continue;
+      	   
+	if (a < b)
+	  return -1;
+      	   
+	return 1;
       }
-
-    int r = remaining ();
-    int i1 = position ();
-    int i2 = a.position ();
-
-    for (int i = 0; i < r; i++)
-      {
-        int t = (int) (get (i1) - a.get (i2));
-
-        if (t != 0)
-          {
-            return (int) t;
-          }
-      }
-
-    return 0;
+      
+    return remaining() - other.remaining();
   }
 
   /**
Index: java/nio/CharBuffer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/CharBuffer.java,v
retrieving revision 1.15
diff -u -r1.15 CharBuffer.java
--- java/nio/CharBuffer.java	16 Feb 2004 19:53:26 -0000	1.15
+++ java/nio/CharBuffer.java	4 May 2004 21:29:11 -0000
@@ -1,5 +1,5 @@
 /* CharBuffer.java -- 
-   Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -310,32 +310,27 @@
    */
   public int compareTo (Object obj)
   {
-    CharBuffer a = (CharBuffer) obj;
+    CharBuffer other = (CharBuffer) obj;
 
-    if (a.remaining () != remaining ())
-      return 1;
-
-    if (! hasArray () ||
-        ! a.hasArray ())
-      {
-        return 1;
-      }
-
-    int r = remaining ();
-    int i1 = position ();
-    int i2 = a.position ();
-
-    for (int i = 0; i < r; i++)
+    int num = Math.min(remaining(), other.remaining());
+    int pos_this = position();
+    int pos_other = other.position();
+    
+    for (int count = 0; count < num; count++)
       {
-        int t = (int) (get (i1) - a.get (i2));
-
-        if (t != 0)
-          {
-            return (int) t;
-          }
+	 char a = get(pos_this++);
+	 char b = other.get(pos_other++);
+      	 
+	 if (a == b)
+	   continue;
+      	   
+	 if (a < b)
+	   return -1;
+      	   
+	 return 1;
       }
-
-    return 0;
+      
+     return remaining() - other.remaining();
   }
 
   /**
Index: java/nio/DoubleBuffer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/DoubleBuffer.java,v
retrieving revision 1.9
diff -u -r1.9 DoubleBuffer.java
--- java/nio/DoubleBuffer.java	16 Feb 2004 19:53:26 -0000	1.9
+++ java/nio/DoubleBuffer.java	4 May 2004 21:29:11 -0000
@@ -1,5 +1,5 @@
 /* DoubleBuffer.java -- 
-   Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -265,32 +265,27 @@
    */
   public int compareTo (Object obj)
   {
-    DoubleBuffer a = (DoubleBuffer) obj;
+    DoubleBuffer other = (DoubleBuffer) obj;
 
-    if (a.remaining () != remaining ())
-      return 1;
-
-    if (! hasArray () ||
-        ! a.hasArray ())
-      {
-        return 1;
-      }
-
-    int r = remaining ();
-    int i1 = position ();
-    int i2 = a.position ();
-
-    for (int i = 0; i < r; i++)
+    int num = Math.min(remaining(), other.remaining());
+    int pos_this = position();
+    int pos_other = other.position();
+    
+    for (int count = 0; count < num; count++)
       {
-        int t = (int) (get (i1) - a.get (i2));
-
-        if (t != 0)
-          {
-            return (int) t;
-          }
+	double a = get(pos_this++);
+	double b = other.get(pos_other++);
+      	 
+	if (a == b)
+	  continue;
+      	   
+	if (a < b)
+	  return -1;
+      	   
+	return 1;
       }
-
-    return 0;
+      
+    return remaining() - other.remaining();
   }
 
   /**
Index: java/nio/FloatBuffer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/FloatBuffer.java,v
retrieving revision 1.9
diff -u -r1.9 FloatBuffer.java
--- java/nio/FloatBuffer.java	16 Feb 2004 19:53:26 -0000	1.9
+++ java/nio/FloatBuffer.java	4 May 2004 21:29:11 -0000
@@ -1,5 +1,5 @@
 /* FloatBuffer.java -- 
-   Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -265,32 +265,27 @@
    */
   public int compareTo (Object obj)
   {
-    FloatBuffer a = (FloatBuffer) obj;
+    FloatBuffer other = (FloatBuffer) obj;
 
-    if (a.remaining () != remaining ())
-      return 1;
-
-    if (! hasArray () ||
-        ! a.hasArray ())
-      {
-        return 1;
-      }
-
-    int r = remaining ();
-    int i1 = position ();
-    int i2 = a.position ();
-
-    for (int i = 0; i < r; i++)
+    int num = Math.min(remaining(), other.remaining());
+    int pos_this = position();
+    int pos_other = other.position();
+    
+    for (int count = 0; count < num; count++)
       {
-        int t = (int) (get (i1) - a.get (i2));
-
-        if (t != 0)
-          {
-            return (int) t;
-          }
+	float a = get(pos_this++);
+	float b = other.get(pos_other++);
+      	 
+	if (a == b)
+	  continue;
+      	   
+	if (a < b)
+	  return -1;
+      	   
+	return 1;
       }
-
-    return 0;
+      
+    return remaining() - other.remaining();
   }
 
   /**
Index: java/nio/IntBuffer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/IntBuffer.java,v
retrieving revision 1.9
diff -u -r1.9 IntBuffer.java
--- java/nio/IntBuffer.java	16 Feb 2004 19:53:26 -0000	1.9
+++ java/nio/IntBuffer.java	4 May 2004 21:29:11 -0000
@@ -1,5 +1,5 @@
 /* IntBuffer.java -- 
-   Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -265,32 +265,27 @@
    */
   public int compareTo (Object obj)
   {
-    IntBuffer a = (IntBuffer) obj;
+    IntBuffer other = (IntBuffer) obj;
 
-    if (a.remaining () != remaining ())
-      return 1;
-
-    if (! hasArray () ||
-        ! a.hasArray ())
-      {
-        return 1;
-      }
-
-    int r = remaining ();
-    int i1 = position ();
-    int i2 = a.position ();
-
-    for (int i = 0; i < r; i++)
+    int num = Math.min(remaining(), other.remaining());
+    int pos_this = position();
+    int pos_other = other.position();
+    
+    for (int count = 0; count < num; count++)
       {
-        int t = (int) (get (i1) - a.get (i2));
-
-        if (t != 0)
-          {
-            return (int) t;
-          }
+      	 int a = get(pos_this++);
+      	 int b = other.get(pos_other++);
+      	 
+      	 if (a == b)
+      	   continue;
+      	   
+      	 if (a < b)
+      	   return -1;
+      	   
+      	 return 1;
       }
-
-    return 0;
+      
+     return remaining() - other.remaining();
   }
 
   /**
Index: java/nio/LongBuffer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/LongBuffer.java,v
retrieving revision 1.9
diff -u -r1.9 LongBuffer.java
--- java/nio/LongBuffer.java	16 Feb 2004 19:53:27 -0000	1.9
+++ java/nio/LongBuffer.java	4 May 2004 21:29:11 -0000
@@ -1,5 +1,5 @@
 /* LongBuffer.java -- 
-   Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -265,32 +265,27 @@
    */
   public int compareTo (Object obj)
   {
-    LongBuffer a = (LongBuffer) obj;
+    LongBuffer other = (LongBuffer) obj;
 
-    if (a.remaining () != remaining ())
-      return 1;
-
-    if (! hasArray () ||
-        ! a.hasArray ())
-      {
-        return 1;
-      }
-
-    int r = remaining ();
-    int i1 = position ();
-    int i2 = a.position ();
-
-    for (int i = 0; i < r; i++)
+    int num = Math.min(remaining(), other.remaining());
+    int pos_this = position();
+    int pos_other = other.position();
+    
+    for (int count = 0; count < num; count++)
       {
-        int t = (int) (get (i1) - a.get (i2));
-
-        if (t != 0)
-          {
-            return (int) t;
-          }
+	 long a = get(pos_this++);
+	 long b = other.get(pos_other++);
+      	 
+	 if (a == b)
+	   continue;
+      	   
+	 if (a < b)
+	   return -1;
+      	   
+	 return 1;
       }
-
-    return 0;
+      
+     return remaining() - other.remaining();
   }
 
   /**
Index: java/nio/ShortBuffer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/ShortBuffer.java,v
retrieving revision 1.9
diff -u -r1.9 ShortBuffer.java
--- java/nio/ShortBuffer.java	16 Feb 2004 19:53:27 -0000	1.9
+++ java/nio/ShortBuffer.java	4 May 2004 21:29:11 -0000
@@ -1,5 +1,5 @@
 /* ShortBuffer.java -- 
-   Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -265,32 +265,27 @@
    */
   public int compareTo (Object obj)
   {
-    ShortBuffer a = (ShortBuffer) obj;
+    ShortBuffer other = (ShortBuffer) obj;
 
-    if (a.remaining () != remaining ())
-      return 1;
-
-    if (! hasArray () ||
-        ! a.hasArray ())
-      {
-        return 1;
-      }
-
-    int r = remaining ();
-    int i1 = position ();
-    int i2 = a.position ();
-
-    for (int i = 0; i < r; i++)
+    int num = Math.min(remaining(), other.remaining());
+    int pos_this = position();
+    int pos_other = other.position();
+    
+    for (int count = 0; count < num; count++)
       {
-        int t = (int) (get (i1) - a.get (i2));
-
-        if (t != 0)
-          {
-            return (int) t;
-          }
+	 short a = get(pos_this++);
+	 short b = other.get(pos_other++);
+      	 
+	 if (a == b)
+	   continue;
+      	   
+	 if (a < b)
+	   return -1;
+      	   
+	 return 1;
       }
-
-    return 0;
+      
+     return remaining() - other.remaining();
   }
 
   /**

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]