1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package uk.org.bryanray.testtoys.generator.sequencegenerators;
17
18 import java.util.List;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public class StringSequenceGenerator implements SequenceGenerator {
35
36 private String startValue;
37 private String endValue;
38 private String currentValue;
39
40 private static final Pattern PARAMETER_PATTERN = Pattern.compile("^[A-Z]+$");
41
42
43
44
45
46
47
48
49 public StringSequenceGenerator(List<String> args) {
50 if(2==args.size()) {
51 startValue = args.get(0);
52 endValue = args.get(1);
53 currentValue = startValue;
54 Matcher startMatcher = PARAMETER_PATTERN.matcher(startValue);
55 Matcher endMatcher = PARAMETER_PATTERN.matcher(endValue);
56 if(startMatcher.matches() && endMatcher.matches()) {
57 return;
58
59
60 }
61 else {
62 throw new IllegalArgumentException("Illegal characters in start or end value.");
63 }
64 }
65 else {
66 throw new IllegalArgumentException("Needs a start value, and an end value.");
67 }
68 }
69
70
71
72
73 public void next() {
74 if(endValue.equals(currentValue)) {
75 currentValue = startValue;
76 }
77 else {
78 StringBuffer stringBuffer = new StringBuffer(currentValue);
79 incrementDigit(stringBuffer, stringBuffer.length()-1);
80 currentValue = stringBuffer.toString();
81 }
82 }
83
84
85
86
87
88
89 private void incrementDigit(StringBuffer value, int index) {
90 int code = value.charAt(index);
91 if(90==code) {
92 code = 65;
93 if(0<index) {
94 incrementDigit(value, index - 1);
95 }
96 else {
97 value.insert(0, 'A');
98 }
99 }
100 value.setCharAt(index, (char)(code+1));
101 }
102
103
104
105
106 public String value() {
107 return currentValue;
108 }
109
110 }