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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/*
* 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.jasper.util;
/**
* The FastRemovalDequeue is a Dequeue that supports constant time removal of entries. This is achieved by using a
* doubly linked list and wrapping any object added to the collection with an Entry type, that is returned to the
* consumer. When removing an object from the list, the consumer provides this Entry object.
* <p>
* The Entry type is nearly opaque to the consumer of the queue. The only public member is the getter for any object
* displaced when adding a new object to the queue. This can be used to destroy that object.
* <p>
* The Entry object contains the links pointing to the neighbours in the doubly linked list, so that removal of an Entry
* does not need to search for it but instead can be done in constant time.
* <p>
* The implementation is fully thread-safe.
* <p>
* Invalidation of Entry objects during removal from the list is done by setting their "valid" field to false. All
* public methods which take Entry objects as arguments are NOP if the entry is no longer valid.
* <p>
* A typical use of the FastRemovalDequeue is a list of entries in sorted order, where the sort position of an object
* will only switch to first or last.
* <p>
* Whenever the sort position needs to change, the consumer can remove the object and reinsert it in front or at the end
* in constant time. So keeping the list sorted is very cheap.
*
* @param <T> The type of elements in the queue
*/
public class FastRemovalDequeue<T> {
/** Maximum size of the queue */
private final int maxSize;
/** First element of the queue. */
protected Entry first;
/** Last element of the queue. */
protected Entry last;
/** Size of the queue */
private int size;
/**
* Initialize empty queue.
*
* @param maxSize The maximum size to which the queue will be allowed to grow
*/
public FastRemovalDequeue(int maxSize) {
if (maxSize <= 1) {
maxSize = 2;
}
this.maxSize = maxSize;
first = null;
last = null;
size = 0;
}
/**
* Retrieve the size of the list. This method also needs to be externally synchronized to ensure correct publication
* of changes.
*
* @return the size of the list.
*/
public synchronized int getSize() {
return size;
}
/**
* Adds an object to the start of the list and returns the entry created for said object. The entry can later be
* reused for moving the entry.
*
* @param object the object to prepend to the start of the list.
*
* @return an entry for use when the object should be moved.
*/
public synchronized Entry push(final T object) {
Entry entry = new Entry(object);
if (size >= maxSize) {
entry.setReplaced(pop());
}
if (first == null) {
first = last = entry;
} else {
first.setPrevious(entry);
entry.setNext(first);
first = entry;
}
size++;
return entry;
}
/**
* Adds an object to the end of the list and returns the entry created for said object. The entry can later be
* reused for moving the entry.
*
* @param object the object to append to the end of the list.
*
* @return an entry for use when the object should be moved.
*/
public synchronized Entry unpop(final T object) {
Entry entry = new Entry(object);
if (size >= maxSize) {
entry.setReplaced(unpush());
}
if (first == null) {
first = last = entry;
} else {
last.setNext(entry);
entry.setPrevious(last);
last = entry;
}
size++;
return entry;
}
/**
* Removes the first element of the list and returns its content.
*
* @return the content of the first element of the list.
**/
public synchronized T unpush() {
T content = null;
if (first != null) {
Entry element = first;
first = first.getNext();
content = element.getContent();
if (first == null) {
last = null;
} else {
first.setPrevious(null);
}
size--;
element.invalidate();
}
return content;
}
/**
* Removes the last element of the list and returns its content.
*
* @return the content of the last element of the list.
**/
public synchronized T pop() {
T content = null;
if (last != null) {
Entry element = last;
last = last.getPrevious();
content = element.getContent();
if (last == null) {
first = null;
} else {
last.setNext(null);
}
size--;
element.invalidate();
}
return content;
}
/**
* Removes any element of the list and returns its content.
*
* @param element The element to remove
*/
public synchronized void remove(final Entry element) {
if (element == null || !element.getValid()) {
return;
}
Entry next = element.getNext();
Entry prev = element.getPrevious();
if (next != null) {
next.setPrevious(prev);
} else {
last = prev;
}
if (prev != null) {
prev.setNext(next);
} else {
first = next;
}
size--;
element.invalidate();
}
/**
* Moves the element in front. Could also be implemented as remove() and push(), but explicitly coding might be a
* bit faster.
*
* @param element the entry to move in front.
*/
public synchronized void moveFirst(final Entry element) {
if (element.getValid() && element.getPrevious() != null) {
Entry prev = element.getPrevious();
Entry next = element.getNext();
prev.setNext(next);
if (next != null) {
next.setPrevious(prev);
} else {
last = prev;
}
first.setPrevious(element);
element.setNext(first);
element.setPrevious(null);
first = element;
}
}
/**
* Moves the element to the back. Could also be implemented as remove() and unpop(), but explicitly coding might be
* a bit faster.
*
* @param element the entry to move to the back.
*/
public synchronized void moveLast(final Entry element) {
if (element.getValid() && element.getNext() != null) {
Entry next = element.getNext();
Entry prev = element.getPrevious();
next.setPrevious(prev);
if (prev != null) {
prev.setNext(next);
} else {
first = next;
}
last.setNext(element);
element.setPrevious(last);
element.setNext(null);
last = element;
}
}
/**
* Implementation of a doubly linked list entry. All implementation details are private. For the consumer of the
* above collection, this is simply garbage in, garbage out.
*/
public class Entry {
/** Is this entry still valid? */
private boolean valid = true;
/** The content this entry is valid for. */
private final T content;
/** Optional content that was displaced by this entry */
private T replaced = null;
/** Pointer to next element in queue. */
private Entry next = null;
/** Pointer to previous element in queue. */
private Entry previous = null;
private Entry(T object) {
content = object;
}
private boolean getValid() {
return valid;
}
private void invalidate() {
this.valid = false;
this.previous = null;
this.next = null;
}
public final T getContent() {
return content;
}
public final T getReplaced() {
return replaced;
}
private void setReplaced(final T replaced) {
this.replaced = replaced;
}
public final void clearReplaced() {
this.replaced = null;
}
private Entry getNext() {
return next;
}
private void setNext(final Entry next) {
this.next = next;
}
private Entry getPrevious() {
return previous;
}
private void setPrevious(final Entry previous) {
this.previous = previous;
}
@Override
public String toString() {
return "Entry-" + content.toString();
}
}
}
|