Package org.plumelib.util
Class Intern
java.lang.Object
org.plumelib.util.Intern
Utilities for interning objects. Interning is also known as canonicalization or hash-consing: it
returns a single representative object that
Object.equals(java.lang.Object) the object, and the client
discards the argument and uses the result instead. Since only one object exists for every set of
equal objects, space usage is reduced. Time may also be reduced, since it is possible to use
== instead of .equals() for comparisons.
Java builds in interning for Strings, but not for other objects. The methods in this class extend interning to all Java objects.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classHasher object which hashes and compares double[] objects according to their contents.private static final classHasher object which hashes and compares Doubles.private static final classHasher object which hashes and compares int[] objects according to their contents.private static final classHasher object which hashes and compares Integers.private static final classHasher object which hashes and compares long[] objects according to their contents.private static final classHasher object which hashes and compares Longs.private static final classHasher object which hashes and compares Object[] objects according to their contents.private static final classHasher object which hashes and compares String[] objects according to their contents.private static final classIntern.Subsequence<T extends @Interned Object>A subsequence view on a sequence.private static final classIntern.SubsequenceHasher<T extends @Interned Object>Hasher object which hashes and compares sequences according to their contents. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static booleanWhether assertions are enabled.private static final doubleAnother multiplicative constant for use in hashing function.private static final intMultiplicative constant for use in hashing function.private static org.plumelib.util.WeakHasherMap<double @Interned [],WeakReference<double @Interned []>> All the interned Double arrays.private static @Interned DoubleThe interned NaN.private static org.plumelib.util.WeakHasherMap<@Interned Double,WeakReference<@Interned Double>> All the interned Doubles.private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<double @Interned []>,WeakReference<double @Interned []>> All the interned Double subsequences.private static @Interned DoubleThe interned Double zero.private static org.plumelib.util.WeakHasherMap<int @Interned [],WeakReference<int @Interned []>> All the interned Int arrays.private static org.plumelib.util.WeakHasherMap<@Interned Integer,WeakReference<@Interned Integer>> All the interned Integers.private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<int @Interned []>,WeakReference<int @Interned []>> All the interned Int subsequences.private static org.plumelib.util.WeakHasherMap<long @Interned [],WeakReference<long @Interned []>> All the interned Long arrays.private static org.plumelib.util.WeakHasherMap<@Interned Long,WeakReference<@Interned Long>> All the interned Longs.private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<long @Interned []>,WeakReference<long @Interned []>> All the interned Long subsequences.private static org.plumelib.util.WeakHasherMap<@Nullable @Interned Object @Interned [],WeakReference<@Nullable @Interned Object @Interned []>> All the interned Object arrays.private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<@Nullable @Interned Object @Interned []>,WeakReference<@Nullable @Interned Object @Interned []>> All the interned Object subsequences.private static org.plumelib.util.WeakHasherMap<@Nullable @Interned String @Interned [],WeakReference<@Nullable @Interned String @Interned []>> All the interned String arrays.private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<@Nullable @Interned String @Interned []>,WeakReference<@Nullable @Interned String @Interned []>> All the interned String subsequences. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateIntern()This class is a collection of methods; it does not represent anything. -
Method Summary
Modifier and TypeMethodDescription(package private) static Iterator<double @Interned []>Returns all the interned double arrays.doubles()Returns all the interned doubles.(package private) static Iterator<int @Interned []>Returns all the interned int arrays.integers()Returns all the interned integers.static doubleintern(double d) Interns a double A no-op.static double @Interned @PolyValue @SameLen("#1") []intern(double @PolyValue [] a) Intern (canonicalize) a double[].static int @Interned @PolyValue @SameLen("#1") []intern(int @PolyValue [] a) Intern (canonicalize) an int[].static longintern(long l) Interns a long.static long @Interned @PolyValue @SameLen("#1") []intern(long @PolyValue [] a) Intern (canonicalize) a long[].static @PolyNull @Interned Object @Interned @PolyValue @SameLen("#1") []Intern (canonicalize) an Object[].static @PolyNull @Interned String @Interned @PolyValue @SameLen("#1") []Intern (canonicalize) a String[].static @Interned @PolyNull @PolyValue @SameLen("#1") StringInterns a String.static @Interned @PolyNull ObjectConvenience method to intern an Object when we don't know its run-time type.static @Interned DoubleIntern (canonicalize) a Double.static @Interned IntegerIntern (canonicalize) an Integer.static @Interned LongIntern (canonicalize) a Long.static @Interned DoubleinternedDouble(double d) Returns an interned Double with value i.static @Interned DoubleReturns an interned Double with value parsed from the string.static @Interned IntegerinternedInteger(int i) Returns an interned Integer with value i.static @Interned IntegerReturns an interned Integer with value parsed from the string.static @Interned LonginternedLong(long i) Returns an interned Long with value i.static @Interned LongReturns an interned Long with value parsed from the string.static @Interned String @PolyValue @SameLen("#1") []internStrings(String @PolyValue [] a) Replace each element of the array by its interned version.static double @Interned []internSubsequence(double @Interned [] seq, @org.checkerframework.checker.index.qual.IndexFor({"#1"}),@org.checkerframework.checker.index.qual.LessThan({"#3"}) int start, @org.checkerframework.checker.index.qual.NonNegative,@org.checkerframework.checker.index.qual.LTLengthOf(value={"#1"}, offset={"#2 - 1"}) int end) Returns a subsequence of seq from start to end that is interned.static int @Interned []internSubsequence(int @Interned [] seq, @org.checkerframework.checker.index.qual.IndexFor({"#1"}),@org.checkerframework.checker.index.qual.LessThan({"#3"}) int start, @org.checkerframework.checker.index.qual.NonNegative,@org.checkerframework.checker.index.qual.LTLengthOf(value={"#1"}, offset={"#2 - 1"}) int end) Returns an interned subsequence of seq from start (inclusive) to end (exclusive).static long @Interned []internSubsequence(long @Interned [] seq, @org.checkerframework.checker.index.qual.IndexFor({"#1"}),@org.checkerframework.checker.index.qual.LessThan({"#3"}) int start, @org.checkerframework.checker.index.qual.NonNegative,@org.checkerframework.checker.index.qual.LTLengthOf(value={"#1"}, offset={"#2 - 1"}) int end) Returns a subsequence of seq from start to end that is interned.static @PolyNull @Interned Object @Interned []internSubsequence(@PolyNull @Interned Object @Interned [] seq, @org.checkerframework.checker.index.qual.IndexFor({"#1"}),@org.checkerframework.checker.index.qual.LessThan({"#3"}) int start, @org.checkerframework.checker.index.qual.NonNegative,@org.checkerframework.checker.index.qual.LTLengthOf(value={"#1"}, offset={"#2 - 1"}) int end) Returns a subsequence of seq from start to end that is interned.static @PolyNull @Interned String @Interned []internSubsequence(@PolyNull @Interned String @Interned [] seq, @org.checkerframework.checker.index.qual.IndexFor({"#1"}),@org.checkerframework.checker.index.qual.LessThan({"#3"}) int start, @org.checkerframework.checker.index.qual.NonNegative,@org.checkerframework.checker.index.qual.LTLengthOf(value={"#1"}, offset={"#2 - 1"}) int end) Returns a subsequence of seq from start to end that is interned.static booleanisInterned(@Nullable Object value) Returns true if the argument is interned (is canonical among all objects equal to itself).(package private) static Iterator<long @Interned []>Returns all the interned long arrays.longs()Returns all the interned longs.(package private) static intReturns the number of interned double arrays.(package private) static intReturns the number of interned doubles.(package private) static intReturns the number of interned int arrays.(package private) static intReturns the number of interned integers.(package private) static intReturns the number of interned long arrays.(package private) static intnumLongs()Returns the number of interned longs.(package private) static intReturns the number of interned object arrays.(package private) static intReturns the number of interned string arrays.Returns all the interned object arrays.Returns all the interned string arrays.
-
Field Details
-
assertsEnabled
private static boolean assertsEnabledWhether assertions are enabled. -
FACTOR
private static final int FACTORMultiplicative constant for use in hashing function.- See Also:
-
DOUBLE_FACTOR
private static final double DOUBLE_FACTORAnother multiplicative constant for use in hashing function.- See Also:
-
internedIntegers
private static org.plumelib.util.WeakHasherMap<@Interned Integer,WeakReference<@Interned Integer>> internedIntegersAll the interned Integers. -
internedLongs
private static org.plumelib.util.WeakHasherMap<@Interned Long,WeakReference<@Interned Long>> internedLongsAll the interned Longs. -
internedIntArrays
private static org.plumelib.util.WeakHasherMap<int @Interned [],WeakReference<int @Interned []>> internedIntArraysAll the interned Int arrays. -
internedLongArrays
private static org.plumelib.util.WeakHasherMap<long @Interned [],WeakReference<long @Interned []>> internedLongArraysAll the interned Long arrays. -
internedDoubles
private static org.plumelib.util.WeakHasherMap<@Interned Double,WeakReference<@Interned Double>> internedDoublesAll the interned Doubles. -
internedDoubleNaN
The interned NaN. -
internedDoubleZero
The interned Double zero. -
internedDoubleArrays
private static org.plumelib.util.WeakHasherMap<double @Interned [],WeakReference<double @Interned []>> internedDoubleArraysAll the interned Double arrays. -
internedStringArrays
private static org.plumelib.util.WeakHasherMap<@Nullable @Interned String @Interned [],WeakReference<@Nullable @Interned String @Interned []>> internedStringArraysAll the interned String arrays. -
internedObjectArrays
private static org.plumelib.util.WeakHasherMap<@Nullable @Interned Object @Interned [],WeakReference<@Nullable @Interned Object @Interned []>> internedObjectArraysAll the interned Object arrays. -
internedIntSubsequence
private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<int @Interned []>,WeakReference<int @Interned []>> internedIntSubsequenceAll the interned Int subsequences. -
internedLongSubsequence
private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<long @Interned []>,WeakReference<long @Interned []>> internedLongSubsequenceAll the interned Long subsequences. -
internedDoubleSubsequence
private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<double @Interned []>,WeakReference<double @Interned []>> internedDoubleSubsequenceAll the interned Double subsequences. -
internedObjectSubsequence
private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<@Nullable @Interned Object @Interned []>,WeakReference<@Nullable @Interned Object @Interned []>> internedObjectSubsequenceAll the interned Object subsequences. -
internedStringSubsequence
private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<@Nullable @Interned String @Interned []>,WeakReference<@Nullable @Interned String @Interned []>> internedStringSubsequenceAll the interned String subsequences.
-
-
Constructor Details
-
Intern
private Intern()This class is a collection of methods; it does not represent anything.
-
-
Method Details
-
internStrings
Replace each element of the array by its interned version. Side-effects the array, but also returns it.- Parameters:
a- the array whose elements to intern in place- Returns:
- an interned version of a
- See Also:
-
isInterned
Returns true if the argument is interned (is canonical among all objects equal to itself).- Parameters:
value- the value to test for interning- Returns:
- true iff value is interned
-
numIntegers
static int numIntegers()Returns the number of interned integers. For testing only.- Returns:
- the number of interned integers
-
numLongs
static int numLongs()Returns the number of interned longs. For testing only.- Returns:
- the number of interned longs
-
numIntArrays
static int numIntArrays()Returns the number of interned int arrays. For testing only.- Returns:
- the number of interned int arrays
-
numLongArrays
static int numLongArrays()Returns the number of interned long arrays. For testing only.- Returns:
- the number of interned long arrays
-
numDoubles
static int numDoubles()Returns the number of interned doubles. For testing only.- Returns:
- the number of interned doubles
-
numDoubleArrays
static int numDoubleArrays()Returns the number of interned double arrays. For testing only.- Returns:
- the number of interned double arrays
-
numStringArrays
static int numStringArrays()Returns the number of interned string arrays. For testing only.- Returns:
- the number of interned string arrays
-
numObjectArrays
static int numObjectArrays()Returns the number of interned object arrays. For testing only.- Returns:
- the number of interned object arrays
-
integers
Returns all the interned integers. For testing only.- Returns:
- all the interned integers
-
longs
Returns all the interned longs. For testing only.- Returns:
- all the interned longs
-
intArrays
Returns all the interned int arrays. For testing only.- Returns:
- all the interned int arrays
-
longArrays
Returns all the interned long arrays. For testing only.- Returns:
- all the interned long arrays
-
doubles
Returns all the interned doubles. For testing only.- Returns:
- all the interned doubles
-
doubleArrays
Returns all the interned double arrays. For testing only.- Returns:
- all the interned double arrays
-
stringArrays
Returns all the interned string arrays. For testing only.- Returns:
- all the interned string arrays
-
objectArrays
Returns all the interned object arrays. For testing only.- Returns:
- all the interned object arrays
-
intern
@Pure public static @Interned @PolyNull @PolyValue @SameLen("#1") String intern(@PolyNull @PolyValue String a) Interns a String. Delegates to the builtin String.intern() method, but handlesnull.- Parameters:
a- the string to intern; may be null- Returns:
- an interned version of the argument, or null if the argument was null
-
intern
@Pure public static long intern(long l) Interns a long. A no-op. Provided for completeness.- Parameters:
l- the long to intern- Returns:
- an interned version of the argument
-
intern
@Pure public static double intern(double d) Interns a double A no-op. Provided for completeness.- Parameters:
d- the double to intern- Returns:
- an interned version of the argument
-
intern
Intern (canonicalize) an Integer. Return a canonical representation for the Integer.- Parameters:
a- an Integer to canonicalize- Returns:
- a canonical representation for the Integer
-
internedInteger
Returns an interned Integer with value i.- Parameters:
i- the value to intern- Returns:
- an interned Integer with value i
-
internedInteger
Returns an interned Integer with value parsed from the string.- Parameters:
s- the string to parse- Returns:
- an interned Integer parsed from s
-
intern
Intern (canonicalize) a Long. Return a canonical representation for the Long.- Parameters:
a- the value to intern- Returns:
- a canonical representation for the Long
-
internedLong
Returns an interned Long with value i.- Parameters:
i- the value to intern- Returns:
- an interned Integer with value i
-
internedLong
Returns an interned Long with value parsed from the string.- Parameters:
s- the string to parse- Returns:
- an interned Long parsed from s
-
intern
@Pure public static int @Interned @PolyValue @SameLen("#1") [] intern(int @PolyValue [] a) Intern (canonicalize) an int[]. Return a canonical representation for the int[] array. Arrays are compared according to their elements.- Parameters:
a- the array to canonicalize- Returns:
- a canonical representation for the int[] array
-
intern
@Pure public static long @Interned @PolyValue @SameLen("#1") [] intern(long @PolyValue [] a) Intern (canonicalize) a long[]. Return a canonical representation for the long[] array. Arrays are compared according to their elements.- Parameters:
a- the array to canonicalize- Returns:
- a canonical representation for the long[] array
-
intern
Intern (canonicalize) a Double. Return a canonical representation for the Double.- Parameters:
a- the Double to canonicalize- Returns:
- a canonical representation for the Double
-
internedDouble
Returns an interned Double with value i.- Parameters:
d- the value to intern- Returns:
- an interned Double with value d
-
internedDouble
Returns an interned Double with value parsed from the string.- Parameters:
s- the string to parse- Returns:
- an interned Double parsed from s
-
intern
@Pure public static double @Interned @PolyValue @SameLen("#1") [] intern(double @PolyValue [] a) Intern (canonicalize) a double[]. Return a canonical representation for the double[] array. Arrays are compared according to their elements.- Parameters:
a- the array to canonicalize- Returns:
- a canonical representation for the double[] array
-
intern
@Pure public static @PolyNull @Interned String @Interned @PolyValue @SameLen("#1") [] intern(@PolyNull @Interned String @PolyValue [] a) Intern (canonicalize) a String[]. Return a canonical representation for the String[] array. Arrays are compared according to their elements' equals() methods.- Parameters:
a- the array to canonicalize. Its elements should already be interned.- Returns:
- a canonical representation for the String[] array
-
intern
@Pure public static @PolyNull @Interned Object @Interned @PolyValue @SameLen("#1") [] intern(@PolyNull @Interned @PolyValue Object[] a) Intern (canonicalize) an Object[]. Return a canonical representation for the Object[] array. Arrays are compared according to their elements. The elements should themselves already be interned; they are compared using their equals() methods.- Parameters:
a- the array to canonicalize- Returns:
- a canonical representation for the Object[] array
-
intern
Convenience method to intern an Object when we don't know its run-time type. Its run-time type must be one of the types for which we have an intern() method, else an exception is thrown. If the argument is an array, its elements should themselves be interned.- Parameters:
a- an Object to canonicalize- Returns:
- a canonical version of a
-
internSubsequence
public static int @Interned [] internSubsequence(int @Interned [] seq, @org.checkerframework.checker.index.qual.IndexFor({"#1"}),@org.checkerframework.checker.index.qual.LessThan({"#3"}) int start, @org.checkerframework.checker.index.qual.NonNegative,@org.checkerframework.checker.index.qual.LTLengthOf(value={"#1"}, offset={"#2 - 1"}) int end) Returns an interned subsequence of seq from start (inclusive) to end (exclusive). The argument seq should already be interned.The result is the same as computing the subsequence and then interning it, but this method is more efficient: if the subsequence is already interned, it avoids computing the subsequence.
For example, since derived variables in Daikon compute the subsequence many times, this shortcut saves quite a bit of computation. It saves even more when there may be many derived variables that are non-canonical, since they are guaranteed to be ==.
- Parameters:
seq- the interned sequence whose subsequence should be computed and internedstart- the index of the start of the subsequence to compute and internend- the index of the end of the subsequence to compute and intern- Returns:
- a subsequence of seq from start to end that is interned
-
internSubsequence
@Pure public static long @Interned [] internSubsequence(long @Interned [] seq, @org.checkerframework.checker.index.qual.IndexFor({"#1"}),@org.checkerframework.checker.index.qual.LessThan({"#3"}) int start, @org.checkerframework.checker.index.qual.NonNegative,@org.checkerframework.checker.index.qual.LTLengthOf(value={"#1"}, offset={"#2 - 1"}) int end) Returns a subsequence of seq from start to end that is interned.- Parameters:
seq- the interned sequence whose subsequence should be computed and internedstart- the index of the start of the subsequence to compute and internend- the index of the end of the subsequence to compute and intern- Returns:
- a subsequence of seq from start to end that is interned
- See Also:
-
internSubsequence
@Pure public static double @Interned [] internSubsequence(double @Interned [] seq, @org.checkerframework.checker.index.qual.IndexFor({"#1"}),@org.checkerframework.checker.index.qual.LessThan({"#3"}) int start, @org.checkerframework.checker.index.qual.NonNegative,@org.checkerframework.checker.index.qual.LTLengthOf(value={"#1"}, offset={"#2 - 1"}) int end) Returns a subsequence of seq from start to end that is interned.- Parameters:
seq- the interned sequence whose subsequence should be computed and internedstart- the index of the start of the subsequence to compute and internend- the index of the end of the subsequence to compute and intern- Returns:
- a subsequence of seq from start to end that is interned
- See Also:
-
internSubsequence
@Pure public static @PolyNull @Interned Object @Interned [] internSubsequence(@PolyNull @Interned Object @Interned [] seq, @org.checkerframework.checker.index.qual.IndexFor({"#1"}),@org.checkerframework.checker.index.qual.LessThan({"#3"}) int start, @org.checkerframework.checker.index.qual.NonNegative,@org.checkerframework.checker.index.qual.LTLengthOf(value={"#1"}, offset={"#2 - 1"}) int end) Returns a subsequence of seq from start to end that is interned.- Parameters:
seq- the interned sequence whose subsequence should be computed and internedstart- the index of the start of the subsequence to compute and internend- the index of the end of the subsequence to compute and intern- Returns:
- a subsequence of seq from start to end that is interned
- See Also:
-
internSubsequence
@Pure public static @PolyNull @Interned String @Interned [] internSubsequence(@PolyNull @Interned String @Interned [] seq, @org.checkerframework.checker.index.qual.IndexFor({"#1"}),@org.checkerframework.checker.index.qual.LessThan({"#3"}) int start, @org.checkerframework.checker.index.qual.NonNegative,@org.checkerframework.checker.index.qual.LTLengthOf(value={"#1"}, offset={"#2 - 1"}) int end) Returns a subsequence of seq from start to end that is interned.- Parameters:
seq- the interned sequence whose subsequence should be computed and internedstart- the index of the start of the subsequence to compute and internend- the index of the end of the subsequence to compute and intern- Returns:
- a subsequence of seq from start to end that is interned
- See Also:
-