From 9413382eec489236a06827e7dbff33975b249bb1 Mon Sep 17 00:00:00 2001 From: Sven de Marothy Date: Tue, 21 Sep 2004 15:50:13 +0200 Subject: [PATCH] ByteBuffer.java (hashCode): Implemented. 2004-09-21 Sven de Marothy * java/nio/ByteBuffer.java (hashCode): Implemented. * java/nio/CharBuffer.java: Likewise. * java/nio/DoubleBuffer.java: Likewise. * java/nio/FloatBuffer.java: Likewise. * java/nio/LongBuffer.java: Likewise. * java/nio/IntBuffer.java: Likewise. * java/nio/ShortBuffer.java: Likewise. From-SVN: r87804 --- libjava/ChangeLog | 10 ++++++++++ libjava/java/nio/ByteBuffer.java | 20 ++++++++++++++++++-- libjava/java/nio/CharBuffer.java | 18 ++++++++++++++++-- libjava/java/nio/DoubleBuffer.java | 20 ++++++++++++++++++-- libjava/java/nio/FloatBuffer.java | 20 ++++++++++++++++++-- libjava/java/nio/IntBuffer.java | 20 ++++++++++++++++++-- libjava/java/nio/LongBuffer.java | 20 ++++++++++++++++++-- libjava/java/nio/ShortBuffer.java | 20 ++++++++++++++++++-- 8 files changed, 134 insertions(+), 14 deletions(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index e32ff8c4fbe..c27d6ee96bb 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,13 @@ +2004-09-21 Sven de Marothy + + * java/nio/ByteBuffer.java (hashCode): Implemented. + * java/nio/CharBuffer.java: Likewise. + * java/nio/DoubleBuffer.java: Likewise. + * java/nio/FloatBuffer.java: Likewise. + * java/nio/LongBuffer.java: Likewise. + * java/nio/IntBuffer.java: Likewise. + * java/nio/ShortBuffer.java: Likewise. + 2004-09-21 Andreas Tobler * javax/security/auth/x500/X500Principal.java: Fix some merge glitches. diff --git a/libjava/java/nio/ByteBuffer.java b/libjava/java/nio/ByteBuffer.java index 378e58827fa..e502e003199 100644 --- a/libjava/java/nio/ByteBuffer.java +++ b/libjava/java/nio/ByteBuffer.java @@ -260,11 +260,27 @@ public abstract class ByteBuffer extends Buffer /** * Calculates a hash code for this buffer. + * + * This is done with int arithmetic, + * where ** represents exponentiation, by this formula:
+ * s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + + * (s[limit()-1]+30)*31**(limit()-1). + * Where s is the buffer data. Note that the hashcode is dependent + * on buffer content, and therefore is not useful if the buffer + * content may change. + * + * @return the hash code */ public int hashCode () { - // FIXME: Check what SUN calculates here. - return super.hashCode (); + int hashCode = get(position()) + 31; + int multiplier = 1; + for (int i = position() + 1; i < limit(); ++i) + { + multiplier *= 31; + hashCode += (get(i) + 30)*multiplier; + } + return hashCode; } /** diff --git a/libjava/java/nio/CharBuffer.java b/libjava/java/nio/CharBuffer.java index 4ef257bed68..700c56750f3 100644 --- a/libjava/java/nio/CharBuffer.java +++ b/libjava/java/nio/CharBuffer.java @@ -297,11 +297,25 @@ public abstract class CharBuffer extends Buffer /** * Calculates a hash code for this buffer. + * + * This is done with int arithmetic, + * where ** represents exponentiation, by this formula:
+ * s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + + * (s[limit()-1]+30)*31**(limit()-1). + * Where s is the buffer data. Note that the hashcode is dependent + * on buffer content, and therefore is not useful if the buffer + * content may change. */ public int hashCode () { - // FIXME: Check what SUN calculates here. - return super.hashCode (); + int hashCode = get(position()) + 31; + int multiplier = 1; + for (int i = position() + 1; i < limit(); ++i) + { + multiplier *= 31; + hashCode += (get(i) + 30)*multiplier; + } + return hashCode; } /** diff --git a/libjava/java/nio/DoubleBuffer.java b/libjava/java/nio/DoubleBuffer.java index 10055cd08d9..dd16ee04962 100644 --- a/libjava/java/nio/DoubleBuffer.java +++ b/libjava/java/nio/DoubleBuffer.java @@ -243,11 +243,27 @@ public abstract class DoubleBuffer extends Buffer /** * Calculates a hash code for this buffer. + * + * This is done with long arithmetic, + * where ** represents exponentiation, by this formula:
+ * s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + + * (s[limit()-1]+30)*31**(limit()-1). + * Where s is the buffer data, in Double.doubleToLongBits() form + * Note that the hashcode is dependent on buffer content, + * and therefore is not useful if the buffer content may change. + * + * @return the hash code (casted to int) */ public int hashCode () { - // FIXME: Check what SUN calculates here. - return super.hashCode (); + long hashCode = Double.doubleToLongBits(get(position())) + 31; + long multiplier = 1; + for (int i = position() + 1; i < limit(); ++i) + { + multiplier *= 31; + hashCode += (Double.doubleToLongBits(get(i)) + 30)*multiplier; + } + return ((int)hashCode); } /** diff --git a/libjava/java/nio/FloatBuffer.java b/libjava/java/nio/FloatBuffer.java index 6d19503525f..2ead8cbc494 100644 --- a/libjava/java/nio/FloatBuffer.java +++ b/libjava/java/nio/FloatBuffer.java @@ -243,11 +243,27 @@ public abstract class FloatBuffer extends Buffer /** * Calculates a hash code for this buffer. + * + * This is done with int arithmetic, + * where ** represents exponentiation, by this formula:
+ * s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + + * (s[limit()-1]+30)*31**(limit()-1). + * Where s is the buffer data, in Float.floatToIntBits() form + * Note that the hashcode is dependent on buffer content, + * and therefore is not useful if the buffer content may change. + * + * @return the hash code */ public int hashCode () { - // FIXME: Check what SUN calculates here. - return super.hashCode (); + int hashCode = Float.floatToIntBits(get(position())) + 31; + int multiplier = 1; + for (int i = position() + 1; i < limit(); ++i) + { + multiplier *= 31; + hashCode += (Float.floatToIntBits(get(i)) + 30)*multiplier; + } + return hashCode; } /** diff --git a/libjava/java/nio/IntBuffer.java b/libjava/java/nio/IntBuffer.java index 68a71db3b53..814e0414bc6 100644 --- a/libjava/java/nio/IntBuffer.java +++ b/libjava/java/nio/IntBuffer.java @@ -243,11 +243,27 @@ public abstract class IntBuffer extends Buffer /** * Calculates a hash code for this buffer. + * + * This is done with int arithmetic, + * where ** represents exponentiation, by this formula:
+ * s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + + * (s[limit()-1]+30)*31**(limit()-1). + * Where s is the buffer data. Note that the hashcode is dependent + * on buffer content, and therefore is not useful if the buffer + * content may change. + * + * @return the hash code */ public int hashCode () { - // FIXME: Check what SUN calculates here. - return super.hashCode (); + int hashCode = get(position()) + 31; + int multiplier = 1; + for (int i = position() + 1; i < limit(); ++i) + { + multiplier *= 31; + hashCode += (get(i) + 30)*multiplier; + } + return hashCode; } /** diff --git a/libjava/java/nio/LongBuffer.java b/libjava/java/nio/LongBuffer.java index c4066b1d30b..6e9ef34e8eb 100644 --- a/libjava/java/nio/LongBuffer.java +++ b/libjava/java/nio/LongBuffer.java @@ -243,11 +243,27 @@ public abstract class LongBuffer extends Buffer /** * Calculates a hash code for this buffer. + * + * This is done with long arithmetic, + * where ** represents exponentiation, by this formula:
+ * s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + + * (s[limit()-1]+30)*31**(limit()-1). + * Where s is the buffer data. Note that the hashcode is dependent + * on buffer content, and therefore is not useful if the buffer + * content may change. + * + * @return the hash code (casted to int) */ public int hashCode () { - // FIXME: Check what SUN calculates here. - return super.hashCode (); + long hashCode = get(position()) + 31; + long multiplier = 1; + for (int i = position() + 1; i < limit(); ++i) + { + multiplier *= 31; + hashCode += (get(i) + 30)*multiplier; + } + return ((int)hashCode); } /** diff --git a/libjava/java/nio/ShortBuffer.java b/libjava/java/nio/ShortBuffer.java index 083039101e4..db03076b629 100644 --- a/libjava/java/nio/ShortBuffer.java +++ b/libjava/java/nio/ShortBuffer.java @@ -243,11 +243,27 @@ public abstract class ShortBuffer extends Buffer /** * Calculates a hash code for this buffer. + * + * This is done with int arithmetic, + * where ** represents exponentiation, by this formula:
+ * s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... + + * (s[limit()-1]+30)*31**(limit()-1). + * Where s is the buffer data. Note that the hashcode is dependent + * on buffer content, and therefore is not useful if the buffer + * content may change. + * + * @return the hash code */ public int hashCode () { - // FIXME: Check what SUN calculates here. - return super.hashCode (); + int hashCode = get(position()) + 31; + int multiplier = 1; + for (int i = position() + 1; i < limit(); ++i) + { + multiplier *= 31; + hashCode += (get(i) + 30)*multiplier; + } + return hashCode; } /** -- 2.11.4.GIT