Skip to content

Modifiers

Samuel Beausoleil edited this page Mar 22, 2022 · 3 revisions

Modifiers modify a new object before it is returned to the caller. There are three modifiers provided out of the box:

  • FieldMod
  • MethodMod
  • MixedMultiMod

Note that you may add as many modifiers, one after the other, to a generate() call. If you generate multiple objects at the same time, all the modifiers will be applied to all the objects.

City city = new CityGenerator().generate(new FieldMod("nCitizens", 5), new MethodMod("setName", "Toronto"));

Note that modifiers are usually applied after the object has been created.

FieldMod

A FieldMod works by specifying a path to a field to modify and then the value to replace it with.

// Make this city have 15k citizens
City city = cityGenerator.generate(new FieldMod("nCitizens", 15_000L));

It can also follow a path to find a field within any number of objects. Just give it the name of the fields to enter one after the other separated by a '.'.

// Make the city of this address have 15k citizens
Address address = addressGenerator.generate(new FieldMod("city.nCitizens", 15_000L));

MethodMod

A MethodMod works by specifying a method to call with (optionally) arguments to give it on the created objects. The targetted can have any access modifier. Public or private, it makes no difference. If it exists within the object, it will be called.

// Give the new city 15k citizens
City city = new CityGenerator().generate(new MethodMod("setnCitizens", 15_000L));

MixedMultiMod

A MixedMultiMod is a modifier that combines any number of FieldMods and MethodMods in a single container for quick writing.

MixedMultiMod mod = new MixedMultiMod(
                 "postalCode", "H4",
                 "setCity()", new City("Ottawa", 1_408_000L),
                 "setCity(1)", new City("Quebec", 832_000L),
                 "setNumberAndStreet(2)", 123, "Street Name",
                 "city.nCitizens", 1234
);

It follows a simple logic: indexor -> value(s). The indexor must be the name of a field or of a method. To specify that what you wish to target is a method, it must end with an open-close set of parenthesis containing the number of arguments it takes (if no number is written, it is assumed the method takes 1 argument). You can specify a zero-arguments method by writing 0 as its number of arguments (myZeroArgsMethod(0)).

Clone this wiki locally