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;
28
29 import com.rapiddweller.benerator.GeneratorContext;
30 import com.rapiddweller.benerator.distribution.Distribution;
31 import com.rapiddweller.benerator.wrapper.LengthGenerator;
32 import com.rapiddweller.benerator.wrapper.ProductWrapper;
33 import com.rapiddweller.format.regex.RegexParser;
34
35 import java.util.Locale;
36 import java.util.Set;
37
38
39
40
41
42
43
44
45 public class RandomVarLengthStringGenerator extends LengthGenerator<Character, String>
46 implements VarLengthStringGenerator {
47
48 private String pattern;
49 private Locale locale;
50 private Set<Character> chars;
51
52
53
54
55 public RandomVarLengthStringGenerator() {
56 this("[0-9]", 8);
57 }
58
59
60
61
62
63
64
65 public RandomVarLengthStringGenerator(String pattern, int length) {
66 this(pattern, length, length, 1);
67 }
68
69
70
71
72
73
74
75
76
77 public RandomVarLengthStringGenerator(String pattern, int minLength, int maxLength, int lengthGranularity) {
78 this(pattern, minLength, maxLength, lengthGranularity, null);
79 }
80
81
82
83
84
85
86
87
88
89
90 public RandomVarLengthStringGenerator(String pattern, int minLength, int maxLength,
91 int lengthGranularity, Distribution lengthDistribution) {
92 super(null, true, minLength, maxLength, lengthGranularity, lengthDistribution);
93 this.pattern = pattern;
94 }
95
96
97
98
99
100
101
102
103
104
105 public RandomVarLengthStringGenerator(Set<Character> chars, int minLength, int maxLength,
106 int lengthGranularity, Distribution lengthDistribution) {
107 super(null, true, minLength, maxLength, lengthGranularity, lengthDistribution);
108 this.chars = chars;
109 }
110
111
112
113
114
115
116
117
118
119 public String getPattern() {
120 return pattern;
121 }
122
123
124
125
126
127
128 public void setPattern(String charSet) {
129 this.pattern = charSet;
130 this.chars = null;
131 }
132
133
134
135
136
137
138 public Locale getLocale() {
139 return locale;
140 }
141
142
143
144
145
146
147 public void setLocale(Locale locale) {
148 this.locale = locale;
149 }
150
151
152
153
154
155
156 public Set<Character> getChars() {
157 return chars;
158 }
159
160
161
162
163
164
165 public void setChars(Set<Character> chars) {
166 this.chars = chars;
167 this.pattern = null;
168 }
169
170
171
172
173 @Override
174 public Class<String> getGeneratedType() {
175 return String.class;
176 }
177
178 @Override
179 public boolean isThreadSafe() {
180 return true;
181 }
182
183 @Override
184 public boolean isParallelizable() {
185 return true;
186 }
187
188 @Override
189 public void init(GeneratorContext context) {
190 if (pattern != null) {
191 this.chars = new RegexParser(locale).parseSingleChar(pattern).getCharSet().getSet();
192 }
193 setSource(new CharacterGenerator(chars));
194 super.init(context);
195 }
196
197 @Override
198 public ProductWrapper<String> generate(ProductWrapper<String> wrapper) {
199 return wrapper.wrap(generate());
200 }
201
202 @Override
203 public String generate() {
204 return generateWithLength(generateCardinal());
205 }
206
207 @Override
208 public String generateWithLength(int length) {
209 StringBuilder builder = new StringBuilder();
210 for (int i = 0; i < length; i++) {
211 builder.append(generateFromSource().unwrap());
212 }
213 return builder.toString();
214 }
215
216 @Override
217 public String toString() {
218 return getClass().getSimpleName() + "[chars=" + chars + ", minLength=" + getMinLength() + ", " +
219 "maxLength=" + getMaxLength() + ", lengthGranularity=" + getLengthGranularity() + ", " +
220 "lengthDistribution=" + getLengthDistribution() + "]";
221 }
222
223 }