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