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.primitive.number;
28
29 import com.rapiddweller.benerator.IllegalGeneratorStateException;
30 import com.rapiddweller.benerator.distribution.Distribution;
31 import com.rapiddweller.benerator.distribution.SequenceManager;
32 import com.rapiddweller.benerator.util.GeneratingConverter;
33 import com.rapiddweller.benerator.wrapper.ProductWrapper;
34 import com.rapiddweller.common.Converter;
35 import com.rapiddweller.common.converter.ConverterManager;
36 import com.rapiddweller.model.data.Uniqueness;
37 import com.rapiddweller.script.math.ArithmeticEngine;
38
39
40
41
42
43
44
45
46
47 public class NoiseInducer extends GeneratingConverter<Number, Number, Number> {
48
49 private double minNoise;
50 private double maxNoise;
51 private double noiseGranularity;
52 private Distribution noiseDistribution;
53 private boolean relative;
54
55 private Class<? extends Number> numberType;
56 private ArithmeticEngine arithmetic;
57
58
59
60
61 public NoiseInducer() {
62 this(-0.1, 0.1, 0.001);
63 }
64
65
66
67
68
69
70
71
72 public NoiseInducer(double minNoise, double maxNoise, double noiseGranularity) {
73 super(Number.class, Number.class, null);
74 this.minNoise = minNoise;
75 this.maxNoise = maxNoise;
76 this.noiseGranularity = noiseGranularity;
77 this.noiseDistribution = SequenceManager.CUMULATED_SEQUENCE;
78 this.relative = true;
79 }
80
81
82
83
84
85
86
87
88 public double getMinNoise() {
89 return minNoise;
90 }
91
92
93
94
95
96
97 public void setMinNoise(double minNoise) {
98 this.minNoise = minNoise;
99 }
100
101
102
103
104
105
106 public double getMaxNoise() {
107 return maxNoise;
108 }
109
110
111
112
113
114
115 public void setMaxNoise(double maxNoise) {
116 this.maxNoise = maxNoise;
117 }
118
119
120
121
122
123
124 public double getNoiseGranularity() {
125 return noiseGranularity;
126 }
127
128
129
130
131
132
133 public void setNoiseGranularity(double noiseGranularity) {
134 this.noiseGranularity = noiseGranularity;
135 }
136
137
138
139
140
141
142 public Distribution getNoiseDistribution() {
143 return noiseDistribution;
144 }
145
146
147
148
149
150
151 public void setNoiseDistribution(Distribution noiseDistribution) {
152 this.noiseDistribution = noiseDistribution;
153 }
154
155
156
157
158
159
160 public boolean isRelative() {
161 return relative;
162 }
163
164
165
166
167
168
169 public void setRelative(boolean relative) {
170 this.relative = relative;
171 }
172
173
174
175 @Override
176 protected Number doConvert(Number sourceValue) {
177 if (sourceValue == null) {
178 return null;
179 }
180 if (numberType == null) {
181 initialize(sourceValue);
182 }
183 ProductWrapper<Number> wrapper = generate();
184 if (wrapper == null) {
185 throw new IllegalGeneratorStateException("Noise generator unavailable: " + generator);
186 }
187 Number delta = wrapper.unwrap();
188 Number result;
189 if (relative) {
190 result = (Number) arithmetic.multiply(sourceValue, arithmetic.subtract(1, delta));
191 } else {
192 result = (Number) arithmetic.add(sourceValue, delta);
193 }
194 return result;
195 }
196
197
198
199
200
201
202
203
204
205 public Number convert(Number sourceValue, Number minValue, Number maxValue) {
206 if (sourceValue == null) {
207 return null;
208 }
209 Number result = convert(sourceValue);
210 double rd = result.doubleValue();
211 if (rd < minValue.doubleValue()) {
212 return minValue;
213 }
214 if (rd > maxValue.doubleValue()) {
215 return maxValue;
216 }
217 return result;
218 }
219
220
221
222 @Override
223 @SuppressWarnings("unchecked")
224 protected void initialize(Number sourceValue) {
225 this.numberType = (relative ? Double.class : sourceValue.getClass());
226 Converter<Number, ? extends Number> converter = ConverterManager.getInstance().createConverter(Number.class, numberType);
227 arithmetic = new ArithmeticEngine();
228 generator = context.getGeneratorFactory().createNumberGenerator(
229 (Class<Number>) numberType,
230 converter.convert(minNoise), true,
231 converter.convert(maxNoise), true,
232 converter.convert(noiseGranularity),
233 noiseDistribution, Uniqueness.NONE);
234 super.initialize(sourceValue);
235 }
236
237 }