Class AbstractLinkedList<E,T extends LinkedListEntry<E,T>>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- net.automatalib.common.smartcollection.AbstractSmartCollection<E>
-
- net.automatalib.common.smartcollection.AbstractLinkedList<E,T>
-
- Type Parameters:
E- element typeT- linked list entry type
- All Implemented Interfaces:
Iterable<E>,Collection<E>,SmartCollection<E>,SmartSequence<E>
- Direct Known Subclasses:
DefaultLinkedList,IntrusiveLinkedList
public abstract class AbstractLinkedList<E,T extends LinkedListEntry<E,T>> extends AbstractSmartCollection<E> implements SmartSequence<E>
Abstract base class for linked lists.This class implements the base functionality for dealing with linked lists of elements implementing the
LinkedListEntryinterface. It provides the logic for the basic operations (esp. the (re-/un-)linking of elements), but not how entries into the lists are created. Therefore, it can be used by both intrusive and non-intrusive linked lists.- See Also:
IntrusiveLinkedList,DefaultLinkedList
-
-
Constructor Summary
Constructors Constructor Description AbstractLinkedList()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected TcastRef(ElementReference ref)Helper function for casting a generalElementReferenceto the specific linked list entry type.Echoose()Retrieves an arbitrary element from the collection.ElementReferencechooseRef()Retrieves the reference to an arbitrary element from the collection.voidclear()voidconcat(AbstractLinkedList<? extends E,? extends T> other)Concatenates two linked lists.Eget(ElementReference ref)Retrieves an element by its reference.EgetBack()Retrieves the last element in the list.protected @Nullable TgetBackEntry()Retrieves the last entry in the list, ornullif the list is empty.@Nullable ElementReferencegetBackReference()Retrieves a reference to the last element in the list.EgetFront()Retrieves the first element in the list.protected @Nullable TgetFrontEntry()Retrieves the first entry in the list, ornullif the list is empty.@Nullable ElementReferencegetFrontReference()Retrieves a reference to the first element in the list.ElementReferenceinsertAfter(E element, ElementReference ref)Inserts the given element after the element referenced by the specified reference.protected voidinsertAfterEntry(T e, T insertPos)Inserts a new entry after a given one.ElementReferenceinsertBefore(E element, ElementReference ref)Inserts the given element before the element referenced by the specified reference.protected voidinsertBeforeEntry(T e, T insertPos)Inserts a new entry before a given one.booleanisEmpty()Iterator<E>iterator()protected abstract TmakeEntry(E element)Creates (if necessary) aLinkedListEntryfor the given element.EpopBack()Retrieves and removes the last element in the list.protected @Nullable TpopBackEntry()Removes and returns the last entry in the list.EpopFront()Retrieves and removes the first element in the list.protected @Nullable TpopFrontEntry()Removes and returns the first entry in the list.@Nullable ElementReferencepred(ElementReference ref)Retrieves the reference to the preceding element, ornullif the given reference references the first element in the list.ElementReferencepushBack(E element)Adds an element at the end of the list.protected voidpushBackEntry(T e)Adds an entry at the end of the list.ElementReferencepushFront(E element)Adds an element at the beginning of the list.protected voidpushFrontEntry(T e)Adds an entry at the beginning of the list.ElementReferencereferencedAdd(E elem)Adds an element to the collection, returning a reference to the newly added element.Iterator<ElementReference>referenceIterator()Retrieves an iterator for iterating over the references of elements in this collection.voidremove(ElementReference elem)Removes an element (by its reference) from the collection.protected voidremoveEntry(T entry)Removes an entry from the list.voidreplace(ElementReference ref, E newElement)Replaces the element referenced by the given reference with the specified element.protected voidreplaceEntry(T oldEntry, T newEntry)Replaces an entry in the list.intsize()@Nullable ElementReferencesucc(ElementReference ref)Retrieves the reference to the succeeding element, ornullif the given reference references the last element in the list.voidswap(AbstractLinkedList<E,T> other)Swaps the contents of two linked lists with the same entry types.-
Methods inherited from class net.automatalib.common.smartcollection.AbstractSmartCollection
add, addAll, addAll, deepClear, find, quickClear, references, remove
-
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, removeAll, retainAll, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
add, addAll, contains, containsAll, equals, hashCode, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray
-
Methods inherited from interface net.automatalib.common.smartcollection.SmartCollection
addAll, addAll, deepClear, find, quickClear, references, remove
-
-
-
-
Method Detail
-
getFrontEntry
protected @Nullable T getFrontEntry()
Retrieves the first entry in the list, ornullif the list is empty.- Returns:
- the first entry or
null.
-
getBackEntry
protected @Nullable T getBackEntry()
Retrieves the last entry in the list, ornullif the list is empty.- Returns:
- the first entry or
null.
-
concat
public void concat(AbstractLinkedList<? extends E,? extends T> other)
Concatenates two linked lists. All elements of the specified list (which will be empty afterward) are added at the end of this list. This operation runs in constant time.- Parameters:
other- the list to append,
-
choose
public E choose()
Description copied from interface:SmartCollectionRetrieves an arbitrary element from the collection.- Specified by:
choosein interfaceSmartCollection<E>- Overrides:
choosein classAbstractSmartCollection<E>- Returns:
- an arbitrary element from the collection
-
chooseRef
public ElementReference chooseRef()
Description copied from interface:SmartCollectionRetrieves the reference to an arbitrary element from the collection. If the collection is empty, aNoSuchElementExceptionis thrown.- Specified by:
chooseRefin interfaceSmartCollection<E>- Overrides:
chooseRefin classAbstractSmartCollection<E>- Returns:
- the reference to an arbitrary element in the collection
-
iterator
public Iterator<E> iterator()
- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Overrides:
iteratorin classAbstractSmartCollection<E>
-
get
public E get(ElementReference ref)
Description copied from interface:SmartCollectionRetrieves an element by its reference.If the reference belongs to another collection, the behavior is undefined.
- Specified by:
getin interfaceSmartCollection<E>- Parameters:
ref- the element's reference.- Returns:
- the element.
-
referencedAdd
public ElementReference referencedAdd(E elem)
Description copied from interface:SmartCollectionAdds an element to the collection, returning a reference to the newly added element. If the collection does not support containing the same element multiple times, a reference to the previously existing element is returned.- Specified by:
referencedAddin interfaceSmartCollection<E>- Parameters:
elem- the element to be added.- Returns:
- a reference to this element in the collection.
-
remove
public void remove(ElementReference elem)
Description copied from interface:SmartCollectionRemoves an element (by its reference) from the collection.If the reference does not belong to this collection, the behavior is undefined.
- Specified by:
removein interfaceSmartCollection<E>- Parameters:
elem- the reference to the element to be removed.
-
removeEntry
protected void removeEntry(T entry)
Removes an entry from the list.- Parameters:
entry- the entry to remove.
-
referenceIterator
public Iterator<ElementReference> referenceIterator()
Description copied from interface:SmartCollectionRetrieves an iterator for iterating over the references of elements in this collection.- Specified by:
referenceIteratorin interfaceSmartCollection<E>- Returns:
- the reference iterator.
-
replace
public void replace(ElementReference ref, E newElement)
Description copied from interface:SmartCollectionReplaces the element referenced by the given reference with the specified element.- Specified by:
replacein interfaceSmartCollection<E>- Parameters:
ref- the reference of the element to be replaced.newElement- the replacement.
-
replaceEntry
protected void replaceEntry(T oldEntry, T newEntry)
Replaces an entry in the list.- Parameters:
oldEntry- the entry to be replaced.newEntry- the replacement entry.
-
makeEntry
protected abstract T makeEntry(E element)
Creates (if necessary) aLinkedListEntryfor the given element. For intrusive linked lists, e.g., the argument itself is returned.- Parameters:
element- the element for which to retrieve an entry.- Returns:
- the entry for the given element.
-
pushBackEntry
protected void pushBackEntry(T e)
Adds an entry at the end of the list.- Parameters:
e- the entry to add.
-
size
public int size()
- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>
-
isEmpty
@EnsuresQualifierIf(qualifier=org.checkerframework.checker.nullness.qual.NonNull.class, expression={"this.head","this.last"}, result=false) public boolean isEmpty()
- Specified by:
isEmptyin interfaceCollection<E>- Overrides:
isEmptyin classAbstractCollection<E>
-
clear
public void clear()
- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractCollection<E>
-
getBack
public E getBack()
Retrieves the last element in the list. If the list is empty, aNoSuchElementExceptionwill be thrown.- Returns:
- the last element in the list.
-
getBackReference
public @Nullable ElementReference getBackReference()
Retrieves a reference to the last element in the list. If the list is empty,nullis returned.- Returns:
- a reference to the last element, or
null.
-
getFront
public E getFront()
Retrieves the first element in the list. If the list is empty, aNoSuchElementExceptionwill be thrown- Returns:
- the first element in the list.
-
getFrontReference
public @Nullable ElementReference getFrontReference()
Retrieves a reference to the first element in the list. If the list is empty,nullis returned.- Returns:
- a reference to the first element, or
null.
-
popBack
public E popBack()
Retrieves and removes the last element in the list. If the list is empty, aNullPointerExceptionmay be thrown.- Returns:
- the formerly last element in the list.
-
popBackEntry
protected @Nullable T popBackEntry()
Removes and returns the last entry in the list. If the list is empty, it remains unmodified andnullis returned.- Returns:
- the previously first entry in the list, or
null.
-
popFront
public E popFront()
Retrieves and removes the first element in the list. If the list is empty, aNullPointerExceptionmay be thrown.- Returns:
- the formerly first element in the list.
-
popFrontEntry
protected @Nullable T popFrontEntry()
Removes and returns the first entry in the list. If the list is empty, it remains unmodified andnullis returned.- Returns:
- the previously first entry in the list, or
null.
-
pushBack
public ElementReference pushBack(E element)
Adds an element at the end of the list.- Parameters:
element- the element to add.- Returns:
- a reference to the newly added element.
-
pushFront
public ElementReference pushFront(E element)
Adds an element at the beginning of the list.- Parameters:
element- the element to add.- Returns:
- a reference to the newly added element.
-
pushFrontEntry
protected void pushFrontEntry(T e)
Adds an entry at the beginning of the list.- Parameters:
e- the entry to add.
-
pred
public @Nullable ElementReference pred(ElementReference ref)
Description copied from interface:SmartSequenceRetrieves the reference to the preceding element, ornullif the given reference references the first element in the list.- Specified by:
predin interfaceSmartSequence<E>- Parameters:
ref- the reference- Returns:
- the reference to the preceding element
-
castRef
protected T castRef(ElementReference ref)
Helper function for casting a generalElementReferenceto the specific linked list entry type.- Parameters:
ref- the reference.- Returns:
- the argument cast to the entry type.
-
succ
public @Nullable ElementReference succ(ElementReference ref)
Description copied from interface:SmartSequenceRetrieves the reference to the succeeding element, ornullif the given reference references the last element in the list.- Specified by:
succin interfaceSmartSequence<E>- Parameters:
ref- the reference- Returns:
- the reference to the succeeding element
-
insertBefore
public ElementReference insertBefore(E element, ElementReference ref)
Description copied from interface:SmartSequenceInserts the given element before the element referenced by the specified reference.- Specified by:
insertBeforein interfaceSmartSequence<E>- Parameters:
element- the element to be added.ref- reference to the element before which the new element is to be inserted.- Returns:
- reference to the newly added element.
-
insertBeforeEntry
protected void insertBeforeEntry(T e, T insertPos)
Inserts a new entry before a given one.- Parameters:
e- the entry to add.insertPos- the entry before which to add the new one.
-
insertAfter
public ElementReference insertAfter(E element, ElementReference ref)
Description copied from interface:SmartSequenceInserts the given element after the element referenced by the specified reference.- Specified by:
insertAfterin interfaceSmartSequence<E>- Parameters:
element- the element to be added.ref- reference to the element after which the new element is to be inserted.- Returns:
- reference to the newly added element.
-
insertAfterEntry
protected void insertAfterEntry(T e, T insertPos)
Inserts a new entry after a given one.- Parameters:
e- the entry to add.insertPos- the entry before which to add the new one.
-
swap
public void swap(AbstractLinkedList<E,T> other)
Swaps the contents of two linked lists with the same entry types. This method runs in constant time.- Parameters:
other- the other list to swap contents with.
-
-