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.InvalidGeneratorSetupException;
31 import com.rapiddweller.benerator.NonNullGenerator;
32 import com.rapiddweller.benerator.distribution.Sequence;
33 import com.rapiddweller.benerator.distribution.SequenceManager;
34 import com.rapiddweller.benerator.wrapper.SkipGeneratorProxy;
35 import com.rapiddweller.benerator.wrapper.WrapperFactory;
36 import com.rapiddweller.common.BeanUtil;
37 import com.rapiddweller.common.NumberUtil;
38
39 import java.math.BigDecimal;
40
41 import static com.rapiddweller.common.NumberUtil.toDouble;
42 import static com.rapiddweller.common.NumberUtil.toInteger;
43 import static com.rapiddweller.common.NumberUtil.toLong;
44
45
46
47
48
49
50
51
52
53 public class StepSequence extends Sequence {
54
55 private BigDecimal delta;
56 private final BigDecimal initial;
57 private final BigDecimal limit;
58
59
60
61
62 public StepSequence() {
63 this(null);
64 }
65
66
67
68
69
70
71
72
73 public StepSequence(BigDecimal delta) {
74 this(delta, null);
75 }
76
77
78
79
80
81
82
83 public StepSequence(BigDecimal delta, BigDecimal initial) {
84 this(delta, initial, null);
85 }
86
87
88
89
90
91
92
93
94 public StepSequence(BigDecimal delta, BigDecimal initial, BigDecimal limit) {
95 this.delta = delta;
96 this.initial = initial;
97 this.limit = limit;
98 }
99
100
101
102
103
104
105 public void setDelta(BigDecimal delta) {
106 this.delta = delta;
107 }
108
109
110
111
112
113
114 public BigDecimal getDelta() {
115 return delta;
116 }
117
118
119
120
121
122
123 public BigDecimal getInitial() {
124 return initial;
125 }
126
127 @Override
128 public <T> Generator<T> applyTo(Generator<T> source, boolean unique) {
129 int deltaToUse = (delta != null ? toInteger(delta) : 1);
130 if (delta != null && delta.longValue() < 0) {
131 return super.applyTo(source, unique);
132 } else {
133 return new SkipGeneratorProxy<>(source, deltaToUse, deltaToUse,
134 SequenceManager.RANDOM_SEQUENCE, toInteger(limit));
135 }
136 }
137
138 @Override
139 public <T extends Number> NonNullGenerator<T> createNumberGenerator(
140 Class<T> numberType, T min, T max, T granularity, boolean unique) {
141 Number deltaToUse = deltaToUse(granularity);
142 if (unique && deltaToUse.doubleValue() == 0) {
143 throw new InvalidGeneratorSetupException("Can't generate unique numbers with an increment of 0.");
144 }
145 NonNullGenerator<? extends Number> base;
146 if (BeanUtil.isIntegralNumberType(numberType)) {
147 if (max == null) {
148 max = NumberUtil.maxValue(numberType);
149 }
150 base = new StepLongGenerator(
151 toLong(min), toLong(max), toLong(deltaToUse), toLong(initial));
152 } else {
153 base = new StepDoubleGenerator(
154 toDouble(min), toDouble(max), toDouble(deltaToUse), toDouble(initial));
155 }
156 return WrapperFactory.asNonNullNumberGeneratorOfType(numberType, base, min, granularity);
157 }
158
159 private <T extends Number> Number deltaToUse(T granularity) {
160 return (delta != null ? delta : (granularity != null ? granularity : 1));
161 }
162
163 @Override
164 public String toString() {
165 return BeanUtil.toString(this);
166 }
167
168 }