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.              When using null, the granularity parameter will be used to set the increment              in {@link #createNumberGenerator(Class, Number, Number, Number, boolean)}
70     */
71    public StepSequence(BigDecimal delta) {
72      this(delta, null);
73    }
74  
75    /**
76     * Instantiates a new Step sequence.
77     *
78     * @param delta   the delta
79     * @param initial the initial
80     */
81    public StepSequence(BigDecimal delta, BigDecimal initial) {
82      this(delta, initial, null);
83    }
84  
85    /**
86     * Instantiates a new Step sequence.
87     *
88     * @param delta   the delta
89     * @param initial the initial
90     * @param limit   the limit
91     */
92    public StepSequence(BigDecimal delta, BigDecimal initial, BigDecimal limit) {
93      this.delta = delta;
94      this.initial = initial;
95      this.limit = limit;
96    }
97  
98    /**
99     * Sets delta.
100    *
101    * @param delta the delta
102    */
103   public void setDelta(BigDecimal delta) {
104     this.delta = delta;
105   }
106 
107   /**
108    * Gets delta.
109    *
110    * @return the delta
111    */
112   public BigDecimal getDelta() {
113     return delta;
114   }
115 
116   /**
117    * Gets initial.
118    *
119    * @return the initial
120    */
121   public BigDecimal getInitial() {
122     return initial;
123   }
124 
125   @Override
126   public <T> Generator<T> applyTo(Generator<T> source, boolean unique) {
127     int deltaToUse = (delta != null ? toInteger(delta) : 1);
128     if (delta != null && delta.longValue() < 0) {
129       return super.applyTo(source, unique);
130     } else {
131       return new SkipGeneratorProxy<>(source, deltaToUse, deltaToUse,
132           SequenceManager.RANDOM_SEQUENCE, toInteger(limit));
133     }
134   }
135 
136   @Override
137   public <T extends Number> NonNullGenerator<T> createNumberGenerator(
138       Class<T> numberType, T min, T max, T granularity, boolean unique) {
139     Number deltaToUse = deltaToUse(granularity);
140     if (unique && deltaToUse.doubleValue() == 0) {
141       throw new InvalidGeneratorSetupException("Can't generate unique numbers with an increment of 0.");
142     }
143     NonNullGenerator<? extends Number> base;
144     if (BeanUtil.isIntegralNumberType(numberType)) {
145       if (max == null) {
146         max = NumberUtil.maxValue(numberType);
147       }
148       base = new StepLongGenerator(
149           toLong(min), toLong(max), toLong(deltaToUse), toLong(initial));
150     } else {
151       base = new StepDoubleGenerator(
152           toDouble(min), toDouble(max), toDouble(deltaToUse), toDouble(initial));
153     }
154     return WrapperFactory.asNonNullNumberGeneratorOfType(numberType, base, min, granularity);
155   }
156 
157   private <T extends Number> Number deltaToUse(T granularity) {
158     return (delta != null ? delta : (granularity != null ? granularity : 1));
159   }
160 
161   @Override
162   public String toString() {
163     return BeanUtil.toString(this);
164   }
165 
166 }