jdk/src/share/classes/java/lang/String.java
changeset 12859 c44b88bb9b5e
parent 12858 97e3f3f77254
child 13156 e88d9099b6f0
equal deleted inserted replaced
12858:97e3f3f77254 12859:c44b88bb9b5e
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package java.lang;
    26 package java.lang;
    27 
    27 
    28 import java.io.ObjectStreamClass;
       
    29 import java.io.ObjectStreamField;
    28 import java.io.ObjectStreamField;
    30 import java.io.UnsupportedEncodingException;
    29 import java.io.UnsupportedEncodingException;
    31 import java.nio.charset.Charset;
    30 import java.nio.charset.Charset;
    32 import java.util.ArrayList;
    31 import java.util.ArrayList;
    33 import java.util.Arrays;
    32 import java.util.Arrays;
  3019      * @return  a string that has the same contents as this string, but is
  3018      * @return  a string that has the same contents as this string, but is
  3020      *          guaranteed to be from a pool of unique strings.
  3019      *          guaranteed to be from a pool of unique strings.
  3021      */
  3020      */
  3022     public native String intern();
  3021     public native String intern();
  3023 
  3022 
       
  3023     /**
       
  3024      * Seed value used for each alternative hash calculated.
       
  3025      */
       
  3026     private static final int HASHING_SEED;
       
  3027 
       
  3028     static {
       
  3029         long nanos = System.nanoTime();
       
  3030         long now = System.currentTimeMillis();
       
  3031         int SEED_MATERIAL[] = {
       
  3032                 System.identityHashCode(String.class),
       
  3033                 System.identityHashCode(System.class),
       
  3034                 (int) (nanos >>> 32),
       
  3035                 (int) nanos,
       
  3036                 (int) (now >>> 32),
       
  3037                 (int) now,
       
  3038                 (int) (System.nanoTime() >>> 2)
       
  3039         };
       
  3040 
       
  3041         // Use murmur3 to scramble the seeding material.
       
  3042         // Inline implementation to avoid loading classes
       
  3043         int h1 = 0;
       
  3044 
       
  3045         // body
       
  3046         for(int k1 : SEED_MATERIAL) {
       
  3047             k1 *= 0xcc9e2d51;
       
  3048             k1 = (k1 << 15) | (k1 >>> 17);
       
  3049             k1 *= 0x1b873593;
       
  3050 
       
  3051             h1 ^= k1;
       
  3052             h1 = (h1 << 13) | (h1 >>> 19);
       
  3053             h1 = h1 * 5 + 0xe6546b64;
       
  3054         }
       
  3055 
       
  3056         // tail (always empty, as body is always 32-bit chunks)
       
  3057 
       
  3058         // finalization
       
  3059 
       
  3060         h1 ^= SEED_MATERIAL.length * 4;
       
  3061 
       
  3062         // finalization mix force all bits of a hash block to avalanche
       
  3063         h1 ^= h1 >>> 16;
       
  3064         h1 *= 0x85ebca6b;
       
  3065         h1 ^= h1 >>> 13;
       
  3066         h1 *= 0xc2b2ae35;
       
  3067         h1 ^= h1 >>> 16;
       
  3068 
       
  3069         HASHING_SEED = h1;
       
  3070     }
       
  3071 
       
  3072     /**
       
  3073      * Cached value of the hashing algorithm result
       
  3074      */
       
  3075     private transient int hash32 = 0;
       
  3076 
       
  3077     /**
       
  3078     * Return a 32-bit hash code value for this object.
       
  3079     * <p>
       
  3080     * The general contract of {@code hash32} is:
       
  3081     * <ul>
       
  3082     * <li>Whenever it is invoked on the same object more than once during
       
  3083     *     an execution of a Java application, the {@code hash32} method
       
  3084     *     must consistently return the same integer, provided no information
       
  3085     *     used in {@code equals} comparisons on the object is modified.
       
  3086     *     This integer need not remain consistent from one execution of an
       
  3087     *     application to another execution of the same application.
       
  3088     * <li>If two objects are equal according to the {@code equals(Object)}
       
  3089     *     method, then calling the {@code hash32} method on each of
       
  3090     *     the two objects must produce the same integer result.
       
  3091     * <li>It is <em>not</em> required that if two objects are unequal
       
  3092     *     according to the {@link java.lang.Object#equals(java.lang.Object)}
       
  3093     *     method, then calling the {@code hash32} method on each of the
       
  3094     *     two objects must produce distinct integer results.  However, the
       
  3095     *     programmer should be aware that producing distinct integer results
       
  3096     *     for unequal objects may improve the performance of hash tables.
       
  3097     * </ul>
       
  3098     * <p/>
       
  3099      * The hash value will never be zero.
       
  3100     *
       
  3101     * @return  a hash code value for this object.
       
  3102     * @see     java.lang.Object#equals(java.lang.Object)
       
  3103     */
       
  3104     public int hash32() {
       
  3105         int h = hash32;
       
  3106         if (0 == h) {
       
  3107            // harmless data race on hash32 here.
       
  3108            h = sun.misc.Hashing.murmur3_32(HASHING_SEED, value, 0, value.length);
       
  3109 
       
  3110            // ensure result is not zero to avoid recalcing
       
  3111            h = (0 != h) ? h : 1;
       
  3112 
       
  3113            hash32 = h;
       
  3114         }
       
  3115 
       
  3116         return h;
       
  3117     }
       
  3118 
  3024 }
  3119 }