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.distribution.Distribution;
31  import com.rapiddweller.benerator.sample.ConstantGenerator;
32  import com.rapiddweller.script.Expression;
33  
34  /**
35   * Behaves similar to the {@link DynamicLongGenerator},
36   * but generates <code>maxFallback</code> values, if <code>max</code> is set to <code>null</code>.<br/><br/>
37   * Created: 28.03.2010 08:48:11
38   *
39   * @author Volker Bergmann
40   * @since 0.6.0
41   */
42  public class DynamicCountGenerator extends DynamicLongGenerator {
43  
44    private boolean resetToMin;
45  
46    /**
47     * Instantiates a new Dynamic count generator.
48     */
49    public DynamicCountGenerator() {
50      super();
51    }
52  
53    /**
54     * Instantiates a new Dynamic count generator.
55     *
56     * @param min          the min
57     * @param max          the max
58     * @param granularity  the granularity
59     * @param distribution the distribution
60     * @param unique       the unique
61     * @param resetToMin   the reset to min
62     */
63    public DynamicCountGenerator(Expression<Long> min, Expression<Long> max, Expression<Long> granularity,
64                                 Expression<? extends Distribution> distribution, Expression<Boolean> unique, boolean resetToMin) {
65      super(min, max, granularity, distribution, unique);
66      this.resetToMin = resetToMin;
67    }
68  
69    @SuppressWarnings({"rawtypes", "unchecked"})
70    @Override
71    protected void resetMembers(Long minValue, Long maxValue) {
72      if (maxValue != null) {
73        super.resetMembers(minValue, maxValue);
74      } else {
75        // if it is not required to reset to min (<generate> or <iterate>), make it unlimited (returning null),
76        // otherwise reset to the min value (component generation)
77        Long constant = (resetToMin ? minValue : null);
78        Generator<Long> source = new ConstantGenerator(constant);
79        source.init(context);
80        setSource(source);
81      }
82    }
83  
84  }