- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- fr.dufrenoy.util.UnsynchronizedChunkyList<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 UnsynchronizedChunkyList<E> extends AbstractList<E> implements ChunkyList<E>, Serializable
An unrolled linked list implementation ofChunkyList, backed by a chain of fixed-size arrays (Chunks).This implementation is not thread-safe. If multiple threads access an
UnsynchronizedChunkyListinstance concurrently, and at least one of them modifies it structurally, it must be synchronized externally. A structural modification is any operation that adds or removes one or more elements, or explicitly resizes the list; merely setting the value of an element is not a structural modification.For concurrent access, it is recommended to use
SynchronizedChunkyList, or to wrap the list at creation time:List list = Collections.synchronizedList(new UnsynchronizedChunkyList(...));The iterators returned by this class are fail-fast: if the list is structurally modified after the iterator is created, the iterator will throw a
ConcurrentModificationException. This behaviour cannot be guaranteed in a concurrent context without external synchronization.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface fr.dufrenoy.util.ChunkyList
ChunkyList.GrowingStrategy, ChunkyList.ShrinkingStrategy
-
-
Field Summary
-
Fields inherited from class java.util.AbstractList
modCount
-
-
Constructor Summary
Constructors Constructor Description UnsynchronizedChunkyList()Creates a newUnsynchronizedChunkyListwith the default chunk size.UnsynchronizedChunkyList(int chunkSize)Creates a newUnsynchronizedChunkyListwith the given chunk size.UnsynchronizedChunkyList(int chunkSize, UnsynchronizedChunkyList<? extends E> other)Creates a copy of the givenUnsynchronizedChunkyListwith a different chunk size, preserving both strategies.UnsynchronizedChunkyList(int chunkSize, Collection<? extends E> c)Creates a newUnsynchronizedChunkyListwith the given chunk size containing all elements of the given collection, in the order returned by its iterator.UnsynchronizedChunkyList(UnsynchronizedChunkyList<? extends E> other)Creates a faithful copy of the givenUnsynchronizedChunkyList, preserving its chunk size (not the default chunk size), both strategies, and the internal chunk structure.UnsynchronizedChunkyList(Collection<? extends E> c)Creates a newUnsynchronizedChunkyListwith 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(Collection<? extends E> c)Appends all elements of the given collection to the end of this list, in the order returned by the collection's iterator.voidclear()booleancontains(Object o)Eget(int index)intgetChunkSize()Returns the chunk size of this list.ChunkyList.GrowingStrategygetCurrentGrowingStrategy()Returns the current growing strategy.ChunkyList.ShrinkingStrategygetCurrentShrinkingStrategy()Returns the current shrinking strategy.intindexOf(Object o)booleanisEmpty()Iterator<E>iterator()intlastIndexOf(Object o)Eremove(int index)booleanremove(Object o)voidreorganize()Reorganizes the list by redistributing all elements into chunks of exactlychunkSizeelements (except possibly the last one).Eset(int index, E element)voidsetCurrentGrowingStrategy(ChunkyList.GrowingStrategy currentGrowingStrategy)Sets the growing strategy.voidsetCurrentShrinkingStrategy(ChunkyList.ShrinkingStrategy currentShrinkingStrategy)Sets the shrinking strategy.voidsetStrategies(ChunkyList.GrowingStrategy growingStrategy, ChunkyList.ShrinkingStrategy shrinkingStrategy)Sets both strategies atomically.intsize()Spliterator<E>spliterator()-
Methods inherited from class java.util.AbstractList
addAll, equals, hashCode, listIterator, listIterator, removeRange, subList
-
Methods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.List
addAll, containsAll, equals, hashCode, listIterator, listIterator, removeAll, replaceAll, retainAll, sort, subList, toArray, toArray
-
-
-
-
Constructor Detail
-
UnsynchronizedChunkyList
public UnsynchronizedChunkyList()
Creates a newUnsynchronizedChunkyListwith the default chunk size.
-
UnsynchronizedChunkyList
public UnsynchronizedChunkyList(int chunkSize)
Creates a newUnsynchronizedChunkyListwith the given chunk size.- Parameters:
chunkSize- the number of elements per chunk; must be at least 1- Throws:
IllegalArgumentException- ifchunkSizeis less than 1
-
UnsynchronizedChunkyList
public UnsynchronizedChunkyList(UnsynchronizedChunkyList<? extends E> other)
Creates a faithful copy of the givenUnsynchronizedChunkyList, preserving its chunk size (not the default chunk size), both strategies, and the internal chunk structure.- Parameters:
other- the list to copy
-
UnsynchronizedChunkyList
public UnsynchronizedChunkyList(int chunkSize, UnsynchronizedChunkyList<? extends E> other)Creates a copy of the givenUnsynchronizedChunkyListwith a different chunk size, preserving both strategies. Chunks whose number of elements does not exceed the new chunk size are copied as-is. Chunks that exceed the new chunk size are split according to the currentChunkyList.GrowingStrategy.- Parameters:
chunkSize- the new chunk size; must be at least 1other- the list to copy
-
UnsynchronizedChunkyList
public UnsynchronizedChunkyList(Collection<? extends E> c)
Creates a newUnsynchronizedChunkyListwith 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
-
UnsynchronizedChunkyList
public UnsynchronizedChunkyList(int chunkSize, Collection<? extends E> c)Creates a newUnsynchronizedChunkyListwith 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()
Description copied from interface:ChunkyListReturns the chunk size of this list.- Specified by:
getChunkSizein interfaceChunkyList<E>- Returns:
- the chunk size
-
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 currentGrowingStrategy)
Description copied from interface:ChunkyListSets the growing strategy. The new strategy applies immediately to all subsequent insertions.- Specified by:
setCurrentGrowingStrategyin interfaceChunkyList<E>- Parameters:
currentGrowingStrategy- 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 currentShrinkingStrategy)
Description copied from interface:ChunkyListSets the shrinking strategy. The new strategy applies immediately to all subsequent removals.- Specified by:
setCurrentShrinkingStrategyin interfaceChunkyList<E>- Parameters:
currentShrinkingStrategy- the new shrinking strategy- See Also:
ChunkyList.ShrinkingStrategy
-
setStrategies
public void setStrategies(ChunkyList.GrowingStrategy growingStrategy, ChunkyList.ShrinkingStrategy shrinkingStrategy)
Description copied from interface:ChunkyListSets both strategies atomically. Prefer this method over callingChunkyList.setCurrentGrowingStrategy(fr.dufrenoy.util.ChunkyList.GrowingStrategy)andChunkyList.setCurrentShrinkingStrategy(fr.dufrenoy.util.ChunkyList.ShrinkingStrategy)separately when both need to change together, as it guarantees 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()
- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein classAbstractCollection<E>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceList<E>- Overrides:
isEmptyin classAbstractCollection<E>
-
contains
public boolean contains(Object o)
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceList<E>- Overrides:
containsin classAbstractCollection<E>
-
indexOf
public int indexOf(Object o)
-
lastIndexOf
public int lastIndexOf(Object o)
- Specified by:
lastIndexOfin interfaceList<E>- Overrides:
lastIndexOfin classAbstractList<E>
-
get
public E get(int index)
-
add
public boolean add(E e)
- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>
-
addAll
public boolean addAll(Collection<? extends E> c)
Appends all elements of the given collection to the end of this list, in the order returned by the collection's iterator.This implementation converts the collection to an array via
Collection.toArray(), then fills the last existing chunk and creates new full chunks usingSystem.arraycopy(java.lang.Object, int, java.lang.Object, int, int), avoiding the per-element overhead of repeatedadd(Object)calls.- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractCollection<E>- Parameters:
c- the collection whose elements are to be appended- Returns:
trueif the list was modified- Throws:
IllegalArgumentException- if any element incisnull
-
add
public void add(int index, E element)
-
remove
public boolean remove(Object o)
- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractCollection<E>
-
remove
public E remove(int index)
-
reorganize
public void reorganize()
Reorganizes the list by redistributing all elements into chunks of exactlychunkSizeelements (except possibly the last one). The order of elements is preserved.This is useful after many removals have left chunks sparsely filled.
- Specified by:
reorganizein interfaceChunkyList<E>
-
clear
public void clear()
- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>
-
spliterator
public Spliterator<E> spliterator()
- Specified by:
spliteratorin interfaceCollection<E>- Specified by:
spliteratorin interfaceIterable<E>- Specified by:
spliteratorin interfaceList<E>
-
-