Class CyclicArrayList<E>
- java.lang.Object
-
- eu.simuline.util.CyclicArrayList<E>
-
- Type Parameters:
E- the class of the elements in this list.
- All Implemented Interfaces:
CyclicList<E>,java.lang.Cloneable,java.lang.Iterable<E>,java.util.Collection<E>
public final class CyclicArrayList<E> extends java.lang.Object implements CyclicList<E>, java.lang.Cloneable
Resizable-array implementation of theCyclicListinterface. Implements all optional operations, and permits all elements, includingnull. In addition to implementing theCyclicListinterface, this class provides methods to manipulate the size of the array that is used internally to store the list.The
size,isEmpty,get,set, anditeratoroperations run in constant time. Theaddoperation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking).Each
CyclicArrayListinstance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.An application can increase the capacity of a
CyclicArrayListinstance before adding a large number of elements using theensureCapacityoperation. This may reduce the amount of incremental reallocation.Note that this implementation is not synchronized. if Multiple threads access an
CyclicArrayListinstance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list.The iterator returned by this class's
iteratoranditerator(int)methods are fail-fast: if list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw aConcurrentModificationException. 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.- Version:
- 1.0
- Author:
- Ernst Reissner
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCyclicArrayList.CyclicArrayIterator<E>An iterator over aCyclicList.(package private) static classCyclicArrayList.StateIterEnumeration of the state of an iterator.
-
Constructor Summary
Constructors Constructor Description CyclicArrayList()Creates a new emptyCyclicArrayList.CyclicArrayList(E[] list)Creates a newCyclicArrayListsuch thatnew CyclicArrayList(list).get(i) == list.get(i)for all indicesifor which the right hand side is valid.CyclicArrayList(CyclicList<? extends E> other)Copy constructor.CyclicArrayList(java.util.List<? extends E> list)Creates a newCyclicArrayListsuch thatnew CyclicArrayList(list).get(i) == list.get(i)for all indicesifor which the right hand side is valid.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int index, E element)Inserts the specified element at the specified position in this list.booleanadd(E element)voidaddAll(int index, java.util.Iterator<E> iter)Inserts the cyclic list of the specified iterator at the specified position in this list (optional operation).voidaddAll(int index, java.util.List<? extends E> addList)Inserts the specified list at the given position in this cyclic list. ***** done for collections!booleanaddAll(java.util.Collection<? extends E> coll)java.util.List<E>asList()java.util.List<E>asList(int index)Returns a List containing all of the elements in this cyclic list in proper sequence.voidclear()Removes all of the elements from this list (optional operation).CyclicArrayList<E>clone()Returns a clone of thisCyclicArrayList.booleancontains(java.lang.Object obj)Returnstrueif this list contains the specified element.booleancontainsAll(java.util.Collection<?> coll)CyclicArrayList<E>cycle(int index)Returns a cyclic permutationpof this cyclic list.CyclicIterator<E>cyclicIterator(int index)Returns aCyclicIteratorof the elements in this list (in proper sequence), starting at the specified position in this list.booleanequals(java.lang.Object obj)Compares the specified object with this cyclic list for equality.booleanequalsCyclic(java.lang.Object obj)Compares the specified object with this cyclic list for equality.Eget(int index)Returns the element at the specified position in this list, provided this list is not empty.CyclicList<E>getCopy(int len)Returns aCyclicListwhich is by copying this list step by step such that the length of the result islen.intgetIndexOf(int idx, java.lang.Object obj)Returns the non-negative index in this cyclic list of the first occurrence of the specified element, or-1if this cyclic list does not contain this element.CyclicList<E>getInverse()Returns the inverse of this cyclic list: the list with inverse order.inthashCode()Returns the hash code value for this cyclic list.inthashCodeCyclic()Returns a hash code value for this cyclic list which is invariant under cyclic permutation.booleanisEmpty()Returnstrueiff this list contains no elements.java.util.Iterator<E>iterator()ReturnscyclicIterator(index)for some unspecifiedindex.Eremove(int index)Removes the element at the specified position in this list (optional operation).booleanremove(java.lang.Object obj)booleanremoveAll(java.util.Collection<?> coll)voidreplace(int index, java.util.Iterator<E> iter)Replaces the element at the specified position in this list with the cyclic list of the specified iterator (optional operation).voidreplace(int index, java.util.List<E> list)Replaces the element at the specified position in this list with the specified list (optional operation).booleanretainAll(java.util.Collection<?> coll)Eset(int index, E element)Replaces the element at the specified position in this list with the specified element (optional operation), provided this list is not empty.intshiftIndex(int index)Returns the number which equalsindexmodulothis.size(), provided this list is not empty.intshiftIndex(int index, int size)intsize()Returns the number of elements in this list.java.lang.Object[]toArray()java.lang.Object[]toArray(int index)Returns an array containing all of the elements in this list in proper sequence.<E> E[]toArray(int index, E[] ret)Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array.<E> E[]toArray(E[] ret)java.lang.StringtoString()-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
-
-
-
Field Detail
-
list
private final java.util.List<E> list
The array this implementation of CyclicList is based on. it satisfiesthis.get(i) == list.get(i)for all indicesifor which the right hand side is valid.
-
-
Constructor Detail
-
CyclicArrayList
public CyclicArrayList()
Creates a new emptyCyclicArrayList.
-
CyclicArrayList
public CyclicArrayList(E[] list)
Creates a newCyclicArrayListsuch thatnew CyclicArrayList(list).get(i) == list.get(i)for all indicesifor which the right hand side is valid.- Parameters:
list- some array of objects.
-
CyclicArrayList
public CyclicArrayList(java.util.List<? extends E> list)
Creates a newCyclicArrayListsuch thatnew CyclicArrayList(list).get(i) == list.get(i)for all indicesifor which the right hand side is valid.- Parameters:
list- some list of objects.
-
CyclicArrayList
public CyclicArrayList(CyclicList<? extends E> other)
Copy constructor.- Parameters:
other- some cyclic list of objects.
-
-
Method Detail
-
size
public int size()
Returns the number of elements in this list. If this list contains more thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE.- Specified by:
sizein interfacejava.util.Collection<E>- Specified by:
sizein interfaceCyclicList<E>- Returns:
- the number of elements in this list.
-
isEmpty
public boolean isEmpty()
Returnstrueiff this list contains no elements.- Specified by:
isEmptyin interfacejava.util.Collection<E>- Specified by:
isEmptyin interfaceCyclicList<E>- Returns:
trueiff this list contains no elements.
-
getInverse
public CyclicList<E> getInverse()
Returns the inverse of this cyclic list: the list with inverse order.- Specified by:
getInversein interfaceCyclicList<E>- Returns:
- The list with the same entries but inverse order.
-
contains
public boolean contains(java.lang.Object obj)
Returnstrueif this list contains the specified element. More formally, returnstrueif and only if this list contains at least one elementesuch that(o==null ? e==null : o.equals(e)).- Specified by:
containsin interfacejava.util.Collection<E>- Specified by:
containsin interfaceCyclicList<E>- Parameters:
obj- element whose presence in this list is to be tested.- Returns:
trueif this list contains the specified element.
-
containsAll
public boolean containsAll(java.util.Collection<?> coll)
- Specified by:
containsAllin interfacejava.util.Collection<E>
-
cyclicIterator
public CyclicIterator<E> cyclicIterator(int index)
Returns aCyclicIteratorof the elements in this list (in proper sequence), starting at the specified position in this list. The specified index indicates the first element that would be returned by an initial call to thenextmethod. An initial call to thepreviousmethod would return the element with the specified index minus one (modulo the length of this cyclic list).- Specified by:
cyclicIteratorin interfaceCyclicList<E>- Parameters:
index- index of first element to be returned from the list iterator (by a call to thenextmethod). This is interpreted modulo the length of this cyclic list. Any index (even a negative one) is valid.- Returns:
- a cyclic iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
-
iterator
public java.util.Iterator<E> iterator()
Description copied from interface:CyclicListReturnscyclicIterator(index)for some unspecifiedindex.- Specified by:
iteratorin interfacejava.util.Collection<E>- Specified by:
iteratorin interfaceCyclicList<E>- Specified by:
iteratorin interfacejava.lang.Iterable<E>- Returns:
cyclicIterator(index)for some unspecifiedindex.
-
toArray
public java.lang.Object[] toArray()
- Specified by:
toArrayin interfacejava.util.Collection<E>
-
toArray
public <E> E[] toArray(E[] ret)
- Specified by:
toArrayin interfacejava.util.Collection<E>
-
toArray
public java.lang.Object[] toArray(int index)
Returns an array containing all of the elements in this list in proper sequence. Modifying the return value does not modify this CyclicList.- Specified by:
toArrayin interfaceCyclicList<E>- Parameters:
index- index of the element in the cyclic list which comes first in the array returned. This is interpreted modulo the length of this cyclic list. Any index (even negative ones) are valid.- Returns:
- an array containing all of the elements in this list in proper sequence.
-
toArray
public <E> E[] toArray(int index, E[] ret)Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array. Modifying the return value does not modify this CyclicList.- Specified by:
toArrayin interfaceCyclicList<E>- Parameters:
index- index of the element in the cyclic list which comes first in the array returned. This is interpreted modulo the length of this cyclic list. Any index (even negative ones) are valid.ret- the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.- Returns:
- an array containing all of the elements in this list in proper sequence.
- Throws:
java.lang.ArrayStoreException- if the runtime type of the specified array is not a supertype of the runtime type of every element in this list.
-
asList
public java.util.List<E> asList(int index)
Description copied from interface:CyclicListReturns a List containing all of the elements in this cyclic list in proper sequence. Modifying the return value does not modify this CyclicList.- Specified by:
asListin interfaceCyclicList<E>- Parameters:
index- index of the element in the cyclic list which comes first in the List returned. This is interpreted modulo the length of this cyclic list. Any index (even negative ones) are valid.- Returns:
- a list containing all of the elements in this cyclic list in proper sequence.
-
asList
public java.util.List<E> asList()
- Specified by:
asListin interfaceCyclicList<E>
-
cycle
public CyclicArrayList<E> cycle(int index)
Returns a cyclic permutationpof this cyclic list.- Specified by:
cyclein interfaceCyclicList<E>- Parameters:
index- index of the element in the cyclic list which comes first in the cyclic list returned. This is interpreted modulo the length of this cyclic list. Any index (even negative ones) are valid.- Returns:
- a cyclic permutation
pof this cyclic list. It satisfiesp.size() == this.size()andp.get(i) == this.get(i+num).
-
clear
public void clear()
Removes all of the elements from this list (optional operation). This list will be empty after this call returns (unless it throws an exception).- Specified by:
clearin interfacejava.util.Collection<E>- Specified by:
clearin interfaceCyclicList<E>
-
equals
public boolean equals(java.lang.Object obj)
Compares the specified object with this cyclic list for equality. Returnstrueif and only if the specified object is also a cyclic list, both lists have the same size and all corresponding pairs of elements the two lists are equal. (Two elementse1ande2are equal if(e1==null ? e2==null : e1.equals(e2)).) In other words, two cyclic lists are defined to be equal if they contain the same elements in the same order according to their iterators. This definition ensures that the equals method works properly across different implementations of theCyclicListinterface.- Specified by:
equalsin interfacejava.util.Collection<E>- Specified by:
equalsin interfaceCyclicList<E>- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- the object to be compared for equality with this list.- Returns:
trueif the specified object is equal to this list.- See Also:
equalsCyclic(Object)
-
equalsCyclic
public boolean equalsCyclic(java.lang.Object obj)
Compares the specified object with this cyclic list for equality. Returnstrueif and only if the specified object is also a cyclic list, both lists have the same size, and, up to a cyclic permutation, all corresponding pairs of elements the two lists are equal. (Two elementse1ande2are equal if(e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order up to a cyclic permutation. This definition ensures that the equals method works properly across different implementations of theCyclicListinterface.- Specified by:
equalsCyclicin interfaceCyclicList<E>- Parameters:
obj- the object to be compared for equality with this list.- Returns:
trueif the specified object is equal to this list.- See Also:
equals(Object)
-
hashCode
public int hashCode()
Returns the hash code value for this cyclic list. The hash code of a list is defined to be the result of the following calculation:hashCode = 1; Iterator i = list.iterator(); while (i.hasNext()) { Object obj = i.next(); hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode()); }This ensures thatlist1.equals(list2)implies thatlist1.hashCode()==list2.hashCode()for any two lists,list1andlist2, as required by the general contract ofObject.hashCode.- Specified by:
hashCodein interfacejava.util.Collection<E>- Specified by:
hashCodein interfaceCyclicList<E>- Overrides:
hashCodein classjava.lang.Object- Returns:
- the hash code value for this list.
- See Also:
List.hashCode(),Object.equals(Object),equals(Object)
-
hashCodeCyclic
public int hashCodeCyclic()
Description copied from interface:CyclicListReturns a hash code value for this cyclic list which is invariant under cyclic permutation. This kind of hash code is conform withCyclicList.equalsCyclic(Object)and withCyclicList.equals(Object), i.e. equals objects have equal hash codes. The hash code of this cyclic list is the hash code of the underlying set. This ensures thatlist1.equalsCyclic(list2)implies thatlist1.hashCodeCyclic()==list2.hashCodeCyclic()for any two listslist1andlist2.- Specified by:
hashCodeCyclicin interfaceCyclicList<E>- Returns:
- the "cyclic hash code" value for this list.
- See Also:
CyclicList.hashCode(),CyclicList.equalsCyclic(Object)
-
shiftIndex
public int shiftIndex(int index) throws EmptyCyclicListExceptionReturns the number which equalsindexmodulothis.size(), provided this list is not empty.- Specified by:
shiftIndexin interfaceCyclicList<E>- Parameters:
index- This is interpreted modulo the length of this cyclic list. Any index (even negative ones) are valid.- Returns:
- the number which equals
indexmodulothis.size(), provided this list is not empty. - Throws:
EmptyCyclicListException- if this list is empty.
-
shiftIndex
public int shiftIndex(int index, int size) throws EmptyCyclicListException- Throws:
EmptyCyclicListException
-
get
public E get(int index) throws EmptyCyclicListException
Returns the element at the specified position in this list, provided this list is not empty.- Specified by:
getin interfaceCyclicList<E>- Parameters:
index- index of element to return. This is interpreted modulo the length of this cyclic list. Any index (even negative ones) are valid.- Returns:
- the element at the specified position in this list.
- Throws:
EmptyCyclicListException- if this list is empty.
-
set
public E set(int index, E element) throws EmptyCyclicListException
Replaces the element at the specified position in this list with the specified element (optional operation), provided this list is not empty.- Specified by:
setin interfaceCyclicList<E>- Parameters:
index- index of the element to replace. This is interpreted modulo the length of this cyclic list. Any index (even negative ones) are valid.element- element to be stored at the specified position. This is interpreted modulo the length of this cyclic list. Any index (even negative ones) are valid.- Returns:
- the element previously at the specified position.
- Throws:
EmptyCyclicListException- if this list is empty.
-
replace
public void replace(int index, java.util.Iterator<E> iter)Replaces the element at the specified position in this list with the cyclic list of the specified iterator (optional operation). Places the elements of that list as returned byiter.nextin this list.- Specified by:
replacein interfaceCyclicList<E>- Parameters:
index- index index of element to replace.iter- aCyclicIteratorwhich determines an index in a list which replacesthis.get(i).- Throws:
EmptyCyclicListException- if this list is empty.java.lang.IllegalArgumentException- if the specified iterator is empty.- See Also:
CyclicList.replace(int, List)
-
replace
public void replace(int index, java.util.List<E> list)Description copied from interface:CyclicListReplaces the element at the specified position in this list with the specified list (optional operation). Places the elements of that list as returned byiter.nextin this list.- Specified by:
replacein interfaceCyclicList<E>- Parameters:
index- index of element to replace.list- aListwhich determines an index in a list which replacesthis.get(i).- See Also:
CyclicList.replace(int, Iterator)
-
addAll
public void addAll(int index, java.util.Iterator<E> iter)Inserts the cyclic list of the specified iterator at the specified position in this list (optional operation). In contrast toreplace(int, Iterator), the element currently at the specified position is not lost.- Specified by:
addAllin interfaceCyclicList<E>- Parameters:
index- index at which the specified list is to be inserted. This is interpreted modulo the length of this cyclic list. Any index (even negative ones) are valid.iter- iterator delivering the elements to be inserted.- See Also:
CyclicList.addAll(int, List)
-
addAll
public void addAll(int index, java.util.List<? extends E> addList)Inserts the specified list at the given position in this cyclic list. ***** done for collections! Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are returned by the specified collection's iterator. The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.) Contract:list.addAll(i, l); return list.get(i+k)yieldslist.get(k), for allkin0,..,l.size()-1.Note that for
lcontaining a single elemente,list.addAll(i, l)is equivalent withlist.add(i, e).- Specified by:
addAllin interfaceCyclicList<E>- Parameters:
index- index at which the specified list is to be inserted. This is interpreted modulo the length of this cyclic list. Any index (even negative ones) are valid.addList- the list to be inserted. **** this is much more complicated! ****- See Also:
CyclicList.addAll(int, Iterator)
-
addAll
public boolean addAll(java.util.Collection<? extends E> coll)
- Specified by:
addAllin interfacejava.util.Collection<E>
-
add
public void add(int index, E element)Inserts the specified element at the specified position in this list. Contract:list.add(i,o);return list.get(i)yieldso. In contrast toset(int, E), the element currently at the specified position is not lost. Also note that this operation is allowed for empty cyclic lists. In this case,indexis irrelevant.- Specified by:
addin interfaceCyclicList<E>- Parameters:
index- index at which the specified element is to be inserted. This is interpreted modulo the length of this cyclic list plus one (The list emerging after the insertion). In contrast toList.add(int,Object)any index (even a negative one) is valid.element- element to be inserted.
-
remove
public E remove(int index) throws EmptyCyclicListException
Removes the element at the specified position in this list (optional operation). Returns the element that was removed from the list, provided this list is not empty.- Specified by:
removein interfaceCyclicList<E>- Parameters:
index- the index of the element to removed. This is interpreted modulo the length of this cyclic list. Any index (even negative ones) are valid.- Returns:
- the element previously at the specified position.
- Throws:
EmptyCyclicListException- if this list is empty.
-
remove
public boolean remove(java.lang.Object obj)
- Specified by:
removein interfacejava.util.Collection<E>
-
removeAll
public boolean removeAll(java.util.Collection<?> coll)
- Specified by:
removeAllin interfacejava.util.Collection<E>
-
retainAll
public boolean retainAll(java.util.Collection<?> coll)
- Specified by:
retainAllin interfacejava.util.Collection<E>
-
getIndexOf
public int getIndexOf(int idx, java.lang.Object obj)Returns the non-negative index in this cyclic list of the first occurrence of the specified element, or-1if this cyclic list does not contain this element. More formally, returns the lowest indexisuch that(o==null ? get(i)==null : o.equals(get(i))), or some negative index if there is no such index.Note that this specification slightly differs from
List.indexOf(Object).- Specified by:
getIndexOfin interfaceCyclicList<E>- Parameters:
idx- the index to start search with. Independently of this, the search comprises all entries of this cyclic list.obj- element to search for ornull.- Returns:
- the index in this cyclic list
of the first occurrence of the specified
element, or
-1if this list does not contain this element.
-
getCopy
public CyclicList<E> getCopy(int len)
Returns aCyclicListwhich is by copying this list step by step such that the length of the result islen. For examplelen == size()*nyields an n-fold copy of this cyclic list.- Specified by:
getCopyin interfaceCyclicList<E>- Parameters:
len- a non-negativeintvalue.- Returns:
- a
CyclicListwhich is by copying this list step by step such that the length of the result is as specified. - Throws:
java.lang.IllegalArgumentException- iflenis negative.EmptyCyclicListException- if this list is empty andlen > 0.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
clone
public CyclicArrayList<E> clone() throws java.lang.CloneNotSupportedException
Returns a clone of thisCyclicArrayList. This includes copyingvertices.- Overrides:
clonein classjava.lang.Object- Returns:
- a clone of this
CyclicArrayList. This includes copyingvertices. - Throws:
java.lang.CloneNotSupportedException
-
-