Finding a Concise Alternative for RandomStringUtils

After upgrading to org.apache.commons:commons-lang3:3.6from, 3.5I get a lot of warnings about RandomStringUtilsthat that are out of date. Recommended Alternative RandomStringGeneratorFrom commons-text. However, this class is very awkward to use if all you need is just a string (say in unit test). For comparison:

String name1 = RandomStringUtils.randomAlphabetic(FIRST_NAME_LENGTH);
String name2 = new RandomStringGenerator.Builder().withinRange('a', 'z').build()
        .generate(FIRST_NAME_LENGTH);

(I know that these are not even the same semantics, but I wanted it to be short.)

So I'm looking for a short and elegant way, ideally a replacement; Java 8, Spring, Guava, and even test libraries are welcome.

+4
source share
1 answer

Facade pattern, "" RandomStringGenerator.

:

public class RandomStringUtilsFacade
{
    public static String randomAlphabetic (final int firstNameLength)
    {
        return randomAlphabetic(firstNameLength, 'a', 'z');
    }

    // If you want to use the range
    public static String randomAlphabetic (final int firstNameLength, 
                                           final char low, final char high)
    {
        return new RandomStringGenerator.Builder().withinRange(low, high).build()
        .generate(firstNameLength);
    }
}
+3

Source: https://habr.com/ru/post/1680125/


All Articles