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.1 2007-08-24 22:17:29 ewestfal Exp $
028 */
029 public class HighLowSequence implements Serializable
030 {
031 static final long serialVersionUID = -2174468157880921393L;
032 private String name;
033 private long maxKey;
034 private int grabSize;
035 private Integer version;
036 // This is not stored in the DB
037 protected long curVal = 0;
038
039 /**
040 * Default constructor for the HighLowSequence object
041 */
042 public HighLowSequence()
043 {
044 // make sure that version column in DB is never 'null'
045 // to avoid problems with
046 this(null, 0, 0, new Integer(0));
047 }
048
049 public HighLowSequence(String tableName, long maxKey, int grabSize, Integer version)
050 {
051 this.name = tableName;
052 this.maxKey = maxKey;
053 this.grabSize = grabSize;
054 this.version = version;
055 }
056
057 public HighLowSequence getCopy()
058 {
059 HighLowSequence result = new HighLowSequence(this.name, this.maxKey, this.grabSize, this.version);
060 result.curVal = this.curVal;
061 return result;
062 }
063
064 public String toString()
065 {
066 ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
067 buf.append("name", name).
068 append("grabSize", grabSize).
069 append("version", version).
070 append("maxKey", maxKey).
071 append("currentKey", curVal);
072 return buf.toString();
073 }
074
075 public Integer getVersion()
076 {
077 return version;
078 }
079
080 public void setVersion(Integer version)
081 {
082 this.version = version;
083 }
084
085 /**
086 * Sets the name attribute of the HighLowSequence object
087 *
088 * @param name The new className value
089 */
090 public void setName(String name)
091 {
092 this.name = name;
093 }
094
095 /**
096 * Sets the grab size attribute of the HighLowSequence object
097 *
098 * @param grabSize The new grabSize value
099 */
100 public void setGrabSize(int grabSize)
101 {
102 this.grabSize = grabSize;
103 }
104
105 /**
106 * Sets the maxKey attribute of the HighLowSequence object
107 *
108 * @param maxKey The new maxKey value
109 */
110 public void setMaxKey(long maxKey)
111 {
112 this.maxKey = maxKey;
113 }
114
115 /**
116 * Gets the name attribute of the HighLowSequence object
117 *
118 * @return The className value
119 */
120 public String getName()
121 {
122 return this.name;
123 }
124
125 /**
126 * Gets the grabSize attribute of the HighLowSequence object
127 *
128 * @return The grabSize value
129 */
130 public int getGrabSize()
131 {
132 return this.grabSize;
133 }
134
135 /**
136 * Gets the next key from this sequence
137 *
138 * @return The next key or 0 if sequence needs to grab new keyset
139 */
140 public long getNextId()
141 {
142 if (curVal == maxKey)
143 {
144 //no reserved IDs, must be reloaded, reserve new keyset and saved
145 return 0;
146 }
147 else
148 {
149 curVal = curVal + 1;
150 return curVal;
151 }
152 }
153
154 /**
155 * Gets the maxKey attribute of the HighLowSequence object
156 *
157 * @return The maxKey value
158 */
159 public long getMaxKey()
160 {
161 return this.maxKey;
162 }
163
164 /**
165 * Grabs the next key set, the sequence must be saved afterwards!!
166 */
167 public void grabNextKeySet()
168 {
169 curVal = maxKey;
170 maxKey = maxKey + grabSize;
171 }
172 }