Class FakeValuesService
- java.lang.Object
-
- com.github.javafaker.service.FakeValuesService
-
public class FakeValuesService extends java.lang.Object
-
-
Constructor Summary
Constructors Constructor Description FakeValuesService(java.util.Locale locale, RandomService randomService)Resolves YAML file using the most specific path first based on language and country code.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Stringbothify(java.lang.String string)Applies both anumerify(String)and aletterify(String)over the incoming string.java.lang.Stringbothify(java.lang.String string, boolean isUpper)Applies both anumerify(String)and aletterify(String, boolean)over the incoming string.java.lang.Stringexpression(java.lang.String expression, Faker faker)resolves an expression using the current faker.java.lang.Objectfetch(java.lang.String key)Fetch a random value from an array item specified by the keyjava.lang.ObjectfetchObject(java.lang.String key)Return the object selected by the key from yaml file.java.lang.StringfetchString(java.lang.String key)Same asfetch(String)except this casts the result into a String.java.lang.Stringletterify(java.lang.String letterString)Returns a string with the '?' characters in the parameter replaced with random alphabetic characters.java.lang.Stringletterify(java.lang.String letterString, boolean isUpper)Returns a string with the '?' characters in the parameter replaced with random alphabetic characters.protected java.util.List<java.util.Locale>localeChain(java.util.Locale from)Convert the specified locale into a chain of locales used for message resolution.java.lang.Stringnumerify(java.lang.String numberString)Returns a string with the '#' characters in the parameter replaced with random digits between 0-9 inclusive.java.lang.Stringregexify(java.lang.String regex)Generates a String that matches the given regular expression.java.lang.Stringresolve(java.lang.String key, java.lang.Object current, Faker root)Resolves a key to a method on an object.protected java.lang.StringresolveExpression(java.lang.String expression, java.lang.Object current, Faker root)processes a expression in the style #{X.y} using the current objects as the 'current' location within the yml file (or theFakerobject hierarchy as it were).java.lang.StringsafeFetch(java.lang.String key, java.lang.String defaultIfNull)Safely fetches a key.
-
-
-
Constructor Detail
-
FakeValuesService
public FakeValuesService(java.util.Locale locale, RandomService randomService)Resolves YAML file using the most specific path first based on language and country code. 'en_US' would resolve in the following order:
- /en-US.yml
- /en.yml
Localeinstance. This is legacy behavior and not condoned, but it will work.- EN_US
- En-Us
- eN_uS
- Parameters:
locale-randomService-
-
-
Method Detail
-
localeChain
protected java.util.List<java.util.Locale> localeChain(java.util.Locale from)
Convert the specified locale into a chain of locales used for message resolution. For example:Locale.FRANCE(fr_FR) -> [ fr_FR, anotherTest, en ]- Returns:
- a list of
Localeinstances
-
fetch
public java.lang.Object fetch(java.lang.String key)
Fetch a random value from an array item specified by the key- Parameters:
key-- Returns:
-
fetchString
public java.lang.String fetchString(java.lang.String key)
Same asfetch(String)except this casts the result into a String.- Parameters:
key-- Returns:
-
safeFetch
public java.lang.String safeFetch(java.lang.String key, java.lang.String defaultIfNull)Safely fetches a key.If the value is null, it will return an empty string.
If it is a list, it will assume it is a list of strings and select a random value from it.
If the retrieved value is an slash encoded regular expression such as
/[a-b]/then the regex will be converted to a regexify expression and returned (ex.#regexify '[a-b]')Otherwise it will just return the value as a string.
- Parameters:
key- the key to fetch from the YML structure.defaultIfNull- the value to return if the fetched value is null- Returns:
- see above
-
fetchObject
public java.lang.Object fetchObject(java.lang.String key)
Return the object selected by the key from yaml file.- Parameters:
key- key contains path to an object. Path segment is separated by dot. E.g. name.first_name- Returns:
-
numerify
public java.lang.String numerify(java.lang.String numberString)
Returns a string with the '#' characters in the parameter replaced with random digits between 0-9 inclusive. For example, the string "ABC##EFG" could be replaced with a string like "ABC99EFG".- Parameters:
numberString-- Returns:
-
bothify
public java.lang.String bothify(java.lang.String string)
Applies both anumerify(String)and aletterify(String)over the incoming string.- Parameters:
string-- Returns:
-
bothify
public java.lang.String bothify(java.lang.String string, boolean isUpper)Applies both anumerify(String)and aletterify(String, boolean)over the incoming string.- Parameters:
string-isUpper-- Returns:
-
regexify
public java.lang.String regexify(java.lang.String regex)
Generates a String that matches the given regular expression.
-
letterify
public java.lang.String letterify(java.lang.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".- Parameters:
letterString-- Returns:
-
letterify
public java.lang.String letterify(java.lang.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".- Parameters:
letterString-isUpper- specifies whether or not letters should be upper case- Returns:
-
resolve
public java.lang.String resolve(java.lang.String key, java.lang.Object current, Faker root)Resolves a key to a method on an object.#{hello} with result in a method call to current.hello();
#{Person.hello_someone} will result in a method call to person.helloSomeone();
-
expression
public java.lang.String expression(java.lang.String expression, Faker faker)resolves an expression using the current faker.- Parameters:
expression-faker-- Returns:
-
resolveExpression
protected java.lang.String resolveExpression(java.lang.String expression, java.lang.Object current, Faker root)processes a expression in the style #{X.y} using the current objects as the 'current' location within the yml file (or the
Fakerobject hierarchy as it were).#{Address.streetName} would get resolved to
Faker.address()'sAddress.streetName()#{address.street} would get resolved to the YAML > locale: faker: address: street:
Combinations are supported as well: "#{x} #{y}"
Recursive templates are supported. if "#{x}" resolves to "#{Address.streetName}" then "#{x}" resolves to
Faker.address()'sAddress.streetName().
-
-