01: package org.databene.domain.person;
02:
03: import java.util.Date;
04: import java.util.Calendar;
05:
06: import org.databene.benerator.primitive.DateGenerator;
07: import org.databene.benerator.Sequence;
08: import org.databene.benerator.IllegalGeneratorStateException;
09: import org.databene.benerator.LightweightGenerator;
10: import org.databene.commons.TimeUtil;
11: import org.databene.commons.Period;
12:
13: /**
14: * (c) Copyright 2006 by Volker Bergmann
15: * Created: 13.06.2006 07:15:03
16: */
17: public class BirthDateGenerator extends LightweightGenerator<Date> {
18:
19: private int minAgeYears;
20: private int maxAgeYears;
21:
22: private DateGenerator dateGenerator;
23:
24: public BirthDateGenerator() {
25: this (18, 80);
26: }
27:
28: public BirthDateGenerator(int minAgeYears, int maxAgeYears) {
29: this .minAgeYears = minAgeYears;
30: this .maxAgeYears = maxAgeYears;
31: Date today = TimeUtil.today().getTime();
32: Calendar min = TimeUtil.calendar(today);
33: min.add(Calendar.YEAR, -maxAgeYears);
34: Calendar max = TimeUtil.calendar(today);
35: max.add(Calendar.YEAR, -minAgeYears);
36: dateGenerator = new DateGenerator(min.getTime(), max.getTime(),
37: Period.DAY.getMillis());
38: dateGenerator.setDistribution(Sequence.RANDOM);
39: }
40:
41: public Class<Date> getGeneratedType() {
42: return Date.class;
43: }
44:
45: public Date generate() throws IllegalGeneratorStateException {
46: return dateGenerator.generate();
47: }
48:
49: public String toString() {
50: return getClass().getSimpleName() + "[minAgeYears="
51: + minAgeYears + ", maxAgeYears=" + maxAgeYears + ']';
52: }
53: }
|