Menu

A Quick Introduction To DataMakerAPI

Clark

DataMakerAPI Textual Data

A more extensive user manual is included in the downloadable tarball from SourceForge as well as GroovyDoc detailing the calling conventions for all of the available methods. This Wiki entry provides a sampling from the user manual.

DataMakerAPI can fabricate a variety of textual data. Addresses are easy and realistic for U.S. address types. Here is an example showing how randomized data can be generated for addresses :

String rslt = AddressFactory.addressToPerson(Gender.findRandomGender(), 
              NameFactory.FIRST_MI_LAST);

Sample results from 3 calls to this method :

Kade A. Owen : 7698 Vinal EXPY : Sweet Home MS : 46019-3420
Myles Z. Townsend : 3456 Boylston RTE E : Chewelah GA : 94614-7535
Tucker Y. Mclaughlin : 315 Kenney AVE BSMT : Eagle Point OK : 14688-0304
...

The fields in the return string are separated with colons.

Note that in the call to addressToPerson( ) a Gender object is found randomly from the Gender enum type. The enum types in this API provide methods to get a randomized object in addition to sensible attributes like a descriptive string or abbreviation, etc.

The name format, not just the value, could have been randomized in the addresses above by changing the method called in the NameFactory like so :

String name = NameFactory.randomNameFormat();

Sample output from 7 calls to the method above :

Alexandria Morris
N. O. Joseph
Marquez, Bryant
Eliza Janet Matthews
Blaine E. Mcdonald
Pham, N. V.
Fletcher, Branden

The AddressFactory relies on other factories and enum types that you can call yourself when your data need is fine-grained, as in the call to NameFactory.randomNameFormat() above. Here is an example for getting a random city name :

String rslt = CityFactory.cityName();

Sample output from 3 calls to the method above :

Oakridge
Elmer City
Chehalis
...

DataMakerAPI has a free-form text ability too. You can specify the amount of text you want and get back some random words to fill that length requirement :

// Get random text that is 30 characters in length.
String rslt = TextFactory.textOfLength(30);

Sample output from 5 calls to the method above :

a i incidental evidence is yet
possessed own so sun rapping a
intellect hoped sooner a rob i
above appeared arrive hour day
connaitre up attested chosen i

Since an API cannot provide for every possible kind of data, methods are provided to select randomly from a list of input. You can create a list of something (aircraft part designations, employee types, dog breeds, etc...) and receive a member from that list returned back to you in a randomized way :

List<String> dogBreeds = new LinkedList<String>();
// Populate your list here.
String breed = TextFactory.fromList(dogBreeds);

Sample output from 3 calls to the method above :

Greyhound
Boston Terrier
Shih Tsu
...

DataMakerAPI Boolean Data

DataMakerAPI has a BooleanFactory to help you produce boolean data in different formats.

int val = BooleanFactory.makeRandomBooleanInt();

Some sample output from 5 calls to the method above :

0
1
0
1
1

You might prefer text.

String val = BooleanFactory.makeRandomBooleanString();

Some sample output from 6 calls to the method above :

FALSE
FALSE
TRUE
FALSE
TRUE
FALSE

Or maybe you want something unusual.

String trueValue = "1";
String falseValue = "-1";
String val = BooleanFactory.makeRandomBooleanStringFromArgs(trueValue, falseValue);

Some sample output from 5 calls to the method above :

1
-1
-1
1
1

DataMakerAPI Temporal Data

DataMakerAPI has a DateFactory to help you produce temporal data in different formats. Here are several examples that show how you can produce temporal data to suit your needs.

Make a random date in some year :

Date rslt = DateFactory.makeDateInYear(1979);

Some sample output from 5 calls to the method above :

Sat Dec 01 14:45:21 EST 1979
Mon Jan 01 13:07:45 EST 1979
Thu Aug 16 08:56:47 EDT 1979
Wed Jan 10 03:42:49 EST 1979
Sat Jul 07 12:06:51 EDT 1979

Make a random date in a range of years :

Date rslt = DateFactory.makeDateInYearRange(1955, 1985);

Some sample output from 5 calls to the method above :

Fri Jun 30 02:17:57 EDT 1978
Tue Mar 23 01:34:43 EST 1976
Thu May 01 20:23:19 EDT 1958
Tue Apr 09 00:47:07 EST 1963
Sat Aug 01 17:22:43 EDT 1959

There are also methods that format the randomized date before return. Here we are making a random date in some month and formatting the output :

String rslt = DateFactory.formattedDateInMonthYear(DateFactory.FEBRUARY, 2005, "dd MMM yyyy");

Some sample output from 5 calls to the method above :

03 Feb 2005
17 Feb 2005
14 Feb 2005
07 Feb 2005
14 Feb 2005

DataMakerAPI Alpha-Numeric Patterns

An AlphaNumFactory was included to make randomized patterns of alpha-numeric sequences. In the patternedNumberString method you choose a symbol that will be a placeholder for a random digit. All the other symbols in the pattern are literals in the output string. These methods are handy for making data for machine part numbers, document numbers, etc... . A picture is worth a thousand words, so...

String pattern = "00-AKDDDC-99-01";
char numSymbol = 'D';
String rslt = AlphaNumFactory.patternedNumberString(pattern, numSymbol);

Some sample output from 5 calls to the method above :

00-AK498C-99-01
00-AK787C-99-01
00-AK031C-99-01
00-AK330C-99-01
00-AK143C-99-01

The patternedAlphaNumString method substitutes random alpha characters and numbers according to your pattern. Placeholder symbols in the pattern string will be substituted for a random value and the rest of the pattern characters are literals in the output string.

String pattern = "00-AKNNNC-NN-CC-NN";
char numSymbol = 'N';
char alphaSymbol = 'C';
String rslt = AlphaNumFactory.patternedAlphaNumString(pattern, alphaSymbol, numSymbol);

Some sample output from 5 calls to the method above :

00-AK605a-39-GJ-93
00-AK988U-53-jy-81
00-AK741a-53-fa-50
00-AK642d-17-AA-17
00-AK829r-37-sU-69

DataMakerAPI BigIntegerFactory

The BigIntegerFactory provides methods to create really big integers with the length you specify. There are three flavors in these factory methods. You can have an integer of fixed length, an integer of bounded length (lower bound to upper bound), or an integer returned as a string in scientific notation. Here are some examples.

Here is how to create a randomized BigInteger with some fixed length :

BigInteger bi = BigIntegerFactory.integerOfLength(29);

Some sample output from 5 calls to the method above :

41275908381577455932111680082
79745548423703822097330035467
32514439577579588340438502466
26341578398946226016437349489
47858071190711348505317450087

Here is how you can generate a BigInteger in a bounded range of lengths :

int lowEnd = 2;
int highEnd = 32;
BigInteger rslt = BigIntegerFactory.integerInBoundedLength(lowEnd, highEnd);

Some sample output from 5 calls to the method above :

4363694614777682067236
84610727480
5541383243634991668809819810
302836248289
9317

When you do the scientific notation you pass arguments for the size of number being generated as well as the precision of the scientific noation used to represent that large number. Here, we represent a number that has 23 places, but only 3 places of precision after the decimal :

String rslt = BigIntegerFactory.scientificOfLength(23, 3);

Some sample output from 5 calls to the method above :

6.911E22
8.321E22
6.191E22
5.195E22
2.613E22

DataMakerAPI DoubleFactory

The DoubleFactory has two methods that generate doubles. The first is a method to produce a double bounded by a range :

long lowEnd = 3L;
long highEnd = 1021L;
double d = DoubleFactory.doubleInRange(lowEnd, highEnd);

Some sample output from 6 calls to the method above :

5.2814105369157796
376.0020679917318
809.1332132172546
631.137964710534
239.0232453275654
1020.4800572206361

The other method produces bounded random doubles in scientific notation. Note how you need to specify the precision for the scientific notation. The final argument is a boolean for extending zeros or not extending zeros in the output. I chose not to extend zeros :

long lowEnd = 1359727;
long highEnd = lowEnd + 7876991;
int precision = 15;
String rslt = DoubleFactory.scientificDoubleInRange(lowEnd, highEnd, precision, false);

Some sample output from 6 calls to the method above :

2.34877982129684E6
8.789176539183173E6
9.00219946781635E6
7.323470881927416E6
8.307511830624728E6
4.484024538170883E6

There is a FloatFactory that does the same work for float values. The FloatFactory is so like the DoubleFactory that you don't need to see an example to know how it works.

DataMakerAPI IntegerFactory

The IntegerFactory offers a rich set of methods to produce random data. Here we see a method that produces a random digit with each call.

int val = IntegerFactory.randomDigit();

Some sample output from 5 calls to the method above :

5
5
7
9
6

Here is a method that generates a random number in some range. Note how the low end is a negative number in this case :

int lowEnd = -17;
int highEnd = 13;
int val = IntegerFactory.randomNumberInRange(lowEnd, highEnd);

Some sample output from 5 calls to the method above :

-11
-1
-9
7
8

Here is a method that lets you specify a length for the output :

int val = IntegerFactory.randomIntValueOfLength(5);

Some sample output from 5 calls to the method above :

95180
50541
55864
79111
30828

I include a method that selects a random entry from a list of integers given to the method as an argument. This method is good for cases where the domain of the output is not contiguous.

List<Integer> integers = new LinkedList<Integer>();
integers.add(3);
integers.add(7);
integers.add(11);
integers.add(19);
int val = IntegerFactory.randomIntegerFromList(integers);

Some sample output from 5 calls to the method above :

3
11
7
7
19

Here is a method that returns the scientific representation of an integer bounded by some range. The precision argument was incremented in the example below to show the effect of different precision on the output. The last argument is a boolean indicating whether to extend zeros in the output :

String rslt = IntegerFactory.scientificNumberInRange(7, 158246987, precision, false);

Some sample output from 5 calls to the method above :

4.103E7
5.9397E7
1.0389E8
9.592133E7
2.5082321E7

A factory exists for long data types as well. It is just like the integer factory.

DataMakerAPI MoneyFactory

The MoneyFactory class attempts to deal with money amounts in different currencies. Here is a method offering you control over how an amount is output. Note how a constant is provided to give you a way to output the currency with the correct symbol :

char numSymbol = '@';
String amount = MoneyFactory.patternCurrencyAmount(MoneySymbols.BRAZIL_REAL.symbol + 
                " @@,@@@.@@", numSymbol);

Some sample output from 5 calls to the method above :

R$ 52,139.03
R$ 10,697.14
R$ 55,579.51
R$ 28,204.75
R$ 89,731.08

Here is a method that give you a way to output a random amount in a manner that is correct for a specific locale. The trick is that Java needs to support the locale you are working with :

String rslt = MoneyFactory.makeFormattedCurrencyAmount(10, 4, Locale.ITALY);

Some sample output from 5 calls to the method above :

€ 8.219.981.935,71
€ 2.447.444.310,98
€ 4.564.652.183,20
€ 5.310.249.517,76
€ 3.180.078.328,85

Related

Wiki: Home

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.