KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > io > id > SequentialIDGenerator


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
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 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: 1.7 $
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 JavaDoc 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 }
74
Popular Tags