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