01: package org.databene.domain.address;
02:
03: import org.databene.benerator.IllegalGeneratorStateException;
04: import org.databene.benerator.LightweightGenerator;
05: import org.databene.benerator.primitive.regex.RegexStringGenerator;
06: import org.databene.region.*;
07:
08: /**
09: * Creates Addresses.<br/>
10: * <br/>
11: * Created: 11.06.2006 08:07:40
12: * @author Volker Bergmann
13: */
14: public class AddressGenerator extends LightweightGenerator<Address> {
15:
16: private Country country;
17: private CityGenerator cityGenerator;
18: private MobilePhoneCodeGenerator mobilePhoneNumberGenerator;
19: private StreetNameGenerator streetNameGenerator;
20: private RegexStringGenerator localPhoneNumberGenerator;
21:
22: // constructors ----------------------------------------------------------------------------------------------------
23:
24: public AddressGenerator() {
25: this (Country.getDefault());
26: }
27:
28: public AddressGenerator(Country country) {
29: this .country = country;
30: this .cityGenerator = new CityGenerator(country);
31: this .streetNameGenerator = new StreetNameGenerator(Region
32: .getInstance(country.getIsoCode()));
33: this .mobilePhoneNumberGenerator = new MobilePhoneCodeGenerator(
34: country);
35: this .localPhoneNumberGenerator = new RegexStringGenerator(
36: "[1-9]\\d{5}");
37: }
38:
39: // Generator interface ---------------------------------------------------------------------------------------------
40:
41: public Class<Address> getGeneratedType() {
42: return Address.class;
43: }
44:
45: public Address generate() throws IllegalGeneratorStateException {
46: City city = cityGenerator.generate();
47: Street street = new Street(city, streetNameGenerator.generate());
48: String[] data = street.generateHouseNumberWithZipCode(); // TODO v0.5 make street name generator fit the locale
49: String houseNumber = data[0];
50: String zipCode = data[1];
51: PhoneNumber privatePhone = generatePhoneNumber(city);
52: PhoneNumber officePhone = generatePhoneNumber(city);
53: PhoneNumber mobilePhone = mobilePhoneNumberGenerator.generate();
54: PhoneNumber fax = generatePhoneNumber(city);
55: return new Address(street.getName(), houseNumber, zipCode, city
56: .getName(), country, privatePhone, officePhone,
57: mobilePhone, fax);
58: }
59:
60: // java.lang.Object overrides --------------------------------------------------------------------------------------
61:
62: public String toString() {
63: return getClass().getSimpleName() + '[' + country + ']';
64: }
65:
66: // private helpers -------------------------------------------------------------------------------------------------
67:
68: private PhoneNumber generatePhoneNumber(City city) {
69: int localPhoneCodeLength = 9 - city.getPhoneCode().length();
70: localPhoneNumberGenerator.setPattern("[1-9]\\d{"
71: + (localPhoneCodeLength - 1) + '}');
72: String localCode = localPhoneNumberGenerator.generate();
73: return new PhoneNumber(country.getPhoneCode(), city
74: .getPhoneCode(), localCode);
75: }
76: }
|