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;
28  
29  import com.rapiddweller.benerator.Generator;
30  import com.rapiddweller.benerator.GeneratorContext;
31  import com.rapiddweller.benerator.distribution.Distribution;
32  import com.rapiddweller.benerator.distribution.SequenceManager;
33  import com.rapiddweller.benerator.wrapper.GeneratorProxy;
34  import com.rapiddweller.script.Expression;
35  import com.rapiddweller.script.expression.ExpressionUtil;
36  
37  /**
38   * {@link Generator} implementation that generates {@link Long} numbers,
39   * redefining the underlying distribution on each <code>reset()</code> by
40   * evaluating the <code>min</code>, <code>max</code>, <code>granularity</code>,
41   * <code>distribution</code> and <code>unique</code> values.<br/><br/>
42   * Created: 27.03.2010 19:28:38
43   *
44   * @author Volker Bergmann
45   * @since 0.6.0
46   */
47  public class DynamicLongGenerator extends GeneratorProxy<Long> {
48  
49    /**
50     * The Min.
51     */
52    protected final Expression<Long> min;
53    /**
54     * The Max.
55     */
56    protected final Expression<Long> max;
57    /**
58     * The Granularity.
59     */
60    protected final Expression<Long> granularity;
61    /**
62     * The Distribution.
63     */
64    protected final Expression<? extends Distribution> distribution;
65  
66    // constructors ----------------------------------------------------------------------------------------------------
67  
68    /**
69     * Instantiates a new Dynamic long generator.
70     */
71    public DynamicLongGenerator() {
72      this(ExpressionUtil.constant(0L), ExpressionUtil.constant(30L),
73          ExpressionUtil.constant(1L), ExpressionUtil.constant(SequenceManager.RANDOM_SEQUENCE),
74          ExpressionUtil.constant(false));
75    }
76  
77    /**
78     * Instantiates a new Dynamic long generator.
79     *
80     * @param min          the min
81     * @param max          the max
82     * @param granularity  the granularity
83     * @param distribution the distribution
84     * @param unique       the unique
85     */
86    public DynamicLongGenerator(Expression<Long> min, Expression<Long> max,
87                                Expression<Long> granularity, Expression<? extends Distribution> distribution,
88                                Expression<Boolean> unique) {
89      super(Long.class);
90      this.min = min;
91      this.max = max;
92      this.granularity = granularity;
93      this.distribution = distribution;
94    }
95  
96    // Generator interface ---------------------------------------------------------------------------------------------
97  
98    /**
99     * ensures consistency of the state
100    */
101   @Override
102   public void init(GeneratorContext context) {
103     assertNotInitialized();
104     this.context = context;
105     resetMembers(min.evaluate(context), max.evaluate(context));
106     super.init(context);
107   }
108 
109   @Override
110   public void reset() {
111     assertInitialized();
112     resetMembers(min.evaluate(context), max.evaluate(context));
113     super.reset();
114   }
115 
116   /**
117    * Reset members.
118    *
119    * @param minValue the min value
120    * @param maxValue the max value
121    */
122   protected void resetMembers(Long minValue, Long maxValue) {
123     if (minValue == null) {
124       minValue = 0L;
125     }
126     Long granularityValue = ExpressionUtil.evaluate(granularity, context);
127     if (granularityValue == null) {
128       granularityValue = 1L;
129     }
130     Distribution dist = distribution.evaluate(context);
131     Generator<Long> source = dist.createNumberGenerator(Long.class, minValue, maxValue, granularityValue, false);
132     source.init(context);
133     setSource(source);
134   }
135 
136 }