Class Intern

java.lang.Object
org.plumelib.util.Intern

public final class Intern extends Object
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 Classes
    Modifier and Type
    Class
    Description
    private static final class 
    Hasher object which hashes and compares double[] objects according to their contents.
    private static final class 
    Hasher object which hashes and compares Doubles.
    private static final class 
    Hasher object which hashes and compares int[] objects according to their contents.
    private static final class 
    Hasher object which hashes and compares Integers.
    private static final class 
    Hasher object which hashes and compares long[] objects according to their contents.
    private static final class 
    Hasher object which hashes and compares Longs.
    private static final class 
    Hasher object which hashes and compares Object[] objects according to their contents.
    private static final class 
    Hasher object which hashes and compares String[] objects according to their contents.
    private static final class 
    Intern.Subsequence<T extends @Interned Object>
    A subsequence view on a sequence.
    private static final class 
    Intern.SubsequenceHasher<T extends @Interned Object>
    Hasher object which hashes and compares sequences according to their contents.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static boolean
    Whether assertions are enabled.
    private static final double
    Another multiplicative constant for use in hashing function.
    private static final int
    Multiplicative 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 Double
    The 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 Double
    The 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

    Constructors
    Modifier
    Constructor
    Description
    private
    This class is a collection of methods; it does not represent anything.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) static Iterator<double @Interned []>
    Returns all the interned double arrays.
    (package private) static Iterator<@Interned Double>
    Returns all the interned doubles.
    (package private) static Iterator<int @Interned []>
    Returns all the interned int arrays.
    (package private) static Iterator<@Interned Integer>
    Returns all the interned integers.
    static double
    intern(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 long
    intern(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(@PolyNull @Interned @PolyValue Object[] a)
    Intern (canonicalize) an Object[].
    static @PolyNull @Interned String @Interned @PolyValue @SameLen("#1") []
    intern(@PolyNull @Interned String @PolyValue [] a)
    Intern (canonicalize) a String[].
    static @Interned @PolyNull @PolyValue @SameLen("#1") String
    intern(@PolyNull @PolyValue String a)
    Interns a String.
    static @Interned @PolyNull Object
    intern(@PolyNull Object a)
    Convenience method to intern an Object when we don't know its run-time type.
    static @Interned Double
    Intern (canonicalize) a Double.
    static @Interned Integer
    Intern (canonicalize) an Integer.
    static @Interned Long
    Intern (canonicalize) a Long.
    static @Interned Double
    internedDouble(double d)
    Returns an interned Double with value i.
    static @Interned Double
    Returns an interned Double with value parsed from the string.
    static @Interned Integer
    Returns an interned Integer with value i.
    static @Interned Integer
    Returns an interned Integer with value parsed from the string.
    static @Interned Long
    internedLong(long i)
    Returns an interned Long with value i.
    static @Interned Long
    Returns 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 boolean
    isInterned(@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.
    (package private) static Iterator<@Interned Long>
    Returns all the interned longs.
    (package private) static int
    Returns the number of interned double arrays.
    (package private) static int
    Returns the number of interned doubles.
    (package private) static int
    Returns the number of interned int arrays.
    (package private) static int
    Returns the number of interned integers.
    (package private) static int
    Returns the number of interned long arrays.
    (package private) static int
    Returns the number of interned longs.
    (package private) static int
    Returns the number of interned object arrays.
    (package private) static int
    Returns the number of interned string arrays.
    (package private) static Iterator<@Nullable @Interned Object @Interned []>
    Returns all the interned object arrays.
    (package private) static Iterator<@Nullable @Interned String @Interned []>
    Returns all the interned string arrays.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • assertsEnabled

      private static boolean assertsEnabled
      Whether assertions are enabled.
    • FACTOR

      private static final int FACTOR
      Multiplicative constant for use in hashing function.
      See Also:
    • DOUBLE_FACTOR

      private static final double DOUBLE_FACTOR
      Another multiplicative constant for use in hashing function.
      See Also:
    • internedIntegers

      private static org.plumelib.util.WeakHasherMap<@Interned Integer,WeakReference<@Interned Integer>> internedIntegers
      All the interned Integers.
    • internedLongs

      private static org.plumelib.util.WeakHasherMap<@Interned Long,WeakReference<@Interned Long>> internedLongs
      All the interned Longs.
    • internedIntArrays

      private static org.plumelib.util.WeakHasherMap<int @Interned [],WeakReference<int @Interned []>> internedIntArrays
      All the interned Int arrays.
    • internedLongArrays

      private static org.plumelib.util.WeakHasherMap<long @Interned [],WeakReference<long @Interned []>> internedLongArrays
      All the interned Long arrays.
    • internedDoubles

      private static org.plumelib.util.WeakHasherMap<@Interned Double,WeakReference<@Interned Double>> internedDoubles
      All the interned Doubles.
    • internedDoubleNaN

      private static @Interned Double internedDoubleNaN
      The interned NaN.
    • internedDoubleZero

      private static @Interned Double internedDoubleZero
      The interned Double zero.
    • internedDoubleArrays

      private static org.plumelib.util.WeakHasherMap<double @Interned [],WeakReference<double @Interned []>> internedDoubleArrays
      All the interned Double arrays.
    • internedStringArrays

      private static org.plumelib.util.WeakHasherMap<@Nullable @Interned String @Interned [],WeakReference<@Nullable @Interned String @Interned []>> internedStringArrays
      All the interned String arrays.
    • internedObjectArrays

      private static org.plumelib.util.WeakHasherMap<@Nullable @Interned Object @Interned [],WeakReference<@Nullable @Interned Object @Interned []>> internedObjectArrays
      All the interned Object arrays.
    • internedIntSubsequence

      private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<int @Interned []>,WeakReference<int @Interned []>> internedIntSubsequence
      All the interned Int subsequences.
    • internedLongSubsequence

      private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<long @Interned []>,WeakReference<long @Interned []>> internedLongSubsequence
      All the interned Long subsequences.
    • internedDoubleSubsequence

      private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<double @Interned []>,WeakReference<double @Interned []>> internedDoubleSubsequence
      All the interned Double subsequences.
    • internedObjectSubsequence

      private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<@Nullable @Interned Object @Interned []>,WeakReference<@Nullable @Interned Object @Interned []>> internedObjectSubsequence
      All the interned Object subsequences.
    • internedStringSubsequence

      private static org.plumelib.util.WeakHasherMap<Intern.Subsequence<@Nullable @Interned String @Interned []>,WeakReference<@Nullable @Interned String @Interned []>> internedStringSubsequence
      All the interned String subsequences.
  • Constructor Details

    • Intern

      private Intern()
      This class is a collection of methods; it does not represent anything.
  • Method Details

    • internStrings

      public static @Interned String @PolyValue @SameLen("#1") [] internStrings(String @PolyValue [] a)
      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

      @Pure public static boolean isInterned(@Nullable Object value)
      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

      static Iterator<@Interned Integer> integers()
      Returns all the interned integers. For testing only.
      Returns:
      all the interned integers
    • longs

      static Iterator<@Interned Long> longs()
      Returns all the interned longs. For testing only.
      Returns:
      all the interned longs
    • intArrays

      static Iterator<int @Interned []> intArrays()
      Returns all the interned int arrays. For testing only.
      Returns:
      all the interned int arrays
    • longArrays

      static Iterator<long @Interned []> longArrays()
      Returns all the interned long arrays. For testing only.
      Returns:
      all the interned long arrays
    • doubles

      static Iterator<@Interned Double> doubles()
      Returns all the interned doubles. For testing only.
      Returns:
      all the interned doubles
    • doubleArrays

      static Iterator<double @Interned []> doubleArrays()
      Returns all the interned double arrays. For testing only.
      Returns:
      all the interned double arrays
    • stringArrays

      static Iterator<@Nullable @Interned String @Interned []> stringArrays()
      Returns all the interned string arrays. For testing only.
      Returns:
      all the interned string arrays
    • objectArrays

      static Iterator<@Nullable @Interned Object @Interned []> 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 handles null.
      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

      @Pure public static @Interned Integer intern(Integer a)
      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

      public static @Interned Integer internedInteger(int i)
      Returns an interned Integer with value i.
      Parameters:
      i - the value to intern
      Returns:
      an interned Integer with value i
    • internedInteger

      public static @Interned Integer internedInteger(String s)
      Returns an interned Integer with value parsed from the string.
      Parameters:
      s - the string to parse
      Returns:
      an interned Integer parsed from s
    • intern

      @Pure public static @Interned Long intern(Long a)
      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

      public static @Interned Long internedLong(long i)
      Returns an interned Long with value i.
      Parameters:
      i - the value to intern
      Returns:
      an interned Integer with value i
    • internedLong

      public static @Interned Long internedLong(String s)
      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

      @Pure public static @Interned Double intern(Double a)
      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

      public static @Interned Double internedDouble(double d)
      Returns an interned Double with value i.
      Parameters:
      d - the value to intern
      Returns:
      an interned Double with value d
    • internedDouble

      public static @Interned Double internedDouble(String s)
      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

      @Pure public static @Interned @PolyNull Object intern(@PolyNull Object a)
      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 interned
      start - the index of the start of the subsequence to compute and intern
      end - 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 interned
      start - the index of the start of the subsequence to compute and intern
      end - 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 interned
      start - the index of the start of the subsequence to compute and intern
      end - 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 interned
      start - the index of the start of the subsequence to compute and intern
      end - 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 interned
      start - the index of the start of the subsequence to compute and intern
      end - 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: