src/share/classes/java/lang/String.java

Print this page

        

@@ -23,11 +23,10 @@
  * questions.
  */
 
 package java.lang;
 
-import java.io.ObjectStreamClass;
 import java.io.ObjectStreamField;
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Arrays;

@@ -3072,6 +3071,43 @@
      * @return  a string that has the same contents as this string, but is
      *          guaranteed to be from a pool of unique strings.
      */
     public native String intern();
 
+    /**
+     * Seed value used for each alternative hash calculated.
+     */
+    private static final int HASHING_SEED;
+
+    static {
+        HASHING_SEED = System.identityHashCode(String.class)
+                ^ System.identityHashCode(System.class)
+                ^ System.identityHashCode(Thread.currentThread())
+                ^ (int) (System.currentTimeMillis() >>> 2) // resolution is poor
+                ^ (int) (System.nanoTime() >>> 5); // resolution is poor
+    }
+
+    /**
+     * Cached value of the alternative hashing algorithm result
+     */
+    private transient int hash32 = 0;
+
+    /**
+     * Calculates a 32-bit hash value for this string.
+     *
+     * @return a 32-bit hash value for this string.
+     */
+    int hash32() {
+        int h = hash32;
+        if (0 == h) {
+           // harmless data race on hash32 here.            
+           h = sun.misc.Hashing.murmur3_32(HASHING_SEED, value, offset, count);
+           
+           // ensure result is not zero to avoid recalcing
+           h = (0 != h) ? h : 1;
+           
+           hash32 = h;
+        }
+
+        return h;
+    }
 }