View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.benerator.primitive.datetime;
28  
29  import com.rapiddweller.benerator.Generator;
30  import com.rapiddweller.benerator.GeneratorContext;
31  import com.rapiddweller.benerator.NonNullGenerator;
32  import com.rapiddweller.benerator.distribution.Distribution;
33  import com.rapiddweller.benerator.distribution.SequenceManager;
34  import com.rapiddweller.benerator.util.WrapperProvider;
35  import com.rapiddweller.benerator.wrapper.CompositeGenerator;
36  import com.rapiddweller.benerator.wrapper.ProductWrapper;
37  import com.rapiddweller.common.TimeUtil;
38  import com.rapiddweller.model.data.Uniqueness;
39  
40  import java.sql.Time;
41  import java.util.Calendar;
42  import java.util.Date;
43  
44  /**
45   * Creates DateTimes with separate date and time distribution characteristics.<br/><br/>
46   * Created: 29.02.2008 18:19:55
47   *
48   * @author Volker Bergmann
49   * @since 0.5.0
50   */
51  public class DateTimeGenerator extends CompositeGenerator<Date> implements NonNullGenerator<Date> {
52  
53    private DayGenerator dateGenerator;
54    private Generator<Long> timeOffsetGenerator;
55  
56    /**
57     * The Min date.
58     */
59    Date minDate;
60    /**
61     * The Max date.
62     */
63    Date maxDate;
64    /**
65     * The Date granularity.
66     */
67    String dateGranularity;
68    /**
69     * The Date distribution.
70     */
71    Distribution dateDistribution;
72  
73    /**
74     * The Min time.
75     */
76    long minTime;
77    /**
78     * The Max time.
79     */
80    long maxTime;
81    /**
82     * The Time granularity.
83     */
84    long timeGranularity;
85    /**
86     * The Time distribution.
87     */
88    Distribution timeDistribution;
89    private final WrapperProvider<Long> timeWrapperProvider = new WrapperProvider<>();
90  
91    /**
92     * Instantiates a new Date time generator.
93     */
94    public DateTimeGenerator() {
95      this(
96          TimeUtil.add(TimeUtil.today(), Calendar.YEAR, -1),
97          TimeUtil.today(),
98          TimeUtil.time(9, 0),
99          TimeUtil.time(17, 0));
100   }
101 
102   /**
103    * Instantiates a new Date time generator.
104    *
105    * @param minDate the min date
106    * @param maxDate the max date
107    * @param minTime the min time
108    * @param maxTime the max time
109    */
110   public DateTimeGenerator(Date minDate, Date maxDate, Time minTime, Time maxTime) {
111     super(Date.class);
112     setMinDate(minDate);
113     setMaxDate(maxDate);
114     setMinTime(minTime);
115     setMaxTime(maxTime);
116     setDateDistribution(SequenceManager.RANDOM_SEQUENCE);
117     setTimeDistribution(SequenceManager.RANDOM_SEQUENCE);
118     setDateGranularity("00-00-01");
119     setTimeGranularity(TimeUtil.time(0, 1));
120   }
121 
122   // properties ------------------------------------------------------------------------------------------------------
123 
124   /**
125    * Sets min date.
126    *
127    * @param minDate the min date
128    */
129   public void setMinDate(Date minDate) {
130     this.minDate = minDate;
131   }
132 
133   /**
134    * Sets max date.
135    *
136    * @param maxDate the max date
137    */
138   public void setMaxDate(Date maxDate) {
139     this.maxDate = maxDate;
140   }
141 
142   /**
143    * Sets date granularity.
144    *
145    * @param dateGranularity the date granularity
146    */
147   public void setDateGranularity(String dateGranularity) {
148     this.dateGranularity = dateGranularity;
149   }
150 
151   /**
152    * Sets date distribution.
153    *
154    * @param distribution the distribution
155    */
156   public void setDateDistribution(Distribution distribution) {
157     this.dateDistribution = distribution;
158   }
159 
160   /**
161    * Sets min time.
162    *
163    * @param minTime the min time
164    */
165   public void setMinTime(Time minTime) {
166     this.minTime = TimeUtil.millisSinceOwnEpoch(minTime);
167   }
168 
169   /**
170    * Sets max time.
171    *
172    * @param maxTime the max time
173    */
174   public void setMaxTime(Time maxTime) {
175     this.maxTime = TimeUtil.millisSinceOwnEpoch(maxTime);
176   }
177 
178   /**
179    * Sets time granularity.
180    *
181    * @param timeGranularity the time granularity
182    */
183   public void setTimeGranularity(Time timeGranularity) {
184     this.timeGranularity = TimeUtil.millisSinceOwnEpoch(timeGranularity);
185   }
186 
187   /**
188    * Sets time distribution.
189    *
190    * @param distribution the distribution
191    */
192   public void setTimeDistribution(Distribution distribution) {
193     this.timeDistribution = distribution;
194   }
195 
196   // Generator interface ---------------------------------------------------------------------------------------------
197 
198   @Override
199   public void init(GeneratorContext context) {
200     assertNotInitialized();
201     this.dateGenerator = registerComponent(
202         new DayGenerator(minDate, maxDate, dateDistribution, false));
203     dateGenerator.setGranularity(dateGranularity);
204     this.dateGenerator.init(context);
205     this.timeOffsetGenerator = registerComponent(context.getGeneratorFactory().createNumberGenerator(
206         Long.class, minTime, true, maxTime, true, timeGranularity, timeDistribution, Uniqueness.NONE));
207     this.timeOffsetGenerator.init(context);
208     super.init(context);
209   }
210 
211   @Override
212   public ProductWrapper<Date> generate(ProductWrapper<Date> wrapper) {
213     Date result = generate();
214     return (result != null ? wrapper.wrap(result) : null);
215   }
216 
217   @Override
218   public Date generate() {
219     assertInitialized();
220     Date dateGeneration = dateGenerator.generate();
221     if (dateGeneration == null) {
222       return null;
223     }
224     ProductWrapper<Long> timeWrapper = timeOffsetGenerator.generate(timeWrapperProvider.get());
225     if (timeWrapper == null) {
226       return null;
227     }
228     long timeOffsetGeneration = timeWrapper.unwrap();
229     return new Date(dateGeneration.getTime() + timeOffsetGeneration);
230   }
231 
232 }