01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.commons.betwixt.io.id;
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: 438373 $
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: /**
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: }
|