commit-classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Patch: FYI: java.util.Locale


From: Bryce McKinlay
Subject: Patch: FYI: java.util.Locale
Date: Fri, 02 Jul 2004 15:40:53 -0400
User-agent: Mozilla Thunderbird 0.5 (X11/20040502)

This patch cleans up Locale by improving the readObject/writeObject implementations. This allows us to remove synchronization on the hashCode and writeObject methods.

I'm checking this in to libgcj. I can't connect to classpath cvs right now but I'll try to remember to check it in there too when its back up.

Regards

Bryce


2004-07-02  Bryce McKinlay  <address@hidden>

        * java/util/Locale.java (hashcode): Made transient.
        (hashCode): No longer synchronized.
        (equals): Remove comment.
        (writeObject): No longer synchronized. Implement using writeObject 
        calls instead of tweaking hashCode field. Update doc.
        (readObject): Implement using readObject calls.

Index: Locale.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/util/Locale.java,v
retrieving revision 1.17
diff -u -r1.17 Locale.java
--- Locale.java 15 Mar 2004 22:04:59 -0000      1.17
+++ Locale.java 2 Jul 2004 19:32:35 -0000
@@ -186,7 +186,7 @@
    *
    * @serial should be -1 in serial streams
    */
-  private int hashcode;
+  private transient int hashcode;
 
   /**
    * The default locale. Except for during bootstrapping, this should never be
@@ -709,10 +709,8 @@
    *
    * @return the hashcode
    */
-  public synchronized int hashCode()
+  public int hashCode()
   {
-    // This method is synchronized because writeObject() might reset
-    // the hashcode.
     return hashcode;
   }
 
@@ -731,10 +729,6 @@
       return false;
     Locale l = (Locale) obj;
 
-    // ??? We might also want to add:
-    //        hashCode() == l.hashCode()
-    // But this is a synchronized method.  Is the overhead worth it?
-    // Measure this to make a decision.
     return (language == l.language
             && country == l.country
             && variant == l.variant);
@@ -745,17 +739,19 @@
    *
    * @param output the stream to write to
    * @throws IOException if the write fails
-   * @serialData the hashcode should always be written as -1, and recomputed
-   *      when reading it back
+   * @serialData The first three fields are Strings representing language,
+   *             country, and variant. The fourth field is a placeholder for 
+   *             the cached hashcode, but this is always written as -1, and 
+   *             recomputed when reading it back.
    */
-  private synchronized void writeObject(ObjectOutputStream output)
+  private void writeObject(ObjectOutputStream s)
     throws IOException
   {
-    // Synchronized so that hashCode() doesn't get wrong value.
-    int tmpHashcode = hashcode;
-    hashcode = -1;
-    output.defaultWriteObject();
-    hashcode = tmpHashcode;
+    s.writeObject(language);
+    s.writeObject(country);
+    s.writeObject(variant);
+    // Hashcode field is always written as -1.
+    s.writeInt(-1);
   }
 
   /**
@@ -766,10 +762,13 @@
    * @throws ClassNotFoundException if reading fails
    * @serialData the hashCode is always invalid and must be recomputed
    */
-  private void readObject(ObjectInputStream input)
+  private void readObject(ObjectInputStream s)
     throws IOException, ClassNotFoundException
   {
-    input.defaultReadObject();
+    language = (String) s.readObject();
+    country = (String) s.readObject();
+    variant = (String) s.readObject();    
+    // Recompute hashcode.
     hashcode = language.hashCode() ^ country.hashCode() ^ variant.hashCode();
   }
 } // class Locale

reply via email to

[Prev in Thread] Current Thread [Next in Thread]