Class RandomNameGenerator

  • All Implemented Interfaces:
    java.io.Serializable

    @GwtIncompatible("java.util.Collections.shuffle, com.google.common.hash.Hasher, com.google.common.hash.Hashing")
    public final class RandomNameGenerator
    extends java.lang.Object
    A class for generating unique, randomized JavaScript variable/property names.

    Unlike NameGenerator, names do not follow a predictable sequence such as a, b, ... z, aa, ab, ..., az, ba, ... but instead they are random, based on an external random seed. We do partially compromise for efficiency in that

    • Generated names will have the same length as they would with NameGenerator
    • We don't use a completely different alphabet for each name prefix, but instead choose among a few with a predictable formula.

    More precisely:

    • We compute a random shuffle of the alphabet for "first characters", and a small number of random shuffles of the alphabet for "non-first characters". Then we do a typical number-to-text conversion of a name's "index", where the alphabet for digits is not just 0 to 9. The least significant digit comes first.
    • We represent each digit using an appropriate alphabet. If it's not the first character of the name (i.e. not the least significant one, or there is a constant prefix) then we have several appropriate alphabets to choose from; we choose one based a hash of the previous digits of this name.

    This class is not thread safe.

    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      RandomNameGenerator​(java.util.Random random)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      com.google.javascript.jscomp.NameGenerator clone​(java.util.Set<java.lang.String> reservedNames, java.lang.String prefix, char[] reservedCharacters)  
      java.lang.String generateNextName()
      Generates the next short name.
      void reset​(java.util.Set<java.lang.String> reservedNames, java.lang.String prefix, char[] reservedCharacters)  
      void reset​(java.util.Set<java.lang.String> reservedNames, java.lang.String prefix, char[] reservedFirstCharacters, char[] reservedNonFirstCharacters)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RandomNameGenerator

        public RandomNameGenerator​(java.util.Random random)
    • Method Detail

      • reset

        public void reset​(java.util.Set<java.lang.String> reservedNames,
                          java.lang.String prefix,
                          @Nullable
                          char[] reservedCharacters)
      • reset

        public void reset​(java.util.Set<java.lang.String> reservedNames,
                          java.lang.String prefix,
                          @Nullable
                          char[] reservedFirstCharacters,
                          @Nullable
                          char[] reservedNonFirstCharacters)
      • clone

        public com.google.javascript.jscomp.NameGenerator clone​(java.util.Set<java.lang.String> reservedNames,
                                                                java.lang.String prefix,
                                                                @Nullable
                                                                char[] reservedCharacters)
      • generateNextName

        public java.lang.String generateNextName()
        Generates the next short name.

        This generates names of increasing length. To minimize output size, therefore, it's good to call it for the most used symbols first.