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.distribution.sequence;
28  
29  import com.rapiddweller.benerator.Generator;
30  import com.rapiddweller.benerator.InvalidGeneratorSetupException;
31  import com.rapiddweller.benerator.NonNullGenerator;
32  import com.rapiddweller.benerator.distribution.Sequence;
33  import com.rapiddweller.benerator.distribution.SequenceManager;
34  import com.rapiddweller.benerator.wrapper.SkipGeneratorProxy;
35  import com.rapiddweller.benerator.wrapper.WrapperFactory;
36  import com.rapiddweller.common.BeanUtil;
37  import com.rapiddweller.common.NumberUtil;
38  
39  import java.math.BigDecimal;
40  
41  import static com.rapiddweller.common.NumberUtil.toDouble;
42  import static com.rapiddweller.common.NumberUtil.toInteger;
43  import static com.rapiddweller.common.NumberUtil.toLong;
44  
45  /**
46   * Creates numbers by continuously incrementing a base value by a constant amount.<br/>
47   * <br/>
48   * Created at 30.06.2009 09:55:20
49   *
50   * @author Volker Bergmann
51   * @since 0.6.0
52   */
53  public class StepSequence extends Sequence {
54  
55    private BigDecimal delta;
56    private final BigDecimal initial;
57    private final BigDecimal limit;
58  
59    /**
60     * Instantiates a new Step sequence.
61     */
62    public StepSequence() {
63      this(null); // when using null, the granularity parameter will be used to set the increment in createGenerator
64    }
65  
66    /**
67     * Instantiates a new Step sequence.
68     *
69     * @param delta the increment to choose for created generators.
70     *              When using null, the granularity parameter will be used to set the increment
71     *              in {@link #createNumberGenerator(Class, Number, Number, Number, boolean)}
72     */
73    public StepSequence(BigDecimal delta) {
74      this(delta, null);
75    }
76  
77    /**
78     * Instantiates a new Step sequence.
79     *
80     * @param delta   the delta
81     * @param initial the initial
82     */
83    public StepSequence(BigDecimal delta, BigDecimal initial) {
84      this(delta, initial, null);
85    }
86  
87    /**
88     * Instantiates a new Step sequence.
89     *
90     * @param delta   the delta
91     * @param initial the initial
92     * @param limit   the limit
93     */
94    public StepSequence(BigDecimal delta, BigDecimal initial, BigDecimal limit) {
95      this.delta = delta;
96      this.initial = initial;
97      this.limit = limit;
98    }
99  
100   /**
101    * Sets delta.
102    *
103    * @param delta the delta
104    */
105   public void setDelta(BigDecimal delta) {
106     this.delta = delta;
107   }
108 
109   /**
110    * Gets delta.
111    *
112    * @return the delta
113    */
114   public BigDecimal getDelta() {
115     return delta;
116   }
117 
118   /**
119    * Gets initial.
120    *
121    * @return the initial
122    */
123   public BigDecimal getInitial() {
124     return initial;
125   }
126 
127   @Override
128   public <T> Generator<T> applyTo(Generator<T> source, boolean unique) {
129     int deltaToUse = (delta != null ? toInteger(delta) : 1);
130     if (delta != null && delta.longValue() < 0) {
131       return super.applyTo(source, unique);
132     } else {
133       return new SkipGeneratorProxy<>(source, deltaToUse, deltaToUse,
134           SequenceManager.RANDOM_SEQUENCE, toInteger(limit));
135     }
136   }
137 
138   @Override
139   public <T extends Number> NonNullGenerator<T> createNumberGenerator(
140       Class<T> numberType, T min, T max, T granularity, boolean unique) {
141     Number deltaToUse = deltaToUse(granularity);
142     if (unique && deltaToUse.doubleValue() == 0) {
143       throw new InvalidGeneratorSetupException("Can't generate unique numbers with an increment of 0.");
144     }
145     NonNullGenerator<? extends Number> base;
146     if (BeanUtil.isIntegralNumberType(numberType)) {
147       if (max == null) {
148         max = NumberUtil.maxValue(numberType);
149       }
150       base = new StepLongGenerator(
151           toLong(min), toLong(max), toLong(deltaToUse), toLong(initial));
152     } else {
153       base = new StepDoubleGenerator(
154           toDouble(min), toDouble(max), toDouble(deltaToUse), toDouble(initial));
155     }
156     return WrapperFactory.asNonNullNumberGeneratorOfType(numberType, base, min, granularity);
157   }
158 
159   private <T extends Number> Number deltaToUse(T granularity) {
160     return (delta != null ? delta : (granularity != null ? granularity : 1));
161   }
162 
163   @Override
164   public String toString() {
165     return BeanUtil.toString(this);
166   }
167 
168 }