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
 
64 naveen 6
import in.shop2020.util.DBUtils;
80 naveen 7
import in.shop2020.util.Utils;
64 naveen 8
 
457 rajveer 9
import java.io.File;
10 shop2020 10
import java.io.Serializable;
22 naveen 11
import java.util.Arrays;
10 shop2020 12
 
13
/**
49 naveen 14
 * Utility class to generate unique numbers
15
 * 
10 shop2020 16
 * @author naveen
17
 * 
49 naveen 18
 * 
10 shop2020 19
 */
20
public class SequenceGenerator implements Serializable {
21
 
22
	/**
23
	 * 
24
	 */
25
	private static final long serialVersionUID = 1L;
49 naveen 26
 
27
	/**
51 naveen 28
	 * Entity object's starting sequence number
49 naveen 29
	 */
21 naveen 30
	public static final int ENTITY = 0;
10 shop2020 31
 
32
	/**
51 naveen 33
	 * Singleton instance that is de-serialized from file on the disk
10 shop2020 34
	 * 
21 naveen 35
	 * @return 	SequenceGenerator 	instance of SequenceGenerator
36
	 * @throws Exception 
10 shop2020 37
	 */
21 naveen 38
	public static final SequenceGenerator getInstance() throws Exception {
39
		// De-serialize
457 rajveer 40
		String dbFile = Utils.CONTENT_DB_PATH + "entities" + File.separator + "sequence.ser";
21 naveen 41
 
42
		SequenceGenerator sequenceGenerator = 
43
			(SequenceGenerator) DBUtils.read(dbFile);
44
 
45
		if(sequenceGenerator == null) {
46
			sequenceGenerator = new SequenceGenerator();
47
		}
48
 
10 shop2020 49
		return sequenceGenerator;
50
	}
51
 
52
	/**
726 chandransh 53
	 * Instance member that records all current counts
10 shop2020 54
	 */
21 naveen 55
	private long[] currentCounts = null;
10 shop2020 56
 
57
	/**
51 naveen 58
	 * A private Constructor prevents any other class from instantiating. All 
59
	 * objects that need dynamic sequence generation need to provide starting 
60
	 * number here.
10 shop2020 61
	 */
62
	private SequenceGenerator() {
63
		this.currentCounts = new long[1];
64
 
65
		// Set initial values
21 naveen 66
		this.currentCounts[ENTITY] = 1000000;
10 shop2020 67
	}
68
 
69
	/**
51 naveen 70
	 * Client of the class call this method to get new sequence number. 
71
	 * Once a sequence number if fetched it cannot be undone.
10 shop2020 72
	 * 
73
	 * @param index
74
	 * @return currentCount
21 naveen 75
	 * @throws Exception 
10 shop2020 76
	 */
21 naveen 77
	public long getNextSequence(int index) throws Exception {
78
		// Increment
79
		this.currentCounts[index]++;
80
 
81
		// Store back
457 rajveer 82
		String dbFile = Utils.CONTENT_DB_PATH + "entities" + File.separator + "sequence.ser";
21 naveen 83
		DBUtils.store(this, dbFile);
84
 
85
		return this.currentCounts[index];
10 shop2020 86
	}
22 naveen 87
 
88
	/* (non-Javadoc)
89
	 * @see java.lang.Object#toString()
90
	 */
91
	@Override
92
	public String toString() {
93
		return "SequenceGenerator [currentCounts="
94
				+ Arrays.toString(currentCounts) + "]";
95
	}
96
 
10 shop2020 97
}