Subversion Repositories SmartDukaan

Rev

Rev 10 | Rev 22 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.metamodel.util;

import in.shop2020.metamodel.definitions.Unit;

import java.io.Serializable;
import java.util.Map;

/**
 * @author naveen
 * 
 * Singleton
 */
public class SequenceGenerator implements Serializable {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        public static final int ENTITY = 0;

        /**
         * 
         * @return      SequenceGenerator       instance of SequenceGenerator
         * @throws Exception 
         */
        public static final SequenceGenerator getInstance() throws Exception {
                // De-serialize
                String dbFile = CN.CONTENT_DB_PATH + "sequence" + ".ser";
                
                SequenceGenerator sequenceGenerator = 
                        (SequenceGenerator) DBUtils.read(dbFile);
                
                if(sequenceGenerator == null) {
                        sequenceGenerator = new SequenceGenerator();
                }
                
                return sequenceGenerator;
        }
        
        /**
         * 
         */
        private long[] currentCounts = null;

        /**
         * A private Constructor prevents any other class from instantiating
         */
        private SequenceGenerator() {
                this.currentCounts = new long[1];
                
                // Set initial values
                this.currentCounts[ENTITY] = 1000000;
        }
        
        /**
         * 
         * @param index
         * @return currentCount
         * @throws Exception 
         */
        public long getNextSequence(int index) throws Exception {
                // Increment
                this.currentCounts[index]++;
                
                // Store back
                String dbFile = CN.CONTENT_DB_PATH + "sequence" + ".ser";
                DBUtils.store(this, dbFile);
                
                return this.currentCounts[index];
        }
        
}