Reusable Java library of general tools with minimal external dependencies.
For questions or support, please contact us:
Email: support@aoindustries.com
Phone: 1-800-519-9541
Phone: +1-251-607-9556
Web: https://www.aoindustries.com/contact
public class PersistentLinkedList<E> extends AbstractSequentialList<E> implements List<E>, Queue<E>
Serializes and stores objects in a persistent buffer. Unlike FileList which
is intended for efficient RandomAccess,
this is a linked list implementation and has the expected benefits and costs.
There are no size limits to the stored data.
This class is not thread-safe. It is absolutely critical that external synchronization be applied.
The objects are serialized using the standard Java serialization, unless a
Serializer is provided. If an object that is not Serializable
is to be stored, a Serializer must be provided. Serializers
may also provide a more efficient or more compact representation of an object.
This class is intended for scalability and persistence, not for intra-process or intra-thread shared data.
The first block allocated is a header:
Offset Type Description
0- 3 ASCII "PLL\n"
4- 7 int version
8-15 long block id of the head or END_PTR if empty.
16-23 long block id of the tail or END_PTR if empty.
Each entry consists of:
Offset Name Type Description
0- 7 next long block id of next, END_PTR for last element
8-15 prev long block id of prev, END_PTR for first element
16-23 dataSize long the size of the serialized data, -1 means null element
24+ data data the binary data
TODO: In Java 1.6 support Deque interface instead of just Queue TODO: Add corrupt flag, set on exceptions? Cause immediate crash recovery? TODO: Similar thing for the underlying block buffers and byte buffers?
modCount| Constructor and Description |
|---|
PersistentLinkedList(Class<E> type)
Constructs a list backed by a temporary file using standard serialization.
|
PersistentLinkedList(Class<E> type,
Collection<? extends E> c)
Constructs a list with a temporary file using standard serialization containing all of the provided elements.
|
PersistentLinkedList(PersistentBuffer pbuffer,
Class<E> type)
Constructs a list backed by the provided persistent buffer using the most efficient serialization
for the provided type.
|
PersistentLinkedList(PersistentBuffer pbuffer,
Serializer<E> serializer)
Constructs a list backed by the provided persistent buffer.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E element)
Appends the specified element to the end of this list.
|
void |
add(int index,
E element)
Inserts the specified element at the specified position in this list.
|
boolean |
addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of
this list, in the order that they are returned by the specified
collection's iterator.
|
boolean |
addAll(int index,
Collection<? extends E> c)
Inserts all of the elements in the specified collection into this
list, starting at the specified position.
|
void |
addFirst(E element)
Inserts the specified element at the beginning of this list.
|
void |
addLast(E element)
Appends the specified element to the end of this list.
|
protected void |
checkConsistency(boolean autoCorrect)
Performs a check that this linked list is in a consistent state, optionally
correcting problems that may occur during an unclean shutdown.
|
void |
clear()
Clears the list.
|
void |
close()
Closes the random access file backing this list.
|
boolean |
contains(Object o)
Returns true if this list contains the specified element.
|
Iterator<E> |
descendingIterator() |
E |
element()
Retrieves, but does not remove, the head (first element) of this list.
|
protected void |
finalize() |
E |
get(int index)
Returns the element at the specified position in this list.
|
E |
getFirst()
Returns the first element in this list.
|
E |
getLast()
Returns the last element in this list.
|
int |
indexOf(Object o)
Returns the index of the first occurrence of the specified element
in this list, or -1 if this list does not contain the element.
|
int |
lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element
in this list, or -1 if this list does not contain the element.
|
ListIterator<E> |
listIterator(int index)
Returns a list-iterator of the elements in this list (in proper
sequence), starting at the specified position in the list.
|
boolean |
offer(E e)
Adds the specified element as the tail (last element) of this list.
|
boolean |
offerFirst(E e)
Inserts the specified element at the front of this list.
|
boolean |
offerLast(E e)
Inserts the specified element at the end of this list.
|
E |
peek() |
E |
peekFirst()
Retrieves, but does not remove, the first element of this list,
or returns null if this list is empty.
|
E |
peekLast()
Retrieves, but does not remove, the last element of this list,
or returns null if this list is empty.
|
E |
poll()
Retrieves and removes the head (first element) of this list
|
E |
pollFirst()
Retrieves and removes the first element of this list,
or returns null if this list is empty.
|
E |
pollLast()
Retrieves and removes the last element of this list,
or returns null if this list is empty.
|
E |
pop()
Pops an element from the stack represented by this list.
|
void |
push(E e)
Pushes an element onto the stack represented by this list.
|
E |
remove()
Retrieves and removes the head (first element) of this list.
|
E |
remove(int index)
Removes the element at the specified position in this list.
|
boolean |
remove(Object o)
Removes the first occurrence of the specified element from this list,
if it is present.
|
E |
removeFirst()
Removes and returns the first element from this list.
|
boolean |
removeFirstOccurrence(Object o)
Removes the first occurrence of the specified element in this
list (when traversing the list from head to tail).
|
E |
removeLast()
Removes and returns the last element from this list.
|
boolean |
removeLastOccurrence(Object o)
Removes the last occurrence of the specified element in this
list (when traversing the list from head to tail).
|
E |
set(int index,
E element)
Replaces the element at the specified position in this list with the
specified element.
|
int |
size()
Gets the number of elements in this list.
|
Object[] |
toArray() |
<T> T[] |
toArray(T[] a) |
iteratorequals, hashCode, listIterator, removeRange, subListcontainsAll, isEmpty, removeAll, retainAll, toStringcontainsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, replaceAll, retainAll, sort, spliterator, subListparallelStream, removeIf, streampublic PersistentLinkedList(Class<E> type) throws IOException
IOExceptionPersistentCollections.getPersistentBuffer(long)public PersistentLinkedList(Class<E> type, Collection<? extends E> c) throws IOException
IOExceptionpublic PersistentLinkedList(PersistentBuffer pbuffer, Class<E> type) throws IOException
IOExceptionPersistentCollections.getSerializer(java.lang.Class)public PersistentLinkedList(PersistentBuffer pbuffer, Serializer<E> serializer) throws IOException
IOExceptionprotected void checkConsistency(boolean autoCorrect)
throws IOException,
IllegalStateException
autoCorrect - Will correct inconsistencies that arise from an unclean shutdown.
Logs any corrections made to logger with level INFO.IOException - if IO error occurs during checkIllegalStateException - when in an inconsistent state and, if autoCorrect, is uncorrectablepublic E getFirst()
NoSuchElementException - if this list is emptypublic E getLast()
NoSuchElementException - if this list is emptypublic E removeFirst()
NoSuchElementException - if this list is emptypublic E removeLast()
NoSuchElementException - if this list is emptypublic void addFirst(E element)
e - the element to addpublic void addLast(E element)
This method is equivalent to add(E).
e - the element to addpublic boolean contains(Object o)
contains in interface Collection<E>contains in interface List<E>contains in class AbstractCollection<E>o - element whose presence in this list is to be testedpublic boolean remove(Object o)
remove in interface Collection<E>remove in interface List<E>remove in class AbstractCollection<E>o - element to be removed from this list, if presentpublic boolean addAll(Collection<? extends E> c)
addAll in interface Collection<E>addAll in interface List<E>addAll in class AbstractCollection<E>c - collection containing elements to be added to this listNullPointerException - if the specified collection is nullpublic boolean addAll(int index,
Collection<? extends E> c)
addAll in interface List<E>addAll in class AbstractSequentialList<E>index - index at which to insert the first element
from the specified collectionc - collection containing elements to be added to this listIndexOutOfBoundsExceptionNullPointerException - if the specified collection is nullpublic int size()
size in interface Collection<E>size in interface List<E>size in class AbstractCollection<E>public boolean add(E element)
This method is equivalent to addLast(E).
add in interface Collection<E>add in interface List<E>add in interface Queue<E>add in class AbstractList<E>e - element to be appended to this listCollection.add(E))public void clear()
clear in interface Collection<E>clear in interface List<E>clear in class AbstractList<E>public E get(int index)
get in interface List<E>get in class AbstractSequentialList<E>index - index of the element to returnIndexOutOfBoundsExceptionpublic E set(int index, E element)
set in interface List<E>set in class AbstractSequentialList<E>index - index of the element to replaceelement - element to be stored at the specified positionIndexOutOfBoundsExceptionpublic void add(int index,
E element)
add in interface List<E>add in class AbstractSequentialList<E>index - index at which the specified element is to be insertedelement - element to be insertedIndexOutOfBoundsExceptionpublic E remove(int index)
remove in interface List<E>remove in class AbstractSequentialList<E>index - the index of the element to be removedIndexOutOfBoundsExceptionpublic int indexOf(Object o)
public int lastIndexOf(Object o)
lastIndexOf in interface List<E>lastIndexOf in class AbstractList<E>o - element to search forpublic E element()
element in interface Queue<E>NoSuchElementException - if this list is emptypublic E poll()
public E remove()
remove in interface Queue<E>NoSuchElementException - if this list is emptypublic boolean offer(E e)
offer in interface Queue<E>e - the element to addQueue.offer(E))public boolean offerFirst(E e)
e - the element to insertDeque#offerFirst)public boolean offerLast(E e)
e - the element to insertDeque#offerLast)public E peekFirst()
public E peekLast()
public E pollFirst()
public E pollLast()
public void push(E e)
This method is equivalent to addFirst(E).
e - the element to pushpublic E pop()
This method is equivalent to removeFirst().
NoSuchElementException - if this list is emptypublic boolean removeFirstOccurrence(Object o)
o - element to be removed from this list, if presentpublic boolean removeLastOccurrence(Object o)
o - element to be removed from this list, if presentpublic ListIterator<E> listIterator(int index)
The list-iterator is fail-fast: if the list is structurally modified at any time after the Iterator is created, in any way except through the list-iterator's own remove or add methods, the list-iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
listIterator in interface List<E>listIterator in class AbstractSequentialList<E>index - index of the first element to be returned from the
list-iterator (by a call to next)IndexOutOfBoundsExceptionList.listIterator(int)public Object[] toArray()
toArray in interface Collection<E>toArray in interface List<E>toArray in class AbstractCollection<E>public <T> T[] toArray(T[] a)
toArray in interface Collection<E>toArray in interface List<E>toArray in class AbstractCollection<E>protected void finalize()
throws Throwable
public void close()
throws IOException
IOExceptionCopyright © 2000–2016 AO Industries, Inc.. All rights reserved.