- java.lang.Object
-
- fr.dufrenoy.util.SynchronizedTreeList<E>
-
- Type Parameters:
E- the type of elements maintained by this list
- All Implemented Interfaces:
TreeList<E>,Serializable,Iterable<E>,Collection<E>,List<E>
public class SynchronizedTreeList<E> extends Object implements TreeList<E>, Serializable
A thread-safe implementation ofTreeListthat delegates to an innerUnsynchronizedTreeListprotected by aReentrantReadWriteLock.Multiple threads may read concurrently; write operations acquire an exclusive write lock. All operations are therefore safe for concurrent use without external synchronization.
iterator(),listIterator(), andlistIterator(int)return snapshot-based iterators: a copy of the list is taken under a read lock at the time the iterator is created. Subsequent modifications to the list are not reflected in the iterator, and the iterator never throwsConcurrentModificationException.The unsupported operations of
TreeList(add(int, Object),set(int, Object)) throwUnsupportedOperationExceptionas documented.subList(int, int)returns a snapshot-basedTreeList: a copy of the elements in the specified range is taken under a read lock. The returned list is an independentUnsynchronizedTreeList— it is not a live view and modifications do not affect the original list.- Version:
- 1.0
- Author:
- Dufrenoy
- See Also:
TreeList,UnsynchronizedTreeList, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description SynchronizedTreeList()Constructs an emptySynchronizedTreeListordered by the natural ordering of its elements.SynchronizedTreeList(Collection<? extends E> c)Constructs aSynchronizedTreeListcontaining the elements of the given collection, ordered by their natural ordering.SynchronizedTreeList(Comparator<? super E> comparator)Constructs an emptySynchronizedTreeListordered by the given comparator.SynchronizedTreeList(Comparator<? super E> comparator, Collection<? extends E> c)Constructs aSynchronizedTreeListcontaining the elements of the given collection, ordered by the given comparator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int index, E element)booleanadd(E e)booleanaddAll(int index, Collection<? extends E> c)booleanaddAll(Collection<? extends E> c)voidclear()Optional<Comparator<? super E>>comparator()Returns the comparator used to order the elements in this list, or an emptyOptionalif the elements are ordered by their natural ordering.booleancontains(Object o)booleancontainsAll(Collection<?> c)booleanequals(Object o)Compares the specified object with this list for equality.Eget(int index)inthashCode()Returns the hash code value for this list.intindexOf(Object o)booleanisEmpty()Iterator<E>iterator()Returns a snapshot-based iterator over the elements in this list in sorted order.intlastIndexOf(Object o)ListIterator<E>listIterator()ListIterator<E>listIterator(int index)Returns a snapshot-based list iterator over the elements in this list in sorted order, starting at the specified position.Eremove(int index)booleanremove(Object o)booleanremoveAll(Collection<?> c)booleanretainAll(Collection<?> c)Eset(int index, E element)intsize()TreeList<E>subList(int fromIndex, int toIndex)Returns a snapshot of the specified range as an independentUnsynchronizedTreeList.Object[]toArray()<T> T[]toArray(T[] a)-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.List
replaceAll, sort, spliterator
-
-
-
-
Constructor Detail
-
SynchronizedTreeList
public SynchronizedTreeList()
Constructs an emptySynchronizedTreeListordered by the natural ordering of its elements.
-
SynchronizedTreeList
public SynchronizedTreeList(Comparator<? super E> comparator)
Constructs an emptySynchronizedTreeListordered by the given comparator.- Parameters:
comparator- the comparator used to order elements, ornullto use natural ordering
-
SynchronizedTreeList
public SynchronizedTreeList(Collection<? extends E> c)
Constructs aSynchronizedTreeListcontaining the elements of the given collection, ordered by their natural ordering. Duplicate elements are silently discarded.- Parameters:
c- the collection whose elements are to be placed into this list- Throws:
ClassCastException- if any element is not mutually comparableNullPointerException- ifcisnull, or if any element ofcisnull
-
SynchronizedTreeList
public SynchronizedTreeList(Comparator<? super E> comparator, Collection<? extends E> c)
Constructs aSynchronizedTreeListcontaining the elements of the given collection, ordered by the given comparator. Duplicate elements are silently discarded.- Parameters:
comparator- the comparator used to order elements, ornullto use natural orderingc- the collection whose elements are to be placed into this list- Throws:
ClassCastException- if any element is not mutually comparableNullPointerException- ifcisnull, or if any element ofcisnull
-
-
Method Detail
-
comparator
public Optional<Comparator<? super E>> comparator()
Returns the comparator used to order the elements in this list, or an emptyOptionalif the elements are ordered by their natural ordering.- Specified by:
comparatorin interfaceTreeList<E>- Returns:
- the comparator used to order this list, or an empty
Optionalif natural ordering is used
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
add
public boolean add(E e)
-
add
public void add(int index, E element)- Specified by:
addin interfaceList<E>- Throws:
UnsupportedOperationException- always
-
addAll
public boolean addAll(Collection<? extends E> c)
-
addAll
public boolean addAll(int index, Collection<? extends E> c)- Specified by:
addAllin interfaceList<E>- Throws:
UnsupportedOperationException- always (positional insertion is not supported)
-
remove
public boolean remove(Object o)
-
removeAll
public boolean removeAll(Collection<?> c)
-
retainAll
public boolean retainAll(Collection<?> c)
-
clear
public void clear()
-
set
public E set(int index, E element)
- Specified by:
setin interfaceList<E>- Throws:
UnsupportedOperationException- always
-
subList
public TreeList<E> subList(int fromIndex, int toIndex)
Returns a snapshot of the specified range as an independentUnsynchronizedTreeList. The snapshot is taken under a read lock and is not a live view — modifications to the returned list do not affect this list, and vice versa.This is consistent with the snapshot-based iterator pattern used throughout
SynchronizedTreeList.- Specified by:
subListin interfaceList<E>- Specified by:
subListin interfaceTreeList<E>- Parameters:
fromIndex- low endpoint (inclusive) of the subListtoIndex- high endpoint (exclusive) of the subList- Returns:
- a snapshot
TreeListcontaining the elements in the range - Throws:
IndexOutOfBoundsException- iffromIndex < 0ortoIndex > size()IllegalArgumentException- iffromIndex > toIndex
-
contains
public boolean contains(Object o)
-
containsAll
public boolean containsAll(Collection<?> c)
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceList<E>
-
lastIndexOf
public int lastIndexOf(Object o)
- Specified by:
lastIndexOfin interfaceList<E>
-
toArray
public Object[] toArray()
-
toArray
public <T> T[] toArray(T[] a)
-
equals
public boolean equals(Object o)
Compares the specified object with this list for equality. Two lists are equal if they contain the same elements in the same order. Delegates to the innerUnsynchronizedTreeListunder a read lock.
-
hashCode
public int hashCode()
Returns the hash code value for this list. Delegates to the innerUnsynchronizedTreeListunder a read lock.
-
iterator
public Iterator<E> iterator()
Returns a snapshot-based iterator over the elements in this list in sorted order. The snapshot is taken under a read lock at the time this method is called. Subsequent modifications to the list are not reflected in the iterator.The iterator does not support
Iterator.remove().
-
listIterator
public ListIterator<E> listIterator(int index)
Returns a snapshot-based list iterator over the elements in this list in sorted order, starting at the specified position. The snapshot is taken under a read lock at the time this method is called.ListIterator.add(Object)andListIterator.set(Object)are not supported and will throwUnsupportedOperationException.- Specified by:
listIteratorin interfaceList<E>- Parameters:
index- index of the first element to be returned byListIterator.next()- Returns:
- a snapshot list iterator over the elements in sorted order
- Throws:
IndexOutOfBoundsException- ifindex < 0 || index > size()
-
listIterator
public ListIterator<E> listIterator()
- Specified by:
listIteratorin interfaceList<E>
-
-