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.InvalidGeneratorSetupException;
31  import com.rapiddweller.benerator.primitive.number.AbstractNonNullNumberGenerator;
32  
33  /**
34   * Double Generator that implements a 'shuffle' Double Sequence.<br/>
35   * <br/>
36   * Created: 18.06.2006 14:40:29
37   *
38   * @author Volker Bergmann
39   * @since 0.1
40   */
41  public class ShuffleDoubleGenerator extends AbstractNonNullNumberGenerator<Double> {
42  
43    private double increment;
44    private Double next;
45  
46    /**
47     * Instantiates a new Shuffle double generator.
48     */
49    public ShuffleDoubleGenerator() {
50      this(Double.MIN_VALUE, Double.MAX_VALUE, 2, 1);
51    }
52  
53    /**
54     * Instantiates a new Shuffle double generator.
55     *
56     * @param min         the min
57     * @param max         the max
58     * @param granularity the granularity
59     * @param increment   the increment
60     */
61    public ShuffleDoubleGenerator(double min, double max, double granularity, double increment) {
62      super(Double.class, min, max, granularity);
63      this.increment = increment;
64      reset();
65    }
66  
67    // config properties -----------------------------------------------------------------------------------------------
68  
69    @Override
70    public void setMin(Double min) {
71      super.setMin(min);
72      reset();
73    }
74  
75    /**
76     * Gets increment.
77     *
78     * @return the increment
79     */
80    public double getIncrement() {
81      return increment;
82    }
83  
84    /**
85     * Sets increment.
86     *
87     * @param increment the increment
88     */
89    public void setIncrement(double increment) {
90      this.increment = increment;
91    }
92  
93    // source interface ---------------------------------------------------------------------------------------------
94  
95    @Override
96    public void init(GeneratorContext context) throws InvalidGeneratorSetupException {
97      if (granularity <= 0) {
98        throw new InvalidGeneratorSetupException("Granularity must be greater than zero, but is " + granularity);
99      }
100     if (min < max && increment <= 0) {
101       throw new InvalidGeneratorSetupException("Unsupported increment value: " + increment);
102     }
103     next = min;
104     super.init(context);
105   }
106 
107   @Override
108   public synchronized Double generate() {
109     assertInitialized();
110     if (next == null) {
111       return null;
112     }
113     double result = next;
114     if (next + increment <= max) {
115       next += increment;
116     } else {
117       double newOffset = (next - min + granularity) % increment;
118       next = (newOffset > 0 ? min + newOffset : null);
119     }
120     return result;
121   }
122 
123   @Override
124   public synchronized void reset() {
125     this.next = min;
126   }
127 
128 }