- java.lang.Object
-
- fr.dufrenoy.util.SynchronizedChunkyList<E>
-
- Type Parameters:
E- the type of elements in this list
- All Implemented Interfaces:
ChunkyList<E>,Serializable,Iterable<E>,Collection<E>,List<E>
public class SynchronizedChunkyList<E> extends Object implements ChunkyList<E>, Serializable
A thread-safe implementation ofChunkyList, backed by anUnsynchronizedChunkyListand protected by aReentrantReadWriteLock.Multiple threads may read concurrently. Write operations are exclusive: a write blocks until all ongoing reads have completed, and blocks any new reads until the write is done.
The iterators returned by this class are fail-safe:
iterator(),listIterator(),spliterator(), andCollection.stream()operate on a snapshot of the list taken at the time of the call, under a read lock. Subsequent modifications to the list are not reflected in the snapshot.Memory note: snapshot-based operations (
spliterator(),Collection.stream(),Collection.parallelStream(),iterator(),listIterator()) copy the entire list at the time of the call. Avoid calling them on very large lists in memory-constrained environments.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface fr.dufrenoy.util.ChunkyList
ChunkyList.GrowingStrategy, ChunkyList.ShrinkingStrategy
-
-
Constructor Summary
Constructors Constructor Description SynchronizedChunkyList()Creates a newSynchronizedChunkyListwith the default chunk size.SynchronizedChunkyList(int chunkSize)Creates a newSynchronizedChunkyListwith the given chunk size.SynchronizedChunkyList(int chunkSize, SynchronizedChunkyList<? extends E> other)Creates a copy of the givenSynchronizedChunkyListwith a different chunk size, preserving both strategies.SynchronizedChunkyList(int chunkSize, Collection<? extends E> c)Creates a newSynchronizedChunkyListwith the given chunk size containing all elements of the given collection, in the order returned by its iterator.SynchronizedChunkyList(SynchronizedChunkyList<? extends E> other)Creates a faithful copy of the givenSynchronizedChunkyList, preserving its chunk size, both strategies, and the internal chunk structure.SynchronizedChunkyList(Collection<? extends E> c)Creates a newSynchronizedChunkyListwith the default chunk size containing all elements of the given collection, in the order returned by its iterator.
-
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()booleancontains(Object o)booleancontainsAll(Collection<?> c)booleanequals(Object o)voidforEach(Consumer<? super E> action)Eget(int index)intgetChunkSize()Returns the Chunksize, which is final, so no need for locking.ChunkyList.GrowingStrategygetCurrentGrowingStrategy()Returns the current growing strategy.ChunkyList.ShrinkingStrategygetCurrentShrinkingStrategy()Returns the current shrinking strategy.inthashCode()intindexOf(Object o)booleanisEmpty()Iterator<E>iterator()Returns an iterator over a snapshot of this list taken under a read lock.intlastIndexOf(Object o)ListIterator<E>listIterator()Returns a list iterator over a snapshot of this list taken under a read lock.ListIterator<E>listIterator(int index)Returns a list iterator over a snapshot of this list taken under a read lock, starting at the given index.Eremove(int index)booleanremove(Object o)booleanremoveAll(Collection<?> c)booleanremoveIf(Predicate<? super E> filter)voidreorganize()Reorganizes the list by redistributing all elements into full chunks, holding the write lock for the entire duration of the operation.voidreorganize(boolean blocking)Reorganizes the list by redistributing all elements into full chunks.voidreplaceAll(UnaryOperator<E> operator)booleanretainAll(Collection<?> c)Eset(int index, E element)voidsetCurrentGrowingStrategy(ChunkyList.GrowingStrategy growingStrategy)Sets the growing strategy.voidsetCurrentShrinkingStrategy(ChunkyList.ShrinkingStrategy shrinkingStrategy)Sets the shrinking strategy.voidsetStrategies(ChunkyList.GrowingStrategy growingStrategy, ChunkyList.ShrinkingStrategy shrinkingStrategy)Sets both strategies atomically under a single write lock, guaranteeing that no operation can observe an inconsistent intermediate state.intsize()voidsort(Comparator<? super E> c)Spliterator<E>spliterator()Returns aSpliteratorover a snapshot of this list taken under a read lock.List<E>subList(int fromIndex, int toIndex)Object[]toArray()<T> T[]toArray(T[] a)StringtoString()-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, stream, toArray
-
-
-
-
Constructor Detail
-
SynchronizedChunkyList
public SynchronizedChunkyList()
Creates a newSynchronizedChunkyListwith the default chunk size.
-
SynchronizedChunkyList
public SynchronizedChunkyList(int chunkSize)
Creates a newSynchronizedChunkyListwith the given chunk size.- Parameters:
chunkSize- the number of elements per chunk; must be at least 1
-
SynchronizedChunkyList
public SynchronizedChunkyList(SynchronizedChunkyList<? extends E> other)
Creates a faithful copy of the givenSynchronizedChunkyList, preserving its chunk size, both strategies, and the internal chunk structure.A read lock is acquired on
otherfor the duration of the copy, guaranteeing a consistent snapshot.- Parameters:
other- the list to copy
-
SynchronizedChunkyList
public SynchronizedChunkyList(int chunkSize, SynchronizedChunkyList<? extends E> other)Creates a copy of the givenSynchronizedChunkyListwith a different chunk size, preserving both strategies.A read lock is acquired on
otherfor the duration of the copy, guaranteeing a consistent snapshot.- Parameters:
chunkSize- the new chunk size; must be at least 1other- the list to copy
-
SynchronizedChunkyList
public SynchronizedChunkyList(Collection<? extends E> c)
Creates a newSynchronizedChunkyListwith the default chunk size containing all elements of the given collection, in the order returned by its iterator.- Parameters:
c- the collection whose elements are to be placed into this list
-
SynchronizedChunkyList
public SynchronizedChunkyList(int chunkSize, Collection<? extends E> c)Creates a newSynchronizedChunkyListwith the given chunk size containing all elements of the given collection, in the order returned by its iterator.- Parameters:
chunkSize- the number of elements per chunk; must be at least 1c- the collection whose elements are to be placed into this list
-
-
Method Detail
-
getChunkSize
public int getChunkSize()
Returns the Chunksize, which is final, so no need for locking.- Specified by:
getChunkSizein interfaceChunkyList<E>- Returns:
- the chunk size of this list
-
getCurrentGrowingStrategy
public ChunkyList.GrowingStrategy getCurrentGrowingStrategy()
Description copied from interface:ChunkyListReturns the current growing strategy.- Specified by:
getCurrentGrowingStrategyin interfaceChunkyList<E>- Returns:
- the current growing strategy
- See Also:
ChunkyList.GrowingStrategy
-
setCurrentGrowingStrategy
public void setCurrentGrowingStrategy(ChunkyList.GrowingStrategy growingStrategy)
Description copied from interface:ChunkyListSets the growing strategy. The new strategy applies immediately to all subsequent insertions.- Specified by:
setCurrentGrowingStrategyin interfaceChunkyList<E>- Parameters:
growingStrategy- the new growing strategy- See Also:
ChunkyList.GrowingStrategy
-
getCurrentShrinkingStrategy
public ChunkyList.ShrinkingStrategy getCurrentShrinkingStrategy()
Description copied from interface:ChunkyListReturns the current shrinking strategy.- Specified by:
getCurrentShrinkingStrategyin interfaceChunkyList<E>- Returns:
- the current shrinking strategy
- See Also:
ChunkyList.ShrinkingStrategy
-
setCurrentShrinkingStrategy
public void setCurrentShrinkingStrategy(ChunkyList.ShrinkingStrategy shrinkingStrategy)
Description copied from interface:ChunkyListSets the shrinking strategy. The new strategy applies immediately to all subsequent removals.- Specified by:
setCurrentShrinkingStrategyin interfaceChunkyList<E>- Parameters:
shrinkingStrategy- the new shrinking strategy- See Also:
ChunkyList.ShrinkingStrategy
-
setStrategies
public void setStrategies(ChunkyList.GrowingStrategy growingStrategy, ChunkyList.ShrinkingStrategy shrinkingStrategy)
Sets both strategies atomically under a single write lock, guaranteeing that no operation can observe an inconsistent intermediate state.- Specified by:
setStrategiesin interfaceChunkyList<E>- Parameters:
growingStrategy- the new growing strategyshrinkingStrategy- the new shrinking strategy
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
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>
-
add
public boolean add(E e)
-
addAll
public boolean addAll(Collection<? extends E> c)
-
addAll
public boolean addAll(int index, Collection<? extends E> c)
-
remove
public boolean remove(Object o)
-
removeAll
public boolean removeAll(Collection<?> c)
-
removeIf
public boolean removeIf(Predicate<? super E> filter)
- Specified by:
removeIfin interfaceCollection<E>
-
retainAll
public boolean retainAll(Collection<?> c)
-
replaceAll
public void replaceAll(UnaryOperator<E> operator)
- Specified by:
replaceAllin interfaceList<E>
-
sort
public void sort(Comparator<? super E> c)
-
clear
public void clear()
-
toArray
public Object[] toArray()
-
toArray
public <T> T[] toArray(T[] a)
-
iterator
public Iterator<E> iterator()
Returns an iterator over a snapshot of this list taken under a read lock.Memory note: this operation copies the entire list.
-
listIterator
public ListIterator<E> listIterator()
Returns a list iterator over a snapshot of this list taken under a read lock.Memory note: this operation copies the entire list.
- Specified by:
listIteratorin interfaceList<E>
-
listIterator
public ListIterator<E> listIterator(int index)
Returns a list iterator over a snapshot of this list taken under a read lock, starting at the given index.Memory note: this operation copies the entire list.
- Specified by:
listIteratorin interfaceList<E>
-
spliterator
public Spliterator<E> spliterator()
Returns aSpliteratorover a snapshot of this list taken under a read lock. The spliterator isSpliterator.ORDERED,Spliterator.SIZED, andSpliterator.SUBSIZED.Memory note: this operation copies the entire list.
- Specified by:
spliteratorin interfaceCollection<E>- Specified by:
spliteratorin interfaceIterable<E>- Specified by:
spliteratorin interfaceList<E>
-
reorganize
public void reorganize()
Reorganizes the list by redistributing all elements into full chunks, holding the write lock for the entire duration of the operation.No reads or writes can proceed while this method is running. This is the safe default; prefer it unless blocking is a concern.
- Specified by:
reorganizein interfaceChunkyList<E>- See Also:
reorganize(boolean)
-
reorganize
public void reorganize(boolean blocking)
Reorganizes the list by redistributing all elements into full chunks.- If
blockingistrue, behaves identically toreorganize(): the write lock is held for the entire operation. - If
blockingisfalse, uses a snapshot strategy: a copy of the list is taken under a read lock, reorganized without any lock, then swapped in under a write lock.Warning: any modifications made to the list between the snapshot and the final swap are silently lost. Use this mode only when the list is known to be quiescent or when occasional data loss is acceptable.
- Parameters:
blocking- iftrue, holds the write lock for the full duration; iffalse, uses a non-blocking snapshot strategy
- If
-
equals
public boolean equals(Object o)
-
hashCode
public int hashCode()
-
-