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
20 /**
21 * <DL>
22 * <DT><B>Project:</B></DT>
23 * <DD>TestDataLoader</DD>
24 * <DT><B>Filename:</B></DT>
25 * <DD>NumberSequenceGenerator.java</DD>
26 * <DT><B>Creation Date:</B></DT>
27 * <DD>25 May 2008</DD>
28 * </DL>
29 *
30 * A class that generates an incrementing sequence of numbers. This
31 * implementation will loop back to the start value when the end value is
32 * reached.
33 *
34 * @author Bryan Ray
35 */
36 public class NumberSequenceGenerator implements SequenceGenerator {
37
38 // TODO this class could have a value by which to increment the sequence.
39
40 private int startValue;
41 private int endValue;
42 private int currentValue;
43
44 /**
45 * @param args
46 * A {@link List} of two values:
47 * <ol>
48 * <li>The start value.</li>
49 * <li>The end value.</li>
50 * </ol>
51 */
52 public NumberSequenceGenerator(List<String> args) {
53 if (2 == args.size()) {
54 int startValue = Integer.parseInt(args.get(0));
55 int endValue = Integer.parseInt(args.get(1));
56 if (startValue <= endValue) {
57 this.startValue = startValue;
58 this.endValue = endValue;
59 this.currentValue = startValue;
60 } else {
61 throw new IllegalArgumentException(
62 "End value is larger than the start value.");
63 }
64 } else {
65 throw new IllegalArgumentException(
66 "Incorrect arguments: startValue, endValue.");
67 }
68 }
69
70 /**
71 * {@inheritDoc}
72 */
73 public void next() {
74 currentValue++;
75 if (currentValue > endValue) {
76 currentValue = startValue;
77 }
78 }
79
80 /**
81 * {@inheritDoc}
82 */
83 public String value() {
84 return Integer.toString(currentValue);
85 }
86 }