| 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 |
* Internally, it uses immutable objects. |
| 13 |
* It uses recursion and is not meant for long lists. |
| 14 |
* |
| 15 |
* @param <K> the key type |
| 16 |
*/ |
| 17 |
public class ConcurrentLinkedList<K> { |
| 18 |
|
| 19 |
/** |
| 20 |
* The sentinel entry. |
| 21 |
*/ |
| 22 |
static final Entry<?> NULL = new Entry<>(null, null); |
| 23 |
|
| 24 |
/** |
| 25 |
* The head entry. |
| 26 |
*/ |
| 27 |
@SuppressWarnings("unchecked") |
| 28 |
volatile Entry<K> head = (Entry<K>) NULL; |
| 29 |
|
| 30 |
/** |
| 31 |
* Get the first element, or null if none. |
| 32 |
* |
| 33 |
* @return the first element |
| 34 |
*/ |
| 35 |
public K peekFirst() { |
| 36 |
Entry<K> x = head; |
| 37 |
return x.obj; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get the last element, or null if none. |
| 42 |
* |
| 43 |
* @return the last element |
| 44 |
*/ |
| 45 |
public K peekLast() { |
| 46 |
Entry<K> x = head; |
| 47 |
while (x != NULL && x.next != NULL) { |
| 48 |
x = x.next; |
| 49 |
} |
| 50 |
return x.obj; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Add an element at the end. |
| 55 |
* |
| 56 |
* @param obj the element |
| 57 |
*/ |
| 58 |
public synchronized void add(K obj) { |
| 59 |
head = Entry.append(head, obj); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Remove the first element, if it matches. |
| 64 |
* |
| 65 |
* @param obj the element to remove |
| 66 |
* @return true if the element matched and was removed |
| 67 |
*/ |
| 68 |
public synchronized boolean removeFirst(K obj) { |
| 69 |
if (head.obj != obj) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
head = head.next; |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Remove the last element, if it matches. |
| 78 |
* |
| 79 |
* @param obj the element to remove |
| 80 |
* @return true if the element matched and was removed |
| 81 |
*/ |
| 82 |
public synchronized boolean removeLast(K obj) { |
| 83 |
if (peekLast() != obj) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
head = Entry.removeLast(head); |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get an iterator over all entries. |
| 92 |
* |
| 93 |
* @return the iterator |
| 94 |
*/ |
| 95 |
public Iterator<K> iterator() { |
| 96 |
return new Iterator<K>() { |
| 97 |
|
| 98 |
Entry<K> current = head; |
| 99 |
|
| 100 |
@Override |
| 101 |
public boolean hasNext() { |
| 102 |
return current != NULL; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public K next() { |
| 107 |
K x = current.obj; |
| 108 |
current = current.next; |
| 109 |
return x; |
| 110 |
} |
| 111 |
|
| 112 |
}; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* An entry in the linked list. |
| 117 |
*/ |
| 118 |
private static class Entry<K> { |
| 119 |
final K obj; |
| 120 |
Entry<K> next; |
| 121 |
|
| 122 |
Entry(K obj, Entry<K> next) { |
| 123 |
this.obj = obj; |
| 124 |
this.next = next; |
| 125 |
} |
| 126 |
|
| 127 |
@SuppressWarnings("unchecked") |
| 128 |
static <K> Entry<K> append(Entry<K> list, K obj) { |
| 129 |
if (list == NULL) { |
| 130 |
return new Entry<>(obj, (Entry<K>) NULL); |
| 131 |
} |
| 132 |
return new Entry<>(list.obj, append(list.next, obj)); |
| 133 |
} |
| 134 |
|
| 135 |
@SuppressWarnings("unchecked") |
| 136 |
static <K> Entry<K> removeLast(Entry<K> list) { |
| 137 |
if (list == NULL || list.next == NULL) { |
| 138 |
return (Entry<K>) NULL; |
| 139 |
} |
| 140 |
return new Entry<>(list.obj, removeLast(list.next)); |
| 141 |
} |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
} |