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 public StepSequence(BigDecimal delta) {
72 this(delta, null);
73 }
74
75
76
77
78
79
80
81 public StepSequence(BigDecimal delta, BigDecimal initial) {
82 this(delta, initial, null);
83 }
84
85
86
87
88
89
90
91
92 public StepSequence(BigDecimal delta, BigDecimal initial, BigDecimal limit) {
93 this.delta = delta;
94 this.initial = initial;
95 this.limit = limit;
96 }
97
98
99
100
101
102
103 public void setDelta(BigDecimal delta) {
104 this.delta = delta;
105 }
106
107
108
109
110
111
112 public BigDecimal getDelta() {
113 return delta;
114 }
115
116
117
118
119
120
121 public BigDecimal getInitial() {
122 return initial;
123 }
124
125 @Override
126 public <T> Generator<T> applyTo(Generator<T> source, boolean unique) {
127 int deltaToUse = (delta != null ? toInteger(delta) : 1);
128 if (delta != null && delta.longValue() < 0) {
129 return super.applyTo(source, unique);
130 } else {
131 return new SkipGeneratorProxy<>(source, deltaToUse, deltaToUse,
132 SequenceManager.RANDOM_SEQUENCE, toInteger(limit));
133 }
134 }
135
136 @Override
137 public <T extends Number> NonNullGenerator<T> createNumberGenerator(
138 Class<T> numberType, T min, T max, T granularity, boolean unique) {
139 Number deltaToUse = deltaToUse(granularity);
140 if (unique && deltaToUse.doubleValue() == 0) {
141 throw new InvalidGeneratorSetupException("Can't generate unique numbers with an increment of 0.");
142 }
143 NonNullGenerator<? extends Number> base;
144 if (BeanUtil.isIntegralNumberType(numberType)) {
145 if (max == null) {
146 max = NumberUtil.maxValue(numberType);
147 }
148 base = new StepLongGenerator(
149 toLong(min), toLong(max), toLong(deltaToUse), toLong(initial));
150 } else {
151 base = new StepDoubleGenerator(
152 toDouble(min), toDouble(max), toDouble(deltaToUse), toDouble(initial));
153 }
154 return WrapperFactory.asNonNullNumberGeneratorOfType(numberType, base, min, granularity);
155 }
156
157 private <T extends Number> Number deltaToUse(T granularity) {
158 return (delta != null ? delta : (granularity != null ? granularity : 1));
159 }
160
161 @Override
162 public String toString() {
163 return BeanUtil.toString(this);
164 }
165
166 }