001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb;
032:
033: import org.hsqldb.HsqlNameManager.HsqlName;
034:
035: /**
036: * Maintains a sequence of numbers.
037: *
038: * @author fredt@users
039: * @since 1.7.2
040: * @version 1.7.2
041: */
042: public class NumberSequence {
043:
044: HsqlName name;
045:
046: // original start value - used in CREATE and ALTER commands
047: private long startValue;
048:
049: // present value
050: private long currValue;
051:
052: // last value
053: private long lastValue;
054: private long increment;
055: private int dataType;
056:
057: /**
058: * constructor with initial value and increment;
059: */
060: public NumberSequence(HsqlName name, long value, long increment,
061: int type) {
062:
063: this .name = name;
064: startValue = currValue = lastValue = value;
065: this .increment = increment;
066: dataType = type;
067: }
068:
069: /**
070: * principal getter for the next sequence value
071: */
072: synchronized long getValue() {
073:
074: long value = currValue;
075:
076: currValue += increment;
077:
078: return value;
079: }
080:
081: /**
082: * getter for a given value
083: */
084: synchronized long getValue(long value) {
085:
086: if (value >= currValue) {
087: currValue = value;
088: currValue += increment;
089:
090: return value;
091: } else {
092: return value;
093: }
094: }
095:
096: /** @todo fredt - check against max value of type */
097: Object getValueObject() {
098:
099: long value = currValue;
100:
101: currValue += increment;
102:
103: Object result;
104:
105: if (dataType == Types.INTEGER) {
106: result = new Integer((int) value);
107: } else {
108: result = new Long(value);
109: }
110:
111: return result;
112: }
113:
114: /**
115: * reset to start value
116: */
117: void reset() {
118:
119: // no change if called before getValue() or called twice
120: lastValue = currValue = startValue;
121: }
122:
123: /**
124: * get next value without incrementing
125: */
126: public long peek() {
127: return currValue;
128: }
129:
130: /**
131: * true if one or more values were retreived since the last resetWasUsed
132: */
133: boolean wasUsed() {
134: return lastValue != currValue;
135: }
136:
137: /**
138: * reset the wasUsed flag
139: */
140: void resetWasUsed() {
141: lastValue = currValue;
142: }
143:
144: /**
145: * reset to new initial value
146: */
147: public void reset(long value) {
148: startValue = currValue = lastValue = value;
149: }
150:
151: void reset(long value, long increment) {
152:
153: reset(value);
154:
155: this .increment = increment;
156: }
157:
158: int getType() {
159: return dataType;
160: }
161:
162: public HsqlName getName() {
163: return name;
164: }
165:
166: public String getSchemaName() {
167: return name.schema.name;
168: }
169:
170: long getIncrement() {
171: return increment;
172: }
173: }
|