Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 shop2020 1
/**
2
 * 
3
 */
4
package in.shop2020.metamodel.util;
5
 
21 naveen 6
import in.shop2020.metamodel.definitions.Unit;
7
 
10 shop2020 8
import java.io.Serializable;
21 naveen 9
import java.util.Map;
10 shop2020 10
 
11
/**
12
 * @author naveen
13
 * 
14
 * Singleton
15
 */
16
public class SequenceGenerator implements Serializable {
17
 
18
	/**
19
	 * 
20
	 */
21
	private static final long serialVersionUID = 1L;
21 naveen 22
	public static final int ENTITY = 0;
10 shop2020 23
 
24
	/**
25
	 * 
21 naveen 26
	 * @return 	SequenceGenerator 	instance of SequenceGenerator
27
	 * @throws Exception 
10 shop2020 28
	 */
21 naveen 29
	public static final SequenceGenerator getInstance() throws Exception {
30
		// De-serialize
31
		String dbFile = CN.CONTENT_DB_PATH + "sequence" + ".ser";
32
 
33
		SequenceGenerator sequenceGenerator = 
34
			(SequenceGenerator) DBUtils.read(dbFile);
35
 
36
		if(sequenceGenerator == null) {
37
			sequenceGenerator = new SequenceGenerator();
38
		}
39
 
10 shop2020 40
		return sequenceGenerator;
41
	}
42
 
43
	/**
44
	 * 
45
	 */
21 naveen 46
	private long[] currentCounts = null;
10 shop2020 47
 
48
	/**
49
	 * A private Constructor prevents any other class from instantiating
50
	 */
51
	private SequenceGenerator() {
52
		this.currentCounts = new long[1];
53
 
54
		// Set initial values
21 naveen 55
		this.currentCounts[ENTITY] = 1000000;
10 shop2020 56
	}
57
 
58
	/**
59
	 * 
60
	 * @param index
61
	 * @return currentCount
21 naveen 62
	 * @throws Exception 
10 shop2020 63
	 */
21 naveen 64
	public long getNextSequence(int index) throws Exception {
65
		// Increment
66
		this.currentCounts[index]++;
67
 
68
		// Store back
69
		String dbFile = CN.CONTENT_DB_PATH + "sequence" + ".ser";
70
		DBUtils.store(this, dbFile);
71
 
72
		return this.currentCounts[index];
10 shop2020 73
	}
74
 
75
}