Class RandomStrings


  • public final class RandomStrings
    extends Object
    Utilities for random string generation. All random distributions are uniform.
    Since:
    0.6.0
    Version:
    $Id: RandomStrings.java 16154 2012-07-14 16:34:05Z colin $
    Author:
    tlerios@marketcetera.com
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_LEN_STR_ALNUM
      The default maximum length of a randomly generated string whose characters are all Unicode code points that are letters or digits; the minimum length is 1.
      static int DEFAULT_LEN_STR_CHARSET
      The default maximum length of a randomly generated string whose characters are all Unicode code points that can be encoded by some charset (supplied or implied); the minimum length is 1.
      static int DEFAULT_LEN_STR_DIGIT
      The default maximum length of a randomly generated string whose characters are all Unicode code points that are digits; the minimum length is 1.
      static int DEFAULT_LEN_STR_ID
      The default maximum length of a randomly generated string whose characters are all Unicode code points; in addition, the first character is always a letter, the second is always a digit, and the rest (if any) are either letters or digits; the minimum length is 2.
      static int DEFAULT_LEN_STR_LETTER
      The default maximum length of a randomly generated string whose characters are all Unicode code points that are letters; the minimum length is 1.
      static int DEFAULT_LEN_STR_VALID
      The default maximum length of a randomly generated string whose characters are all Unicode code points that are deemed valid by StringUtils.isValid(int); the minimum length is 1.
      private static Random sGenerator  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private RandomStrings()
      Constructor.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static String genStr​(UCPFilter filter, int len)
      Returns a generated string of the given length whose characters are all Unicode code points that meet the constraints of the given filter.
      static String genStrAlNum()
      Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_ALNUM) whose characters are all Unicode code points that are letters or digits.
      static String genStrAlNum​(int len)
      Returns a randomly generated string of the given length whose characters are all Unicode code points that are letters or digits.
      static String genStrCharset​(Charset cs)
      Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_CHARSET) whose characters are all Unicode code points that can be encoded by the given charset.
      static String genStrCharset​(Charset cs, int len)
      Returns a randomly generated string of the given length whose characters are all Unicode code points that can be encoded by the given charset.
      static String genStrDefCharset()
      Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_CHARSET) whose characters are all Unicode code points that can be encoded by the default JVM charset.
      static String genStrDefCharset​(int len)
      Returns a randomly generated string of the given length whose characters are all Unicode code points that can be encoded by the default JVM charset.
      static String genStrDigit()
      Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_DIGIT) whose characters are all Unicode code points that are digits.
      static String genStrDigit​(int len)
      Returns a randomly generated string of the given length whose characters are all Unicode code points that are digits.
      static String genStrFileSystem()
      Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_CHARSET) whose characters are all Unicode code points that can be encoded by the current system file encoding/charset (as specified in the system property file.encoding).
      static String genStrFileSystem​(int len)
      Returns a randomly generated string of the given length whose characters are all Unicode code points that can be encoded by the current system file encoding/charset (as specified in the system property file.encoding).
      static String genStrId()
      Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_ID) whose characters are all Unicode code points; in addition, the first character is always a letter, the second is always a digit, and the rest (if any) are either letters or digits.
      static String genStrId​(int len)
      Returns a randomly generated string of the given length whose characters are all Unicode code points; in addition, the first character (if any) is always a letter, the second (if any) is always a digit, and the rest (if any) are either letters or digits.
      static String genStrLetter()
      Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_LETTER) whose characters are all Unicode code points that are letters.
      static String genStrLetter​(int len)
      Returns a randomly generated string of the given length whose characters are all Unicode code points that are letters.
      static String genStrValid()
      Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_VALID) whose characters are all Unicode code points that are deemed valid by StringUtils.isValid(int).
      static String genStrValid​(int len)
      Returns a randomly generated string of the given length whose characters are all Unicode code points that are deemed valid by StringUtils.isValid(int).
      static int genUCP()
      Returns a randomly generated Unicode code point within the full range of valid Unicode scalar values.
      static int genUCP​(UCPFilter filter)
      Returns a randomly generated Unicode code point that meets the constraints of the given filter.
      static int genUCPAlNum()
      Returns a randomly generated Unicode code point that is either a letter or a digit.
      static int genUCPCharset​(Charset cs)
      Returns a randomly generated Unicode code point that can be encoded by the given charset.
      static int genUCPDefCharset()
      Returns a randomly generated Unicode code point that can be encoded by the default JVM charset.
      static int genUCPDigit()
      Returns a randomly generated Unicode code point that is a digit.
      static int genUCPFileSystem()
      Returns a randomly generated Unicode code point that can be encoded by the current system file encoding/charset (as specified in the system property file.encoding).
      static int genUCPLetter()
      Returns a randomly generated Unicode code point that is a letter.
      static int genUCPValid()
      Returns a randomly generated Unicode code point that is deemed valid by StringUtils.isValid(int).
      static void resetGeneratorFixed()
      Resets the random number generator used by this class to a fixed seed value, which henceforth results in a repeatable, deterministic sequence of random code points and strings.
      static long resetGeneratorRandom()
      Resets the random number generator used by this class to a random seed value, and returns that value.
      static void resetGeneratorRandom​(long seed)
      Resets the random number generator used by this class to the given seed value, which henceforth results in a repeatable, deterministic sequence of random code points and strings.
    • Field Detail

      • DEFAULT_LEN_STR_CHARSET

        public static final int DEFAULT_LEN_STR_CHARSET
        The default maximum length of a randomly generated string whose characters are all Unicode code points that can be encoded by some charset (supplied or implied); the minimum length is 1.
        See Also:
        Constant Field Values
      • DEFAULT_LEN_STR_VALID

        public static final int DEFAULT_LEN_STR_VALID
        The default maximum length of a randomly generated string whose characters are all Unicode code points that are deemed valid by StringUtils.isValid(int); the minimum length is 1.
        See Also:
        Constant Field Values
      • DEFAULT_LEN_STR_DIGIT

        public static final int DEFAULT_LEN_STR_DIGIT
        The default maximum length of a randomly generated string whose characters are all Unicode code points that are digits; the minimum length is 1.
        See Also:
        Constant Field Values
      • DEFAULT_LEN_STR_LETTER

        public static final int DEFAULT_LEN_STR_LETTER
        The default maximum length of a randomly generated string whose characters are all Unicode code points that are letters; the minimum length is 1.
        See Also:
        Constant Field Values
      • DEFAULT_LEN_STR_ALNUM

        public static final int DEFAULT_LEN_STR_ALNUM
        The default maximum length of a randomly generated string whose characters are all Unicode code points that are letters or digits; the minimum length is 1.
        See Also:
        Constant Field Values
      • DEFAULT_LEN_STR_ID

        public static final int DEFAULT_LEN_STR_ID
        The default maximum length of a randomly generated string whose characters are all Unicode code points; in addition, the first character is always a letter, the second is always a digit, and the rest (if any) are either letters or digits; the minimum length is 2.
        See Also:
        Constant Field Values
      • sGenerator

        private static Random sGenerator
    • Constructor Detail

      • RandomStrings

        private RandomStrings()
        Constructor. It is private so that no instances can be created.
    • Method Detail

      • resetGeneratorRandom

        public static void resetGeneratorRandom​(long seed)
        Resets the random number generator used by this class to the given seed value, which henceforth results in a repeatable, deterministic sequence of random code points and strings.
        Parameters:
        seed - The seed.
      • resetGeneratorRandom

        public static long resetGeneratorRandom()
        Resets the random number generator used by this class to a random seed value, and returns that value.
        Returns:
        The seed.
      • resetGeneratorFixed

        public static void resetGeneratorFixed()
        Resets the random number generator used by this class to a fixed seed value, which henceforth results in a repeatable, deterministic sequence of random code points and strings.
      • genUCP

        public static int genUCP()
        Returns a randomly generated Unicode code point within the full range of valid Unicode scalar values.
        Returns:
        The code point.
      • genUCP

        public static int genUCP​(UCPFilter filter)
        Returns a randomly generated Unicode code point that meets the constraints of the given filter.
        Parameters:
        filter - The filter.
        Returns:
        The code point.
      • genUCPCharset

        public static int genUCPCharset​(Charset cs)
        Returns a randomly generated Unicode code point that can be encoded by the given charset.
        Parameters:
        cs - The charset.
        Returns:
        The code point.
      • genUCPDefCharset

        public static int genUCPDefCharset()
        Returns a randomly generated Unicode code point that can be encoded by the default JVM charset.
        Returns:
        The code point.
      • genUCPFileSystem

        public static int genUCPFileSystem()
        Returns a randomly generated Unicode code point that can be encoded by the current system file encoding/charset (as specified in the system property file.encoding).
        Returns:
        The code point.
      • genUCPValid

        public static int genUCPValid()
        Returns a randomly generated Unicode code point that is deemed valid by StringUtils.isValid(int).
        Returns:
        The code point.
      • genUCPDigit

        public static int genUCPDigit()
        Returns a randomly generated Unicode code point that is a digit.
        Returns:
        The code point.
      • genUCPLetter

        public static int genUCPLetter()
        Returns a randomly generated Unicode code point that is a letter.
        Returns:
        The code point.
      • genUCPAlNum

        public static int genUCPAlNum()
        Returns a randomly generated Unicode code point that is either a letter or a digit.
        Returns:
        The code point.
      • genStr

        public static String genStr​(UCPFilter filter,
                                    int len)
        Returns a generated string of the given length whose characters are all Unicode code points that meet the constraints of the given filter.
        Parameters:
        filter - The filter.
        len - The length (in code points).
        Returns:
        The string.
      • genStrCharset

        public static String genStrCharset​(Charset cs,
                                           int len)
        Returns a randomly generated string of the given length whose characters are all Unicode code points that can be encoded by the given charset.
        Parameters:
        cs - The charset.
        len - The length (in code points).
        Returns:
        The string.
      • genStrDefCharset

        public static String genStrDefCharset​(int len)
        Returns a randomly generated string of the given length whose characters are all Unicode code points that can be encoded by the default JVM charset.
        Parameters:
        len - The length (in code points).
        Returns:
        The string.
      • genStrFileSystem

        public static String genStrFileSystem​(int len)
        Returns a randomly generated string of the given length whose characters are all Unicode code points that can be encoded by the current system file encoding/charset (as specified in the system property file.encoding).
        Parameters:
        len - The length (in code points).
        Returns:
        The string.
      • genStrValid

        public static String genStrValid​(int len)
        Returns a randomly generated string of the given length whose characters are all Unicode code points that are deemed valid by StringUtils.isValid(int).
        Parameters:
        len - The length (in code points).
        Returns:
        The string.
      • genStrDigit

        public static String genStrDigit​(int len)
        Returns a randomly generated string of the given length whose characters are all Unicode code points that are digits.
        Parameters:
        len - The length (in code points).
        Returns:
        The string.
      • genStrLetter

        public static String genStrLetter​(int len)
        Returns a randomly generated string of the given length whose characters are all Unicode code points that are letters.
        Parameters:
        len - The length (in code points).
        Returns:
        The string.
      • genStrAlNum

        public static String genStrAlNum​(int len)
        Returns a randomly generated string of the given length whose characters are all Unicode code points that are letters or digits.
        Parameters:
        len - The length (in code points).
        Returns:
        The string.
      • genStrId

        public static String genStrId​(int len)
        Returns a randomly generated string of the given length whose characters are all Unicode code points; in addition, the first character (if any) is always a letter, the second (if any) is always a digit, and the rest (if any) are either letters or digits.
        Parameters:
        len - The length (in code points).
        Returns:
        The string.
      • genStrCharset

        public static String genStrCharset​(Charset cs)
        Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_CHARSET) whose characters are all Unicode code points that can be encoded by the given charset.
        Parameters:
        cs - The charset.
        Returns:
        The string.
      • genStrDefCharset

        public static String genStrDefCharset()
        Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_CHARSET) whose characters are all Unicode code points that can be encoded by the default JVM charset.
        Returns:
        The string.
      • genStrFileSystem

        public static String genStrFileSystem()
        Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_CHARSET) whose characters are all Unicode code points that can be encoded by the current system file encoding/charset (as specified in the system property file.encoding).
        Returns:
        The string.
      • genStrDigit

        public static String genStrDigit()
        Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_DIGIT) whose characters are all Unicode code points that are digits.
        Returns:
        The string.
      • genStrLetter

        public static String genStrLetter()
        Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_LETTER) whose characters are all Unicode code points that are letters.
        Returns:
        The string.
      • genStrAlNum

        public static String genStrAlNum()
        Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_ALNUM) whose characters are all Unicode code points that are letters or digits.
        Returns:
        The string.
      • genStrId

        public static String genStrId()
        Returns a randomly generated string of random length (bound by DEFAULT_LEN_STR_ID) whose characters are all Unicode code points; in addition, the first character is always a letter, the second is always a digit, and the rest (if any) are either letters or digits.
        Returns:
        The string.