1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
40
41
42
43
44
45
46
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
55
56
57
58
59 public ExpandSequence() {
60 this(ExpandGeneratorProxy.DEFAULT_DUPLICATION_QUOTA);
61 }
62
63
64
65
66
67
68 public ExpandSequence(Float duplicationQuota) {
69 this(BeneratorOpts.getCacheSize(),
70 duplicationQuota,
71 ExpandGeneratorProxy.defaultBucketSize(BeneratorOpts.getCacheSize()));
72 }
73
74
75
76
77
78
79
80 public ExpandSequence(Integer cacheSize, Integer bucketSize) {
81 this(cacheSize, ExpandGeneratorProxy.DEFAULT_DUPLICATION_QUOTA, bucketSize);
82 }
83
84
85
86
87
88
89
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
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
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 }