Class BaseFaker

java.lang.Object
net.datafaker.providers.base.BaseFaker
All Implemented Interfaces:
BaseProviders, ProviderRegistration
Direct Known Subclasses:
EntertainmentFaker, Faker, FoodFaker, SportFaker, VideoGameFaker

public class BaseFaker extends Object implements BaseProviders
Provides utility methods for generating fake strings, such as names, phone numbers, addresses. generate random strings with given patterns
Author:
ren
  • Constructor Details

  • Method Details

    • getContext

      public FakerContext getContext()
      Specified by:
      getContext in interface ProviderRegistration
    • doWith

      public <T> T doWith(Callable<T> callable, Locale locale)
    • doWith

      public <T> T doWith(Callable<T> callable, long seed)
    • doWith

      public <T> T doWith(Callable<T> callable, Locale locale, long seed)
    • numerify

      public String numerify(String numberString)
      Returns a string with the '#' characters in the parameter replaced with random digits between 0-9 inclusive or random digits in the range from 1-9 when Ø (not zero) is used.

      For example, the string "ABC##EFG" could be replaced with a string like "ABC99EFG" and the string "Ø##" with a value like "149".

      Specified by:
      numerify in interface ProviderRegistration
      Parameters:
      numberString - Template for string generation
      Returns:
      Generated string
    • letterify

      public String letterify(String letterString)
      Returns a string with the '?' characters in the parameter replaced with random alphabetic characters.

      For example, the string "12??34" could be replaced with a string like "12AB34".

      Specified by:
      letterify in interface ProviderRegistration
      Parameters:
      letterString - Template for string generation
      Returns:
      Generated string.
    • letterify

      public String letterify(String letterString, boolean isUpper)
      Returns a string with the '?' characters in the parameter replaced with random alphabetic characters.

      For example, the string "12??34" could be replaced with a string like "12AB34".

      Specified by:
      letterify in interface ProviderRegistration
    • bothify

      public String bothify(String string)
      Applies both a numerify(String) and a letterify(String) over the incoming string.
      Specified by:
      bothify in interface ProviderRegistration
    • bothify

      public String bothify(String string, boolean isUpper)
      Applies both a numerify(String) and a letterify(String) over the incoming string.
      Specified by:
      bothify in interface ProviderRegistration
    • regexify

      public String regexify(String regex)
      Generates a String that matches the given regular expression.
      Specified by:
      regexify in interface ProviderRegistration
    • examplify

      public String examplify(String example)
      Generates a String by example. The output string will have the same pattern as the input string.

      For example: "AAA" becomes "KRA" "abc" becomes "uio" "948" becomes "345" "A19c" becomes "Z20d"

      Specified by:
      examplify in interface ProviderRegistration
      Parameters:
      example - The input string
      Returns:
      The output string based on the input pattern
    • templatify

      public String templatify(String string, char char2replace, String... options)
      Returns a string with the char2replace characters in the parameter replaced with random option from available options.

      For example, the string "ABC??EFG" could be replaced with a string like "ABCtestтестEFG" if passed options are new String[]{"test", "тест"}.

      Specified by:
      templatify in interface ProviderRegistration
      Parameters:
      string - Template for string generation
      char2replace - Char to replace
      options - Options to use while filling the template
      Returns:
      Generated string
    • templatify

      public String templatify(String string, Map<Character,String[]> optionsMap)
      Returns a string with the characters in the keys of optionsMap parameter replaced with random option from values.

      For example, the string "ABC$$EFG" could be replaced with a string like "ABCtestтестEFG" if passed for key '$' there is value new String[]{"test", "тест"} in optionsMap

      Specified by:
      templatify in interface ProviderRegistration
      Parameters:
      string - Template for string generation
      optionsMap - Map with replacement rules
      Returns:
      Generated string
    • csv

      public String csv(int limit, String... columnExpressions)
      Returns a string with generated csv based number of lines and column expressions. This method will use comma as default delimiter, always prints header and double quote as default quote.

      For example, it could generate "name_column","last_name_column" "Sabrina","Kihn"

      for expression faker.expression("#{csv '1','name_column','#{Name.first_name}','last_name_column','#{Name.last_name}'}");

      Specified by:
      csv in interface ProviderRegistration
      Parameters:
      limit - Number of lines
      columnExpressions - Even number of expressions. The odd numbers args are used for columns names, and even for column values.
      Returns:
      Generated string
    • csv

      public String csv(String separator, char quote, boolean withHeader, int limit, String... columnExpressions)
      Returns a string with generated csv based number of lines and column expressions.

      For example, it could generate "Thad" ### "Crist" "Kathryne" ### "Wuckert" "Sybil" ### "Connelly"

      for expression faker.expression("#{csv ' ### ','"','false','3','name_column','#{Name.first_name}','last_name_column','#{Name.last_name}'}");

      Specified by:
      csv in interface ProviderRegistration
      Parameters:
      separator - Delimiter to use
      quote - Quote to use
      withHeader - Print header or not
      limit - Number of lines
      columnExpressions - Even number of expressions. The odd numbers args are used for columns names, and even for column values.
      Returns:
      Generated string
    • json

      public String json(String... fieldExpressions)
      Specified by:
      json in interface ProviderRegistration
    • jsona

      public String jsona(String... fieldExpressions)
      Specified by:
      jsona in interface ProviderRegistration
    • random

      public RandomService random()
      Specified by:
      random in interface ProviderRegistration
    • fakeValuesService

      public FakeValuesService fakeValuesService()
      Specified by:
      fakeValuesService in interface ProviderRegistration
    • addPath

      public void addPath(Locale locale, Path path)
      Allows to add paths to files with custom data. Data should be in YAML format.
      Specified by:
      addPath in interface ProviderRegistration
      Parameters:
      locale - the locale for which a path is going to be added.
      path - path to a file with YAML structure
      Throws:
      IllegalArgumentException - in case of invalid path
    • addUrl

      public void addUrl(Locale locale, URL url)
      Allows to add urls of files with custom data. Data should be in YAML format.
      Specified by:
      addUrl in interface ProviderRegistration
      Parameters:
      locale - the locale for which an url is going to be added.
      url - url of a file with YAML structure
      Throws:
      IllegalArgumentException - in case of invalid url
    • populate

      public static <T> T populate(Class<T> clazz)
    • populate

      public static <T> T populate(Class<T> clazz, Schema<Object,?> schema)
    • getProvider

      public static <PR extends ProviderRegistration, AP extends AbstractProvider<PR>> AP getProvider(Class<AP> clazz, Function<PR,AP> valueSupplier, PR faker)
    • collection

      public <T> FakeSequence.Builder<T> collection()
      Returns:
      builder to build FakeCollection
    • collection

      @SafeVarargs public final <T> FakeSequence.Builder<T> collection(Supplier<T>... suppliers)
    • collection

      public final <T> FakeSequence.Builder<T> collection(List<Supplier<T>> suppliers)
    • stream

      public <T> FakeSequence.Builder<T> stream()
      Returns:
      builder to build FakeStream
    • stream

      @SafeVarargs public final <T> FakeSequence.Builder<T> stream(Supplier<T>... suppliers)
    • stream

      public final <T> FakeSequence.Builder<T> stream(List<Supplier<T>> suppliers)
    • resolve

      public String resolve(String key)
      Specified by:
      resolve in interface ProviderRegistration
    • resolve

      public String resolve(String key, Supplier<String> message)
      Specified by:
      resolve in interface ProviderRegistration
    • expression

      public String expression(String expression)
      Allows the evaluation of native YML expressions to allow you to build your own.

      The following are valid expressions:

      • #{regexify '(a|b){2,3}'}
      • #{regexify '\\.\\*\\?\\+'}
      • #{bothify '????','false'}
      • #{Name.first_name} #{Name.first_name} #{Name.last_name}
      • #{number.number_between '1','10'}
      Specified by:
      expression in interface ProviderRegistration
      Parameters:
      expression - (see examples above)
      Returns:
      the evaluated string expression
      Throws:
      RuntimeException - if unable to evaluate the expression
    • getFaker

      public final <B extends ProviderRegistration> B getFaker()
      Specified by:
      getFaker in interface ProviderRegistration
    • getProvider

      public static AbstractProvider<?> getProvider(String className, FakerContext context)
    • getMethod

      public static Method getMethod(AbstractProvider<?> ap, String methodName)