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.GeneratorContext;
30  import com.rapiddweller.benerator.primitive.number.AbstractNonNullNumberGenerator;
31  
32  /**
33   * Double Generator that implements a 'step' Double Sequence.<br/>
34   * <br/>
35   * Created: 26.07.2007 18:36:45
36   *
37   * @author Volker Bergmann
38   */
39  public class StepDoubleGenerator extends AbstractNonNullNumberGenerator<Double> {
40  
41    private final double increment;
42    private final double initial;
43  
44    private double next;
45  
46    // constructors ----------------------------------------------------------------------------------------------------
47  
48    /**
49     * Instantiates a new Step double generator.
50     */
51    public StepDoubleGenerator() {
52      this(Double.MIN_VALUE, Double.MAX_VALUE);
53    }
54  
55    /**
56     * Instantiates a new Step double generator.
57     *
58     * @param min the min
59     * @param max the max
60     */
61    public StepDoubleGenerator(double min, double max) {
62      this(min, max, 1., null);
63    }
64  
65    /**
66     * Instantiates a new Step double generator.
67     *
68     * @param min       the min
69     * @param max       the max
70     * @param increment the increment
71     */
72    public StepDoubleGenerator(double min, double max, double increment) {
73      this(min, max, increment, null);
74    }
75  
76    /**
77     * Instantiates a new Step double generator.
78     *
79     * @param min       the min
80     * @param max       the max
81     * @param increment the increment
82     * @param initial   the initial
83     */
84    public StepDoubleGenerator(double min, Double max, double increment, Double initial) {
85      super(Double.class, min, max, Math.abs(increment));
86      this.increment = increment;
87      this.initial = (initial != null ? initial : (increment >= 0 ? min : max));
88      reset();
89    }
90  
91    // properties ------------------------------------------------------------------------------------------------------
92  
93    @Override
94    public void init(GeneratorContext context) {
95      assertNotInitialized();
96      resetMembers();
97      super.init(context);
98    }
99  
100   @Override
101   public Double generate() {
102     assertInitialized();
103     if (increment == 0 || (increment > 0 && (max == null || next <= max)) || (increment < 0 && next >= min)) {
104       double value = next;
105       next += increment;
106       return value;
107     } else {
108       return null;
109     }
110   }
111 
112   @Override
113   public synchronized void reset() {
114     resetMembers();
115   }
116 
117   private void resetMembers() {
118     next = initial;
119   }
120 
121 }