| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.dev.util; |
| 7 |
|
| 8 |
import java.util.Iterator; |
| 9 |
|
| 10 |
/** |
| 11 |
* A very simple linked list that supports concurrent access. |
| 12 |
* |
| 13 |
* @param <K> the key type |
| 14 |
*/ |
| 15 |
public class ConcurrentLinkedListWithTail<K> { |
| 16 |
|
| 17 |
/** |
| 18 |
* The first entry (if any). |
| 19 |
*/ |
| 20 |
volatile Entry<K> head; |
| 21 |
|
| 22 |
/** |
| 23 |
* The last entry (if any). |
| 24 |
*/ |
| 25 |
private volatile Entry<K> tail; |
| 26 |
|
| 27 |
/** |
| 28 |
* Get the first element, or null if none. |
| 29 |
* |
| 30 |
* @return the first element |
| 31 |
*/ |
| 32 |
public K peekFirst() { |
| 33 |
Entry<K> x = head; |
| 34 |
return x == null ? null : x.obj; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get the last element, or null if none. |
| 39 |
* |
| 40 |
* @return the last element |
| 41 |
*/ |
| 42 |
public K peekLast() { |
| 43 |
Entry<K> x = tail; |
| 44 |
return x == null ? null : x.obj; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Add an element at the end. |
| 49 |
* |
| 50 |
* @param obj the element |
| 51 |
*/ |
| 52 |
public void add(K obj) { |
| 53 |
Entry<K> x = new Entry<>(obj); |
| 54 |
Entry<K> t = tail; |
| 55 |
if (t != null) { |
| 56 |
t.next = x; |
| 57 |
} |
| 58 |
tail = x; |
| 59 |
if (head == null) { |
| 60 |
head = x; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Remove the first element, if it matches. |
| 66 |
* |
| 67 |
* @param obj the element to remove |
| 68 |
* @return true if the element matched and was removed |
| 69 |
*/ |
| 70 |
public boolean removeFirst(K obj) { |
| 71 |
Entry<K> x = head; |
| 72 |
if (x == null || x.obj != obj) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
if (head == tail) { |
| 76 |
tail = x.next; |
| 77 |
} |
| 78 |
head = x.next; |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Remove the last element, if it matches. |
| 84 |
* |
| 85 |
* @param obj the element to remove |
| 86 |
* @return true if the element matched and was removed |
| 87 |
*/ |
| 88 |
public boolean removeLast(K obj) { |
| 89 |
Entry<K> x = head; |
| 90 |
if (x == null) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
Entry<K> prev = null; |
| 94 |
while (x.next != null) { |
| 95 |
prev = x; |
| 96 |
x = x.next; |
| 97 |
} |
| 98 |
if (x.obj != obj) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
if (prev != null) { |
| 102 |
prev.next = null; |
| 103 |
} |
| 104 |
if (head == tail) { |
| 105 |
head = prev; |
| 106 |
} |
| 107 |
tail = prev; |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get an iterator over all entries. |
| 113 |
* |
| 114 |
* @return the iterator |
| 115 |
*/ |
| 116 |
public Iterator<K> iterator() { |
| 117 |
return new Iterator<K>() { |
| 118 |
|
| 119 |
Entry<K> current = head; |
| 120 |
|
| 121 |
@Override |
| 122 |
public boolean hasNext() { |
| 123 |
return current != null; |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public K next() { |
| 128 |
K x = current.obj; |
| 129 |
current = current.next; |
| 130 |
return x; |
| 131 |
} |
| 132 |
|
| 133 |
}; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* An entry in the linked list. |
| 138 |
*/ |
| 139 |
private static class Entry<K> { |
| 140 |
final K obj; |
| 141 |
Entry<K> next; |
| 142 |
|
| 143 |
Entry(K obj) { |
| 144 |
this.obj = obj; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
} |