001: /*
002: * (c) Copyright 2006 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.benerator.primitive;
028:
029: import org.databene.benerator.primitive.number.adapter.LongGenerator;
030: import org.databene.benerator.Sequence;
031: import org.databene.benerator.Distribution;
032: import org.databene.benerator.Generator;
033: import org.databene.commons.Period;
034:
035: import java.util.Date;
036: import java.util.GregorianCalendar;
037:
038: /**
039: * creates date values by a LongGenerator.
040: * @see LongGenerator<br/>
041: * <br/>
042: * Created: 07.06.2006 22:54:28
043: */
044: public class DateGenerator implements Generator<Date> {
045:
046: /** The generator to use for generating millisecond values */
047: private LongGenerator source;
048:
049: // constructors ----------------------------------------------------------------------------------------------------
050:
051: /** Initializes the generator to create days within about the last 80 years with a one-day resolution */
052: public DateGenerator() {
053: this (defaultStartDate(), currentDay(), Period.DAY.getMillis());
054: }
055:
056: /** Initializes the generator to create dates with a uniform distribution */
057: public DateGenerator(Date min, Date max, long precision) {
058: this (min, max, precision, Sequence.RANDOM);
059: }
060:
061: /** Initializes the generator to create dates of a Sequence or WeightFunction */
062: public DateGenerator(Date min, Date max, long precision,
063: Distribution distribution) {
064: source = new LongGenerator((min != null ? min.getTime()
065: : Long.MIN_VALUE), (max != null ? max.getTime()
066: : Long.MAX_VALUE), precision, distribution);
067: }
068:
069: // config properties -----------------------------------------------------------------------------------------------
070:
071: /** Returns the earliest date to generate */
072: public Date getMin() {
073: return new Date(source.getMin());
074: }
075:
076: /** Sets the earliest date to generate */
077: public void setMin(Date min) {
078: source.setMin(min.getTime());
079: }
080:
081: /** Returns the latest date to generate */
082: public Date getMax() {
083: return new Date(source.getMax());
084: }
085:
086: /** Sets the latest date to generate */
087: public void setMax(Date max) {
088: source.setMax(max.getTime());
089: }
090:
091: /** Returns the date precision in milliseconds */
092: public Long getPrecision() {
093: return source.getPrecision();
094: }
095:
096: /** Sets the date precision in milliseconds */
097: public void setPrecision(Long precision) {
098: source.setPrecision(precision);
099: }
100:
101: /** Returns the distribution used */
102: public Distribution getDistribution() {
103: return source.getDistribution();
104: }
105:
106: /** Sets the distribution to use */
107: public void setDistribution(Distribution distribution) {
108: source.setDistribution(distribution);
109: }
110:
111: /** Returns the first sequence-specific variation parameter */
112: public Long getVariation1() {
113: return source.getVariation1();
114: }
115:
116: /** Sets the first sequence-specific variation parameter */
117: public void setVariation1(Long varation1) {
118: source.setVariation1(varation1);
119: }
120:
121: /** Returns the second sequence-specific variation parameter */
122: public Long getVariation2() {
123: return source.getVariation2();
124: }
125:
126: /** Sets the second sequence-specific variation parameter */
127: public void setVariation2(Long variation2) {
128: source.setVariation2(variation2);
129: }
130:
131: // source interface ---------------------------------------------------------------------------------------------
132:
133: public Class<Date> getGeneratedType() {
134: return Date.class;
135: }
136:
137: public void validate() {
138: source.validate();
139: }
140:
141: /** Generates a Date by creating a millisecond value from the source generator and wrapping it into a Date */
142: public Date generate() {
143: return new Date(source.generate());
144: }
145:
146: public void reset() {
147: source.reset();
148: }
149:
150: public void close() {
151: source.close();
152: }
153:
154: public boolean available() {
155: return source.available();
156: }
157:
158: // implementation --------------------------------------------------------------------------------------------------
159:
160: /** Returns the default start date as 80 years ago */
161: private static Date defaultStartDate() {
162: return new Date(currentDay().getTime() - 80L * 365
163: * Period.DAY.getMillis());
164: }
165:
166: /** Returns the current day as Date value rounded to midnight */
167: private static Date currentDay() {
168: GregorianCalendar calendar = new GregorianCalendar();
169: calendar.set(calendar.get(GregorianCalendar.YEAR), calendar
170: .get(GregorianCalendar.MONTH), calendar
171: .get(GregorianCalendar.DAY_OF_MONTH), 0, 0, 0);
172: calendar.set(GregorianCalendar.MILLISECOND, 0);
173: return new Date();
174: }
175:
176: }
|