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    import org.apache.commons.betwixt.io.IDGenerator;
020    
021    /** <p>Abstract superclass for {@link IDGenerator} implementations.</p>
022      *
023      * <p>It implements the entire <code>IDGenerator</code> interface.
024      * When <code>nextId</code> is called, 
025      * this class sets the <code>LastId</code> property (as well
026      * as returning the value).
027      * Subclasses should override {@link #nextIdImpl}.</p>
028      *
029      * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
030      * @version $Revision: 438373 $
031      */
032    public abstract class AbstractIDGenerator implements IDGenerator {
033        
034        /** Last <code>ID</code> returned */
035        private String lastId = "0";
036        
037        /** 
038         * Gets last <code>ID</code> returned. 
039         *
040         * @return the last id created by the generated
041         */
042        public final String getLastId() {
043            return lastId;
044        }
045        
046        /** 
047          * <p>Generate next <code>ID</code>.</p>
048          *
049          * <p>This method obtains the next <code>ID</code> from subclass
050          * and then uses this to set the <code>LastId</code> property.</p>
051          *
052          * @return the next id generated 
053          */
054        public final String nextId() {
055            lastId = nextIdImpl();
056            return lastId;
057        }
058        
059        /** 
060          * Subclasses should <strong>provide an implementation</strong> for this method.
061          * This implementation needs only provide the next <code>ID</code>
062          * value (according to it's algorithm).
063          * Setting the <code>LastId</code> property can be left to this class.
064          *
065          * @return the next id generated
066          */
067        protected abstract String nextIdImpl();
068    }