Subversion Repositories SmartDukaan

Rev

Rev 10 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 10 Rev 21
Line 1... Line 1...
1
/**
1
/**
2
 * 
2
 * 
3
 */
3
 */
4
package in.shop2020.metamodel.util;
4
package in.shop2020.metamodel.util;
5
 
5
 
-
 
6
import in.shop2020.metamodel.definitions.Unit;
-
 
7
 
6
import java.io.Serializable;
8
import java.io.Serializable;
-
 
9
import java.util.Map;
7
 
10
 
8
/**
11
/**
9
 * @author naveen
12
 * @author naveen
10
 * 
13
 * 
11
 * Singleton
14
 * Singleton
12
 */
15
 */
13
public class SequenceGenerator implements Serializable {
16
public class SequenceGenerator implements Serializable {
14
	private static final SequenceGenerator sequenceGenerator = new SequenceGenerator();
-
 
15
 
17
 
16
	/**
18
	/**
17
	 * 
19
	 * 
18
	 */
20
	 */
19
	private static final long serialVersionUID = 1L;
21
	private static final long serialVersionUID = 1L;
20
	public static final int UNIT = 0;
22
	public static final int ENTITY = 0;
21
 
23
 
22
	/**
24
	/**
23
	 * 
25
	 * 
24
	 * @return 	SequenceGenerator 	single instance of SequenceGenerator
26
	 * @return 	SequenceGenerator 	instance of SequenceGenerator
-
 
27
	 * @throws Exception 
25
	 */
28
	 */
26
	public static final SequenceGenerator getInstance() {
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
		
27
		return sequenceGenerator;
40
		return sequenceGenerator;
28
	}
41
	}
29
	
42
	
30
	/**
43
	/**
31
	 * 
44
	 * 
32
	 */
45
	 */
33
	private long[] currentCounts;
46
	private long[] currentCounts = null;
34
 
47
 
35
	/**
48
	/**
36
	 * A private Constructor prevents any other class from instantiating
49
	 * A private Constructor prevents any other class from instantiating
37
	 */
50
	 */
38
	private SequenceGenerator() {
51
	private SequenceGenerator() {
39
		this.currentCounts = new long[1];
52
		this.currentCounts = new long[1];
40
		
53
		
41
		// Set initial values
54
		// Set initial values
42
		this.currentCounts[UNIT] = 1010000;
55
		this.currentCounts[ENTITY] = 1000000;
43
	}
56
	}
44
	
57
	
45
	/**
58
	/**
46
	 * 
59
	 * 
47
	 * @param index
60
	 * @param index
48
	 * @return currentCount
61
	 * @return currentCount
-
 
62
	 * @throws Exception 
49
	 */
63
	 */
50
	public long getNextSequence(int index) {
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
		
51
		return this.currentCounts[index]++;
72
		return this.currentCounts[index];
52
	}
73
	}
53
	
74
	
54
}
75
}