001: package org.apache.ojb.broker.util.sequence;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.commons.lang.builder.ToStringBuilder;
019: import org.apache.commons.lang.builder.ToStringStyle;
020:
021: import java.io.Serializable;
022:
023: /**
024: * The HighLowSequence is the persistent part of the {@link SequenceManagerHighLowImpl}.
025: * It makes the maximum reserved key persistently available.
026: *
027: * @version $Id: HighLowSequence.java,v 1.11.2.2 2005/12/21 22:28:41 tomdz Exp $
028: */
029: public class HighLowSequence implements Serializable {
030: static final long serialVersionUID = -2174468157880921393L;
031: private String name;
032: private long maxKey;
033: private int grabSize;
034: private Integer version;
035: // This is not stored in the DB
036: protected long curVal = 0;
037:
038: /**
039: * Default constructor for the HighLowSequence object
040: */
041: public HighLowSequence() {
042: // make sure that version column in DB is never 'null'
043: // to avoid problems with
044: this (null, 0, 0, new Integer(0));
045: }
046:
047: public HighLowSequence(String tableName, long maxKey, int grabSize,
048: Integer version) {
049: this .name = tableName;
050: this .maxKey = maxKey;
051: this .grabSize = grabSize;
052: this .version = version;
053: }
054:
055: public HighLowSequence getCopy() {
056: HighLowSequence result = new HighLowSequence(this .name,
057: this .maxKey, this .grabSize, this .version);
058: result.curVal = this .curVal;
059: return result;
060: }
061:
062: public String toString() {
063: ToStringBuilder buf = new ToStringBuilder(this ,
064: ToStringStyle.DEFAULT_STYLE);
065: buf.append("name", name).append("grabSize", grabSize).append(
066: "version", version).append("maxKey", maxKey).append(
067: "currentKey", curVal);
068: return buf.toString();
069: }
070:
071: public Integer getVersion() {
072: return version;
073: }
074:
075: public void setVersion(Integer version) {
076: this .version = version;
077: }
078:
079: /**
080: * Sets the name attribute of the HighLowSequence object
081: *
082: * @param name The new className value
083: */
084: public void setName(String name) {
085: this .name = name;
086: }
087:
088: /**
089: * Sets the grab size attribute of the HighLowSequence object
090: *
091: * @param grabSize The new grabSize value
092: */
093: public void setGrabSize(int grabSize) {
094: this .grabSize = grabSize;
095: }
096:
097: /**
098: * Sets the maxKey attribute of the HighLowSequence object
099: *
100: * @param maxKey The new maxKey value
101: */
102: public void setMaxKey(long maxKey) {
103: this .maxKey = maxKey;
104: }
105:
106: /**
107: * Gets the name attribute of the HighLowSequence object
108: *
109: * @return The className value
110: */
111: public String getName() {
112: return this .name;
113: }
114:
115: /**
116: * Gets the grabSize attribute of the HighLowSequence object
117: *
118: * @return The grabSize value
119: */
120: public int getGrabSize() {
121: return this .grabSize;
122: }
123:
124: /**
125: * Gets the next key from this sequence
126: *
127: * @return The next key or 0 if sequence needs to grab new keyset
128: */
129: public long getNextId() {
130: if (curVal == maxKey) {
131: //no reserved IDs, must be reloaded, reserve new keyset and saved
132: return 0;
133: } else {
134: curVal = curVal + 1;
135: return curVal;
136: }
137: }
138:
139: /**
140: * Gets the maxKey attribute of the HighLowSequence object
141: *
142: * @return The maxKey value
143: */
144: public long getMaxKey() {
145: return this .maxKey;
146: }
147:
148: /**
149: * Grabs the next key set, the sequence must be saved afterwards!!
150: */
151: public void grabNextKeySet() {
152: curVal = maxKey;
153: maxKey = maxKey + grabSize;
154: }
155: }
|