View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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  
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  }