com.mifmif.common.regex
Class Generex

java.lang.Object
  extended by com.mifmif.common.regex.Generex
All Implemented Interfaces:
Iterable

public class Generex
extends Object
implements Iterable

A Java utility class that help generating string values that match a given regular expression.It generate all values that are matched by the Regex, a random value, or you can generate only a specific string based on it's lexicographical order .

Author:
y.mifrah

Field Summary
private  dk.brics.automaton.Automaton automaton
           
private  boolean isTransactionNodeBuilt
           
private  int matchedStringCounter
           
private  List<String> matchedStrings
           
private  Map<String,String> predefinedCharacterClasses
           
private  int preparedTransactionNode
           
private  dk.brics.automaton.RegExp regExp
           
private  Node rootNode
           
 
Constructor Summary
Generex(dk.brics.automaton.Automaton automaton)
           
Generex(String regex)
           
 
Method Summary
private  void buildRootNode()
          Prepare the rootNode and it's child nodes so that we can get matchedString by index
private  String buildStringFromNode(Node node, int indexOrder)
           
private  void generate(String strMatch, dk.brics.automaton.State state, int limit)
           
 List<String> getAllMatchedStrings()
          Generate all Strings that matches the given Regex.
 String getFirstMatch()
           
 String getMatchedString(int indexOrder)
           
 List<String> getMatchedStrings(int limit)
          Generate subList with a size of limit of Strings that matches the given Regex. the Strings are ordered in lexicographical order.
 Iterator iterator()
           
 long matchedStringsSize()
           
private  String prepareRandom(String strMatch, dk.brics.automaton.State state, int minLength, int maxLength)
           
private  List<Node> prepareTransactionNodes(dk.brics.automaton.State state)
          Build list of nodes that present possible transactions from the state.
 String random()
          Generate and return a random String that match the pattern used in this Generex.
 String random(int minLength)
          Generate and return a random String that match the pattern used in this Generex, and the string has a length >= minLength
 String random(int minLength, int maxLength)
          Generate and return a random String that match the pattern used in this Generex, and the string has a length >= minLength and <= maxLength
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

predefinedCharacterClasses

private Map<String,String> predefinedCharacterClasses

regExp

private dk.brics.automaton.RegExp regExp

automaton

private dk.brics.automaton.Automaton automaton

matchedStrings

private List<String> matchedStrings

rootNode

private Node rootNode

isTransactionNodeBuilt

private boolean isTransactionNodeBuilt

matchedStringCounter

private int matchedStringCounter

preparedTransactionNode

private int preparedTransactionNode
Constructor Detail

Generex

public Generex(String regex)

Generex

public Generex(dk.brics.automaton.Automaton automaton)
Method Detail

getMatchedString

public String getMatchedString(int indexOrder)
Parameters:
indexOrder - ( 1<= indexOrder <=n)
Returns:
The matched string by the given pattern in the given it's order in the sorted list of matched String.
indexOrder between 1 and n where n is the number of matched String.
If indexOrder >= n , return an empty string. if there is an infinite number of String that matches the given Regex, the method throws StackOverflowError

buildStringFromNode

private String buildStringFromNode(Node node,
                                   int indexOrder)

getFirstMatch

public String getFirstMatch()
Returns:
first string in lexicographical order that is matched by the given pattern.

matchedStringsSize

public long matchedStringsSize()
Returns:
the number of strings that are matched by the given pattern.

buildRootNode

private void buildRootNode()
Prepare the rootNode and it's child nodes so that we can get matchedString by index


generate

private void generate(String strMatch,
                      dk.brics.automaton.State state,
                      int limit)

prepareTransactionNodes

private List<Node> prepareTransactionNodes(dk.brics.automaton.State state)
Build list of nodes that present possible transactions from the state.

Parameters:
state -
Returns:

getAllMatchedStrings

public List<String> getAllMatchedStrings()
Generate all Strings that matches the given Regex.

Returns:

getMatchedStrings

public List<String> getMatchedStrings(int limit)
Generate subList with a size of limit of Strings that matches the given Regex. the Strings are ordered in lexicographical order.

Parameters:
limit -
Returns:

random

public String random()
Generate and return a random String that match the pattern used in this Generex.

Returns:

random

public String random(int minLength)
Generate and return a random String that match the pattern used in this Generex, and the string has a length >= minLength

Parameters:
minLength -
Returns:

random

public String random(int minLength,
                     int maxLength)
Generate and return a random String that match the pattern used in this Generex, and the string has a length >= minLength and <= maxLength

Parameters:
minLength -
maxLength -
Returns:

prepareRandom

private String prepareRandom(String strMatch,
                             dk.brics.automaton.State state,
                             int minLength,
                             int maxLength)

iterator

public Iterator iterator()
Specified by:
iterator in interface Iterable


Copyright © 2014. All Rights Reserved.