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