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.NonNullGenerator;
31  import com.rapiddweller.benerator.distribution.Sequence;
32  import com.rapiddweller.benerator.distribution.SequenceManager;
33  import com.rapiddweller.benerator.engine.BeneratorOpts;
34  import com.rapiddweller.benerator.wrapper.WrapperFactory;
35  
36  import static com.rapiddweller.common.NumberUtil.toLong;
37  
38  /**
39   * {@link Sequence} implementation that makes use of Benerator's {@link ExpandGeneratorProxy}
40   * for distributing data of unlimited volume in a unique or non-unique manner.<br/>
41   * <br/>
42   * Created: 13.12.2009 08:59:34
43   *
44   * @author Volker Bergmann
45   * @see ExpandGeneratorProxy
46   * @since 0.6.0
47   */
48  public class ExpandSequence extends Sequence {
49  
50    private final Integer cacheSize;
51    private final Integer bucketSize;
52    private final Float duplicationQuota;
53  
54    // construction ----------------------------------------------------------------------------------------------------
55  
56    /**
57     * Instantiates a new Expand sequence.
58     */
59    public ExpandSequence() {
60      this(ExpandGeneratorProxy.DEFAULT_DUPLICATION_QUOTA);
61    }
62  
63    /**
64     * Instantiates a new Expand sequence.
65     *
66     * @param duplicationQuota the duplication quota
67     */
68    public ExpandSequence(Float duplicationQuota) {
69      this(BeneratorOpts.getCacheSize(),
70          duplicationQuota,
71          ExpandGeneratorProxy.defaultBucketSize(BeneratorOpts.getCacheSize()));
72    }
73  
74    /**
75     * Instantiates a new Expand sequence.
76     *
77     * @param cacheSize  the cache size
78     * @param bucketSize the bucket size
79     */
80    public ExpandSequence(Integer cacheSize, Integer bucketSize) {
81      this(cacheSize, ExpandGeneratorProxy.DEFAULT_DUPLICATION_QUOTA, bucketSize);
82    }
83  
84    /**
85     * Instantiates a new Expand sequence.
86     *
87     * @param cacheSize        the cache size
88     * @param duplicationQuota the duplication quota
89     * @param bucketSize       the bucket size
90     */
91    public ExpandSequence(Integer cacheSize, Float duplicationQuota, Integer bucketSize) {
92      this.cacheSize = cacheSize;
93      this.duplicationQuota = duplicationQuota;
94      this.bucketSize = bucketSize;
95    }
96  
97    // Distribution interface implementation ---------------------------------------------------------------------------
98  
99    @Override
100   public <T extends Number> NonNullGenerator<T> createNumberGenerator(
101       Class<T> numberType, T min, T max, T granularity, boolean unique) {
102     NonNullGenerator<T> source = SequenceManager.STEP_SEQUENCE.createNumberGenerator(numberType, min, max, granularity, unique);
103     int cacheSize = cacheSize(min, max, granularity);
104     return WrapperFactory.asNonNullGenerator(
105         new ExpandGeneratorProxy<>(source, duplicationQuota(unique), cacheSize, bucketSize(cacheSize)));
106   }
107 
108   @Override
109   public <T> Generator<T> applyTo(Generator<T> source, boolean unique) {
110     int cacheSize = cacheSize();
111     return new ExpandGeneratorProxy<>(source, duplicationQuota(unique), cacheSize, bucketSize(cacheSize));
112   }
113 
114   // helpers ---------------------------------------------------------------------------------------------------------
115 
116   private float duplicationQuota(boolean unique) {
117     if (unique) {
118       return 0;
119     }
120     return (duplicationQuota != null ? duplicationQuota : ExpandGeneratorProxy.DEFAULT_DUPLICATION_QUOTA);
121   }
122 
123   private int bucketSize(int cacheSize) {
124     return (bucketSize != null ? bucketSize : ExpandGeneratorProxy.defaultBucketSize(cacheSize));
125   }
126 
127   private int cacheSize() {
128     return (cacheSize != null ? cacheSize : BeneratorOpts.getCacheSize());
129   }
130 
131   private <T extends Number> int cacheSize(T min, T max, T granularity) {
132     if (cacheSize != null) {
133       return cacheSize;
134     }
135     long volume = volume(toLong(min), toLong(max), toLong(granularity));
136     return (int) Math.min(BeneratorOpts.getCacheSize(), volume);
137   }
138 
139   private static <T extends Number> long volume(long min, long max, long granularity) {
140     return (max - min + granularity - 1) / granularity;
141   }
142 
143 }