1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.io.id;
18
19
20 /*** <p>Generates <code>ID</code>'s in numeric sequence.
21 * A simple counter is used.
22 * Every time that {@link #nextIdImpl} is called,
23 * this counter is incremented.</p>
24 *
25 * <p>By default, the counter starts at zero.
26 * A user can set the initial value by using the
27 * {@link #SequentialIDGenerator(int start)} constructor.</p>
28 *
29 * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
30 * @version $Revision: 438373 $
31 */
32 public final class SequentialIDGenerator extends AbstractIDGenerator {
33
34 /*** Counter used to assign <code>ID</code>'s */
35 private int counter = 0;
36
37 /***
38 * Base constructor.
39 * Counter starts at zero.
40 */
41 public SequentialIDGenerator() {}
42
43 /***
44 * Constructor sets the start value for the counter.
45 *
46 * <p><strong>Note</strong> since the counter increments
47 * before returning the next value,
48 * first <code>ID</code> generated will be <em>one more</em>
49 * than the given <code>start</code> parameter.</p>
50 *
51 * @param start start the counting at this value
52 */
53 public SequentialIDGenerator(int start) {
54 this.counter = start;
55 }
56
57 /***
58 * Increment counter and then return value.
59 *
60 * @return one more than the current counter (converted to a string)
61 */
62 public String nextIdImpl() {
63 return Integer.toString(++counter);
64 }
65
66 /***
67 * Gets the current counter value
68 *
69 * @return the last ID in the sequence
70 */
71 public int getCount() {
72 return counter;
73 }
74 }