01: /*
02: * This file or a portion of this file is licensed under the terms of
03: * the Globus Toolkit Public License, found in file ../GTPL, or at
04: * http://www.globus.org/toolkit/download/license.html. This notice must
05: * appear in redistributions of this file, with or without modification.
06: *
07: * Redistributions of this Software, with or without modification, must
08: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
09: * some other similar material which is provided with the Software (if
10: * any).
11: *
12: * Copyright 1999-2004 University of Chicago and The University of
13: * Southern California. All rights reserved.
14: */
15: package org.griphyn.vdl.util;
16:
17: import java.text.DecimalFormat;
18:
19: /**
20: * Each instance of this class produces a row of sequence numbers.
21: * By default, the sequence string has six digits.
22: *
23: * @author Jens-S. Vöckler
24: * @author Yong Zhao
25: * @version $Revision: 50 $
26: */
27: public class SequenceGenerator {
28: /**
29: * The sequence counter employed by this generator. Please note
30: * that multiple instances of this class will produce the same
31: * sequences!
32: */
33: private long m_count = 0;
34:
35: /**
36: * The formatting of the number.
37: */
38: private DecimalFormat m_format;
39:
40: /**
41: * C'tor: This will generate an six digit sequence strings.
42: */
43: public SequenceGenerator() {
44: this .m_format = new DecimalFormat("######000000");
45: }
46:
47: /**
48: * C'tor: Instances from this contructor will generate ${prefix}xxxxx.
49: * Please note that multiple instances of this class with the same
50: * prefix will produce the same sequences!
51: *
52: * @param digits are the number of digits to use. This value must be at
53: * least one.
54: * @exception IllegalArgumentException if the number of digits is negative
55: */
56: public SequenceGenerator(int digits) {
57: if (digits < 0)
58: throw new IllegalArgumentException(digits + " < 1");
59: if (digits == 0)
60: this .m_format = null;
61: else {
62: StringBuffer pattern = new StringBuffer(digits << 1);
63: pattern.setLength(digits << 1);
64: for (int i = 0; i < digits; ++i) {
65: pattern.setCharAt(i, '#');
66: pattern.setCharAt(i + digits, '0');
67: }
68: this .m_format = new DecimalFormat(pattern.toString());
69: }
70: }
71:
72: /**
73: * Creates the next sequence number as textual string.
74: * @return the next sequence number string.
75: * @exception RuntimeException, if the maximum permissable value was reached.
76: */
77: public String generate() throws RuntimeException {
78: if (this .m_count == Long.MAX_VALUE)
79: throw new RuntimeException("Reached end of sequence");
80: else
81: this.m_count++;
82:
83: return (this.m_format == null ? Long.toString(this.m_count)
84: : this.m_format.format(this.m_count));
85: }
86: }
|