Class RandomNameGenerator

  • All Implemented Interfaces:
    NameGenerator, 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
    implements NameGenerator
    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
      NameGenerator clone​(java.util.Set<java.lang.String> reservedNames, java.lang.String prefix, char[] reservedCharacters)
      Returns a clone of this NameGenerator, reconfigured and reset.
      java.lang.String generateNextName()
      Generates the next short name.
      void reset​(java.util.Set<java.lang.String> reservedNames, java.lang.String prefix, char[] reservedCharacters)
      Reconfigures this NameGenerator, and resets it to the initial state.
      void reset​(java.util.Set<java.lang.String> reservedNames, java.lang.String prefix, char[] reservedFirstCharacters, char[] reservedNonFirstCharacters)
      Reconfigures this NameGenerator, and resets it to the initial state.
      • 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)
        Description copied from interface: NameGenerator
        Reconfigures this NameGenerator, and resets it to the initial state.
        Specified by:
        reset in interface NameGenerator
        Parameters:
        reservedNames - set of names that are reserved; generated names will not include these names. This set is referenced rather than copied, so changes to the set will be reflected in how names are generated.
        prefix - all generated names begin with this prefix.
        reservedCharacters - If specified these characters won't be used in generated names
      • reset

        public void reset​(java.util.Set<java.lang.String> reservedNames,
                          java.lang.String prefix,
                          @Nullable
                          char[] reservedFirstCharacters,
                          @Nullable
                          char[] reservedNonFirstCharacters)
        Description copied from interface: NameGenerator
        Reconfigures this NameGenerator, and resets it to the initial state.
        Specified by:
        reset in interface NameGenerator
        Parameters:
        reservedNames - set of names that are reserved; generated names will not include these names. This set is referenced rather than copied, so changes to the set will be reflected in how names are generated.
        prefix - all generated names begin with this prefix.
        reservedFirstCharacters - If specified these characters won't be used as the first character in generated names
        reservedNonFirstCharacters - If specified these characters won't be used for characters (after the first) in generated names
      • clone

        public NameGenerator clone​(java.util.Set<java.lang.String> reservedNames,
                                   java.lang.String prefix,
                                   @Nullable
                                   char[] reservedCharacters)
        Description copied from interface: NameGenerator
        Returns a clone of this NameGenerator, reconfigured and reset.
        Specified by:
        clone in interface NameGenerator
      • 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.

        Specified by:
        generateNextName in interface NameGenerator