@ SuppressWarnings ( "unchecked" ) // If a.equals(b) then a and b have the same type. public synchronized < T > T intern ( T value ) { Preconditions . checkNotNull ( value ) ; // Use a pseudo-random number generator to mix up the high and low bits of the hash code. Random generator = new java . util . Random ( value . hashCode ( ) ) ; int tries = 0 ; while ( true ) { int index = generator . nextInt ( table . length ) ; Object candidate = table [ index ] ; if ( candidate == null ) { // Found a good place to hash it. count ++ ; collisions += tries ; table [ index ] = value ; rehashIfNeeded ( ) ; return value ; } if ( candidate . equals ( value ) ) { Preconditions . checkArgument ( value . getClass ( ) == candidate . getClass ( ) , "Interned objects are equals() but different classes: %s and %s" , value , candidate ) ; return ( T ) candidate ; } tries ++ ; } }
Returns either the input or an instance that equals it that was previously passed to this method .