1 package org.apache.ojb.broker.util.sequence;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 import org.apache.commons.lang.builder.ToStringBuilder;
19 import org.apache.commons.lang.builder.ToStringStyle;
20
21 import java.io.Serializable;
22
23 /**
24 * The HighLowSequence is the persistent part of the {@link SequenceManagerHighLowImpl}.
25 * It makes the maximum reserved key persistently available.
26 *
27 * @version $Id: HighLowSequence.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $
28 */
29 public class HighLowSequence implements Serializable
30 {
31 static final long serialVersionUID = -2174468157880921393L;
32 private String name;
33 private long maxKey;
34 private int grabSize;
35 private Integer version;
36 // This is not stored in the DB
37 protected long curVal = 0;
38
39 /**
40 * Default constructor for the HighLowSequence object
41 */
42 public HighLowSequence()
43 {
44 // make sure that version column in DB is never 'null'
45 // to avoid problems with
46 this(null, 0, 0, new Integer(0));
47 }
48
49 public HighLowSequence(String tableName, long maxKey, int grabSize, Integer version)
50 {
51 this.name = tableName;
52 this.maxKey = maxKey;
53 this.grabSize = grabSize;
54 this.version = version;
55 }
56
57 public HighLowSequence getCopy()
58 {
59 HighLowSequence result = new HighLowSequence(this.name, this.maxKey, this.grabSize, this.version);
60 result.curVal = this.curVal;
61 return result;
62 }
63
64 public String toString()
65 {
66 ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
67 buf.append("name", name).
68 append("grabSize", grabSize).
69 append("version", version).
70 append("maxKey", maxKey).
71 append("currentKey", curVal);
72 return buf.toString();
73 }
74
75 public Integer getVersion()
76 {
77 return version;
78 }
79
80 public void setVersion(Integer version)
81 {
82 this.version = version;
83 }
84
85 /**
86 * Sets the name attribute of the HighLowSequence object
87 *
88 * @param name The new className value
89 */
90 public void setName(String name)
91 {
92 this.name = name;
93 }
94
95 /**
96 * Sets the grab size attribute of the HighLowSequence object
97 *
98 * @param grabSize The new grabSize value
99 */
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 }