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;
@@ -106,12 +105,11 @@
* @see java.nio.charset.Charset
* @since JDK1.0
*/
public final class String
- implements java.io.Serializable, Comparable<String>, CharSequence
-{
+ implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** The offset is the first index of the storage that is used. */
private final int offset;
@@ -3075,6 +3073,54 @@
* @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 {
+ long nanos = System.nanoTime();
+ long now = System.currentTimeMillis();
+ int SEED_MATERIAL[] = {
+ System.identityHashCode(String.class),
+ System.identityHashCode(System.class),
+ System.identityHashCode(Thread.currentThread()),
+ (int) (nanos >>> 32),
+ (int) nanos,
+ (int) (now >>> 32),
+ (int) now,
+ (int) (System.nanoTime() >> 2)
+ };
+
+ HASHING_SEED = sun.misc.Hashing.murmur3_32(SEED_MATERIAL);
+ }
+
+ /**
+ * Cached value of the hashing algorithm result
+ */
+ private transient int hash32 = 0;
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p/>
+ * The hash value will never be zero.
+ */
+ public 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;
+ }
+
}