View Javadoc

1   /**
2    * Copyright 2008 Bryan Ray
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0 
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
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   * <DL>
24   *   <DT><B>Project:</B></DT>
25   *   <DD>TestDataLoader</DD>
26   *   <DT><B>Filename:</B></DT>
27   *   <DD>StringSequenceGenerator.java</DD>
28   *   <DT><B>Creation Date:</B></DT>
29   *   <DD>31 May 2008</DD>
30   * </DL>
31   * 
32   * @author Bryan Ray
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  	 * @param args A list of two values:
44  	 * <ol>
45  	 * <li>A start value.</li>
46  	 * <li>An end value.</li>
47  	 * </ol>
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  				// TODO need to check that the start value is smaller than the end value.
59  				// TODO handle strings of different lengths or disallow them.
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  	 * {@inheritDoc}
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  	 * A method that supports incrementing characters A-Z.
86  	 * @param value The character string.
87  	 * @param index The character to increment.
88  	 * */
89  	private void incrementDigit(StringBuffer value, int index) {
90  		int code = value.charAt(index);
91  		if(90==code) { // 90 = 'Z'
92  			code = 65; // 65 = 'A'
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 	 * {@inheritDoc}
105 	 */
106 	public String value() {
107 		return currentValue;
108 	}
109 
110 }