001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */ 
017    package org.apache.commons.betwixt.io.id;
018    
019    
020    /** <p>Generates <code>ID</code>'s in numeric sequence.
021      * A simple counter is used.
022      * Every time that {@link #nextIdImpl} is called, 
023      * this counter is incremented.</p>
024      *
025      * <p>By default, the counter starts at zero.
026      * A user can set the initial value by using the 
027      * {@link #SequentialIDGenerator(int start)} constructor.</p>
028      *
029      * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
030      * @version $Revision: 438373 $
031      */
032    public final class SequentialIDGenerator extends AbstractIDGenerator {
033        
034        /** Counter used to assign <code>ID</code>'s */
035        private int counter = 0;
036            
037        /** 
038          * Base constructor.
039          * Counter starts at zero.
040          */
041        public SequentialIDGenerator() {} 
042        
043        /** 
044         * Constructor sets the start value for the counter.
045         * 
046         * <p><strong>Note</strong> since the counter increments
047         * before returning the next value, 
048         * first <code>ID</code> generated will be <em>one more</em>
049         * than the given <code>start</code> parameter.</p>
050         * 
051         * @param start start the counting at this value
052         */
053        public SequentialIDGenerator(int start) {
054            this.counter = start;
055        }
056        
057        /** 
058          * Increment counter and then return value.
059          *
060          * @return one more than the current counter (converted to a string)
061          */
062        public String nextIdImpl() {
063            return Integer.toString(++counter);
064        }
065        
066        /** 
067          * Gets the current counter value 
068          *
069          * @return the last ID in the sequence
070          */
071        public int getCount() {
072            return counter;
073        }
074    }