1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.util;
import java.util.LinkedList;
import java.util.ListIterator;
/**
* <p>
* 24.08.2009<br>
* </p>
*
* @author Stefan Stern<br>
*/
public class IdentifierManager {
public static final long MAX_ID = Long.MAX_VALUE - 1;
public static final long MIN_ID = 0L;
/**
*
*/
private final long upperbound;
/**
*
*/
private final long lowerbound;
/**
* List of segments of available identifiers
*/
private LinkedList<Segment> segments;
/**
* @param lowerbound the lower limit of the id-range to manage. Must be greater than or equal to {@link #MIN_ID}.
* @param upperbound the upper limit of the id-range to manage. Must be less then or equal {@link #MAX_ID}.
*/
public IdentifierManager(long lowerbound, long upperbound) {
if (lowerbound > upperbound) {
String message = "lowerbound must not be greater than upperbound";
throw new IllegalArgumentException(message);
}
else if (lowerbound < MIN_ID) {
String message = "lowerbound must be greater than or equal to " + Long.toString(MIN_ID);
throw new IllegalArgumentException(message);
}
else if (upperbound > MAX_ID) {
/*
* while MAX_ID is Long.MAX_VALUE, this check is pointless. But if
* someone subclasses / tweaks the limits, this check if fine.
*/
String message = "upperbound must be less thean or equal " + Long.toString(MAX_ID);
throw new IllegalArgumentException(message);
}
this.lowerbound = lowerbound;
this.upperbound = upperbound;
this.segments = new LinkedList<Segment>();
segments.add(new Segment(lowerbound, upperbound));
}
public long reserve(long id) {
if (id < lowerbound || id > upperbound) {
throw new IllegalArgumentException("Value for parameter 'id' was out of bounds");
}
verifyIdentifiersLeft();
if (id == upperbound) {
Segment lastSegment = segments.getLast();
if (lastSegment.end == upperbound) {
lastSegment.end = upperbound - 1;
if (lastSegment.start > lastSegment.end) {
segments.removeLast();
}
return id;
}
return reserveNew();
}
if (id == lowerbound) {
Segment firstSegment = segments.getFirst();
if (firstSegment.start == lowerbound) {
firstSegment.start = lowerbound + 1;
if (firstSegment.end < firstSegment.start) {
segments.removeFirst();
}
return id;
}
return reserveNew();
}
ListIterator<Segment> iter = segments.listIterator();
while (iter.hasNext()) {
Segment segment = iter.next();
if (segment.end < id) {
continue;
}
else if (segment.start > id) {
break;
}
else if (segment.start == id) {
segment.start = id + 1;
if (segment.end < segment.start) {
iter.remove();
}
return id;
}
else if (segment.end == id) {
segment.end = id - 1;
if (segment.start > segment.end) {
iter.remove();
}
return id;
}
else {
iter.add(new Segment(id + 1, segment.end));
segment.end = id - 1;
return id;
}
}
return reserveNew();
}
/**
* @return a new identifier.
* @throws IllegalStateException if no more identifiers are available, then an Exception is raised.
*/
public long reserveNew() {
verifyIdentifiersLeft();
Segment segment = segments.getFirst();
long result = segment.start;
segment.start += 1;
if (segment.start > segment.end) {
segments.removeFirst();
}
return result;
}
/**
* @param id
* the identifier to release. Must be greater than or equal to
* {@link #lowerbound} and must be less than or equal to {@link #upperbound}
* @return true, if the identifier was reserved and has been successfully
* released, false, if the identifier was not reserved.
*/
public boolean release(long id) {
if (id < lowerbound || id > upperbound) {
throw new IllegalArgumentException("Value for parameter 'id' was out of bounds");
}
if (id == upperbound) {
Segment lastSegment = segments.getLast();
if (lastSegment.end == upperbound - 1) {
lastSegment.end = upperbound;
return true;
} else if (lastSegment.end == upperbound) {
return false;
} else {
segments.add(new Segment(upperbound, upperbound));
return true;
}
}
if (id == lowerbound) {
Segment firstSegment = segments.getFirst();
if (firstSegment.start == lowerbound + 1) {
firstSegment.start = lowerbound;
return true;
} else if (firstSegment.start == lowerbound) {
return false;
} else {
segments.addFirst(new Segment(lowerbound, lowerbound));
return true;
}
}
long higher = id + 1;
long lower = id - 1;
ListIterator<Segment> iter = segments.listIterator();
while (iter.hasNext()) {
Segment segment = iter.next();
if (segment.end < lower) {
continue;
}
if (segment.start > higher) {
iter.previous();
iter.add(new Segment(id, id));
return true;
}
if (segment.start == higher) {
segment.start = id;
return true;
}
else if (segment.end == lower) {
segment.end = id;
/* check if releasing this elements glues two segments into one */
if (iter.hasNext()) {
Segment next = iter.next();
if (next.start == segment.end + 1) {
segment.end = next.end;
iter.remove();
}
}
return true;
}
else {
/* id was not reserved, return false */
break;
}
}
return false;
}
public long getRemainingIdentifiers() {
long result = 0;
for (Segment segment : segments) {
result = result - segment.start;
result = result + segment.end + 1;
}
return result;
}
/**
*
*/
private void verifyIdentifiersLeft() {
if (segments.isEmpty()) {
throw new IllegalStateException("No identifiers left");
}
}
private static class Segment {
public Segment(long start, long end) {
this.start = start;
this.end = end;
}
public long start;
public long end;
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {
return "[" + start + "; " + end + "]";
}
}
}
|