[ 
https://issues.apache.org/jira/browse/TEXT-34?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15763499#comment-15763499
 ] 

Duncan Jones commented on TEXT-34:
----------------------------------

{quote}
I'd suggest adding more methods like {{withRange(char, char)}}
{quote}

I really like that syntax. I think I might remove {{withMinimum/Maximum}} in 
favour of {{withinRange(int, int)}}. It also avoids the issue of deferring the 
{{min <= max}} check until {{build()}} is called.

{quote}
... {{withDigits()}} or {{withLetter()}} to make common cases easier.
{quote}

Did you notice the fields I defined? It means you can do:

{code:java}
import static org.apache.commons.text.RandomStringBuilder.DIGIT_PREDICATE;
...
String str = new 
RandomStringBuilder().ofLength(5000).filteredBy(DIGIT_PREDICATE).build();
{code}

{quote}
Regarding the filter, I wonder if it would be worth making the pending string 
accessible to the test method. A test could thus be based on the previous 
characters, and it would be possible to write a test avoiding the repetitions 
characters.
{quote}

Nothing prevents a custom filter from saving the characters seen so far:

{code:java}
CodePointPredicate uniqueFilter = new CodePointPredicate() {
    private final Set<Integer> seen = new HashSet<>();

    @Override
    public boolean test(int codePoint) {
        return seen.add(Integer.valueOf(codePoint));
    }
};

String str = new 
RandomStringBuilder().ofLength(26).withMinimum('A').withMaximum('Z')
        .filteredBy(uniqueFilter)
        .build();
System.out.println(str);
{code}

(Note: building this example showed me that I've messed up the random number 
generation, so I'll add a test to catch that and will fix it in the pull 
request)

If we think accessing the previous string is a common enough occurrence, we 
could update {{CodePointPredicate}} so that it's passed an object representing 
the current state of the string, allowing for queries. In your specific 
example, I'm not sure that makes the code any better, since you would need to 
repeatedly scan the string for each new character.

> Add class to generate random strings
> ------------------------------------
>
>                 Key: TEXT-34
>                 URL: https://issues.apache.org/jira/browse/TEXT-34
>             Project: Commons Text
>          Issue Type: New Feature
>            Reporter: Duncan Jones
>            Assignee: Duncan Jones
>
> The {{RandomStringUtils}} class will be deprecated in Commons Lang and reborn 
> in Commons Text.
> Because the original class has some complicated parameters and potentially 
> unnecessary capabilities, I'll simplify as I move it across. I'll also ensure 
> the class plays nicely with Unicode characters.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to