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.Generator;
30 import com.rapiddweller.benerator.GeneratorContext;
31 import com.rapiddweller.benerator.wrapper.MultiGeneratorWrapper;
32 import com.rapiddweller.benerator.wrapper.ProductWrapper;
33 import com.rapiddweller.common.CharSet;
34
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Set;
38
39
40
41
42
43
44
45
46 public class UniqueScrambledStringGenerator extends MultiGeneratorWrapper<String, String> implements VarLengthStringGenerator {
47
48 private int minLength;
49 private int maxLength;
50 private final Set<Character> chars;
51
52
53
54
55
56
57 public UniqueScrambledStringGenerator() {
58 this(new CharSet('A', 'Z').getSet(), 4, 8);
59 }
60
61
62
63
64
65
66
67
68 public UniqueScrambledStringGenerator(Set<Character> chars, int minLength, int maxLength) {
69 super(String.class);
70 this.minLength = minLength;
71 this.maxLength = maxLength;
72 this.chars = chars;
73 }
74
75
76
77
78
79
80
81
82 public int getMinLength() {
83 return minLength;
84 }
85
86
87
88
89
90
91 public void setMinLength(int minLength) {
92 this.minLength = minLength;
93 }
94
95
96
97
98
99
100 public int getMaxLength() {
101 return maxLength;
102 }
103
104
105
106
107
108
109 public void setMaxLength(int maxLength) {
110 this.maxLength = maxLength;
111 }
112
113
114
115 @Override
116 public void init(GeneratorContext context) {
117 assertNotInitialized();
118
119 List<Generator<? extends String>> subGens = new ArrayList<>(maxLength - minLength + 1);
120 for (int i = minLength; i <= maxLength; i++) {
121 subGens.add(new UniqueFixedLengthStringGenerator(chars, i, false));
122 }
123 setSources(subGens);
124 super.init(context);
125 }
126
127 @Override
128 public String generateWithLength(int length) {
129 ProductWrapper<String> wrapper = generateFromSource(length - minLength, getSourceWrapper());
130 return (wrapper != null ? wrapper.unwrap() : null);
131 }
132
133 @Override
134 public ProductWrapper<String> generate(ProductWrapper<String> wrapper) {
135 assertInitialized();
136 return generateFromRandomSource(wrapper);
137 }
138
139 @Override
140 public String generate() {
141 ProductWrapper<String> wrapper = generate(getResultWrapper());
142 return (wrapper != null ? wrapper.unwrap() : null);
143 }
144
145
146
147 @Override
148 public String toString() {
149 return getClass().getSimpleName() + '[' + minLength + "<=length<=" + maxLength + ", " +
150 "charSet=" + chars + "]";
151 }
152
153 }