io.datakernel.util.IntrusiveLinkedList Maven / Gradle / Ivy
/*
* Copyright (C) 2015 SoftIndex LLC.
*
* Licensed 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 io.datakernel.util;
import io.datakernel.annotation.Nullable;
import java.util.Iterator;
import java.util.NoSuchElementException;
public final class IntrusiveLinkedList implements Iterable {
private IntrusiveLinkedList() {
}
public static IntrusiveLinkedList create() {
return new IntrusiveLinkedList();
}
@Override
public Iterator iterator() {
return new Iterator() {
Node node = getFirstNode();
@Override
public boolean hasNext() {
return node != null;
}
@Override
public T next() {
if (!hasNext()) throw new NoSuchElementException();
T result = node.value;
node = node.next;
return result;
}
};
}
public static final class Node {
final T value;
Node prev;
Node next;
public Node getPrev() {
return prev;
}
public Node getNext() {
return next;
}
public T getValue() {
return value;
}
Node(T value) {
this.value = value;
}
}
private int size;
private Node first;
private Node last;
public Node getFirstNode() {
return first;
}
public Node getLastNode() {
return last;
}
@Nullable
public T getFirstValue() {
return first != null ? first.value : null;
}
@Nullable
public T getLastValue() {
return last != null ? last.value : null;
}
public boolean isEmpty() {
return first == null;
}
public void clear() {
first = null;
last = null;
}
public int size() {
return size;
}
public Node removeFirstNode() {
if (first == null)
return null;
Node node = first;
first = node.next;
if (node.next != null) {
node.next.prev = node.prev;
} else {
assert last == node;
last = node.prev;
}
size--;
node.next = node.prev = null;
return node;
}
public Node removeLastNode() {
if (last == null)
return null;
Node node = last;
last = node.prev;
if (node.prev != null) {
node.prev.next = node.next;
} else {
assert first == node;
first = node.next;
}
size--;
node.next = node.prev = null;
return node;
}
@Nullable
public T removeFirstValue() {
Node node = removeFirstNode();
if (node == null) {
return null;
}
return node.getValue();
}
@Nullable
public T removeLastValue() {
Node node = removeLastNode();
if (node == null) {
return null;
}
return node.getValue();
}
public Node addFirstValue(T value) {
Node node = new Node<>(value);
addFirstNode(node);
return node;
}
public Node addLastValue(T value) {
Node node = new Node<>(value);
addLastNode(node);
return node;
}
public void addFirstNode(Node node) {
assert node.prev == null && node.next == null;
if (first != null) {
assert first.prev == null;
first.prev = node;
node.next = first;
} else {
assert last == null;
last = node;
}
first = node;
size++;
}
public void addLastNode(Node node) {
assert node.prev == null && node.next == null;
if (last != null) {
assert last.next == null;
last.next = node;
node.prev = last;
} else {
assert first == null;
first = node;
}
last = node;
size++;
}
public void moveNodeToLast(Node node) {
if (node.next == null)
return;
removeNode(node);
addLastNode(node);
}
public void moveNodeToFirst(Node node) {
if (node.prev == null)
return;
removeNode(node);
addFirstNode(node);
}
public void removeNode(Node node) {
if (node.prev != null) {
node.prev.next = node.next;
} else {
assert first == node;
first = node.next;
}
if (node.next != null) {
node.next.prev = node.prev;
} else {
assert last == node;
last = node.prev;
}
node.next = node.prev = null;
size--;
}
}