This is a better implementation for java.awt.Font.hashCode(). Before we
created a new identifying string (urgs) and got the hashcode from that
(string hashcode: urgs too). Now we compute the hashcode of the font's
significant properties (name, size, style and transform). In addition to
that we now cache the hashcode, because those properties never change
for a font object.
2007-09-21 Roman Kennke <[EMAIL PROTECTED]>
* java/awt/Font.java
(hashCode): New field. Stores a cached hash code.
(hashCode()): Re-implemented. Don't create new string here, instead
make hashcode of name, style, size and transform. Cache hashcode.
/Roman
--
Dipl.-Inform. (FH) Roman Kennke, Software Engineer, http://kennke.org
aicas Allerton Interworks Computer Automated Systems GmbH
Haid-und-Neu-Straße 18 * D-76131 Karlsruhe * Germany
http://www.aicas.com * Tel: +49-721-663 968-0
USt-Id: DE216375633, Handelsregister HRB 109481, AG Karlsruhe
Geschäftsführer: Dr. James J. Hunt
Index: java/awt/Font.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/Font.java,v
retrieving revision 1.40
diff -u -1 -0 -r1.40 Font.java
--- java/awt/Font.java 25 Jun 2007 11:25:52 -0000 1.40
+++ java/awt/Font.java 21 Sep 2007 08:32:57 -0000
@@ -222,20 +222,25 @@
*/
protected int style;
//Serialization constant
private static final long serialVersionUID = -4206021311591459213L;
// The ClasspathToolkit-provided peer which implements this font
private transient ClasspathFontPeer peer;
+ /**
+ * The cached hashcode. A value of 0 (default initialized) means that the
+ * hashcode is not computed yet.
+ */
+ private transient int hashCode;
/**
* Creates a <code>Font</code> object from the specified string, which
* is in one of the following formats:
* <p>
* <ul>
* <li>fontname-style-pointsize
* <li>fontname-style
* <li>fontname-pointsize
* <li>fontname
@@ -1311,21 +1316,35 @@
}
/**
* Returns a hash value for this font.
*
* @return A hash for this font.
*/
public int hashCode()
{
- return this.toString().hashCode();
+ // We cache the hashcode. This makes sense, because the font wouldn't
+ // change the relevant properties.
+ if (hashCode == 0)
+ {
+ hashCode = getName().hashCode() ^ getTransform().hashCode() ^ getSize()
+ ^ getStyle();
+ // In the rare case when the above yields 0, we set this to some other
+ // value to avoid recomputing over and over again. This is still
+ // conform to the specification of hashCode().
+ if (hashCode == 0)
+ {
+ hashCode = -1;
+ }
+ }
+ return hashCode;
}
/**
* Tests whether or not the specified object is equal to this font. This
* will be true if and only if:
* <P>
* <ul>
* <li>The object is not <code>null</code>.
* <li>The object is an instance of <code>Font</code>.