Class NanoIdUtils


  • public final class NanoIdUtils
    extends java.lang.Object
    A class for generating unique String IDs. The implementations of the core logic in this class are based on NanoId, a JavaScript library by Andrey Sitnik released under the MIT license. (https://github.com/ai/nanoid)
    Author:
    David Klebanoff
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DEFAULT_ALPHABET
      The default alphabet used by this class.
      static java.security.SecureRandom DEFAULT_NUMBER_GENERATOR
      The default random number generator used by this class.
      static int DEFAULT_SIZE
      The default size used by this class.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.lang.String randomNanoId()
      Static factory to retrieve a url-friendly, pseudo randomly generated, NanoId String.
      static java.lang.String randomNanoId​(char[] alphabet, int size)
      Deprecated.
      Using char[] isn't recommended.
      static java.lang.String randomNanoId​(int size)
      Static factory to retrieve a url-friendly, pseudo randomly generated, NanoId String.
      static java.lang.String randomNanoId​(java.lang.String alphabet, int size)
      Static factory to retrieve a url-friendly, pseudo randomly generated, NanoId String.
      static java.lang.String randomNanoId​(java.util.Random random, char[] alphabet, int size)
      Deprecated.
      Using char[] isn't recommended.
      static java.lang.String randomNanoId​(java.util.Random random, java.lang.String alphabet, int size)
      Static factory to retrieve a NanoId String.
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_NUMBER_GENERATOR

        public static final java.security.SecureRandom DEFAULT_NUMBER_GENERATOR
        The default random number generator used by this class. Creates cryptographically strong NanoId Strings.
      • DEFAULT_ALPHABET

        public static final java.lang.String DEFAULT_ALPHABET
        The default alphabet used by this class. Creates url-friendly NanoId Strings using 64 unique symbols.
        See Also:
        Constant Field Values
      • DEFAULT_SIZE

        public static final int DEFAULT_SIZE
        The default size used by this class. Creates NanoId Strings with slightly more unique values than UUID v4.
        See Also:
        Constant Field Values
    • Method Detail

      • randomNanoId

        public static java.lang.String randomNanoId()
        Static factory to retrieve a url-friendly, pseudo randomly generated, NanoId String. The generated NanoId String will have 21 symbols. The NanoId String is generated using a cryptographically strong pseudo random number generator.
        Returns:
        A randomly generated NanoId String.
      • randomNanoId

        public static java.lang.String randomNanoId​(int size)
        Static factory to retrieve a url-friendly, pseudo randomly generated, NanoId String. The NanoId String is generated using a cryptographically strong pseudo random number generator.
        Parameters:
        size - The number of symbols in the NanoId String.
        Returns:
        A randomly generated NanoId String.
      • randomNanoId

        @Deprecated
        public static java.lang.String randomNanoId​(char[] alphabet,
                                                    int size)
        Deprecated.
        Using char[] isn't recommended. Use a String instead.
        Static factory to retrieve a url-friendly, pseudo randomly generated, NanoId String. The NanoId String is generated using a cryptographically strong pseudo random number generator.
        Parameters:
        alphabet - The symbols used in the NanoId String.
        size - The number of symbols in the NanoId String.
        Returns:
        A randomly generated NanoId String.
      • randomNanoId

        public static java.lang.String randomNanoId​(java.lang.String alphabet,
                                                    int size)
        Static factory to retrieve a url-friendly, pseudo randomly generated, NanoId String. The NanoId String is generated using a cryptographically strong pseudo random number generator.
        Parameters:
        alphabet - The symbols used in the NanoId String.
        size - The number of symbols in the NanoId String.
        Returns:
        A randomly generated NanoId String.
      • randomNanoId

        @Deprecated
        public static java.lang.String randomNanoId​(java.util.Random random,
                                                    char[] alphabet,
                                                    int size)
        Deprecated.
        Using char[] isn't recommended. Use a String instead.
        Static factory to retrieve a NanoId String. The string is generated using the given random number generator.
        Parameters:
        random - The random number generator.
        alphabet - The symbols used in the NanoId String.
        size - The number of symbols in the NanoId String.
        Returns:
        A randomly generated NanoId String.
      • randomNanoId

        public static java.lang.String randomNanoId​(java.util.Random random,
                                                    java.lang.String alphabet,
                                                    int size)
        Static factory to retrieve a NanoId String. The string is generated using the given random number generator.
        Parameters:
        random - The random number generator.
        alphabet - The symbols used in the NanoId String.
        size - The number of symbols in the NanoId String.
        Returns:
        A randomly generated NanoId String.