Class TwoSidedList<E>
- java.lang.Object
-
- eu.simuline.util.TwoSidedList<E>
-
- Type Parameters:
E- the class of the elements of this list. Created: Sun Aug 26 23:25:26 2007
- All Implemented Interfaces:
java.lang.Iterable<E>,java.util.Collection<E>,java.util.List<E>
public final class TwoSidedList<E> extends java.lang.Object implements java.util.List<E>Compared to a classical list, the first index of this list may well be positive and negative indices are allowed as well.As a consequence, one can add elements not only at the end of this list but also at its beginning. For details consider
add(Object),addFirst(Object)andaddLast(Object). Methodadd(int ind, Object obj)is not supported because inserting an element requires either shift of subsequent elements to the right or preceeding elements to the left. To determine the direction of the shift useadd(int ind, Object obj, Direction dir)instead. Similar considerations apply to methods removing elements. Also affected are the corresponding collections operations likeaddAll.Methods
toArray()andtoArray(Object[])satisfy a generalized contract based on the additional methodfirstIndex().Essentially this two sided list wrapps a classical list. Various constructors allow to pass that list. This allows to determine the performance behavior. The signatures of the constructors generalize the constructors known from implementations of classical
Lists. The observant reader observes that the generics are slightly more restrictive than for classical lists. This is for performance reasons. Note that the constructors do not create a copy of the wrapped list which may cause hidden dependencies. If full generality is needed the user is asked to use the corresponding factory methods.Additionally methods concerning indices are provided like
firstIndex(),minFreeIndex()and it is possible to shift a list usingshiftRight(int).- Version:
- 1.0
- Author:
- Ernst Reissner
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTwoSidedList.DirectionUsed as an argument for methods adding or removing elements from this list to determine in which direction this list has to shrink or grow.
-
Field Summary
Fields Modifier and Type Field Description private intfirstIndexThe first index in thisTwoSidedList.private java.util.List<E>listThe list backed by this two-sided list.
-
Constructor Summary
Constructors Constructor Description TwoSidedList(int firstIndex)Creates a new emptyTwoSidedListwhich starts growing with indexfirstIndex.TwoSidedList(TwoSidedList<E> other)Copy constructor with shallow copy of the wrapped listlist.TwoSidedList(java.util.List<E> list)Creates a newTwoSidedListfrom aListwithfirstIndex == 0.TwoSidedList(java.util.List<E> list, int firstIndex)Creates a newTwoSidedListcontaining the elements oflistin proper sequence with first index given byfirstIndex.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int ind, E obj)Not supported by this implementation. **** breaks contractvoidadd(int ind, E obj, TwoSidedList.Direction dir)Insertsobjat the specified positionindin this list (optional operation).booleanadd(E obj)Not supported by this implementation. **** breaks contractbooleanaddAll(int ind, java.util.Collection<? extends E> coll)Not supported by this implementation.booleanaddAll(int ind, java.util.Collection<? extends E> coll, TwoSidedList.Direction dir)Inserts all of the elements incollinto this list at the specified position (optional operation).booleanaddAll(java.util.Collection<? extends E> coll)Not supported by this implementation.booleanaddAllFirst(java.util.Collection<? extends E> coll)Addsobjat the beginning of this list.booleanaddAllLast(java.util.Collection<? extends E> coll)Appends all of the elements incollto the end of this list, in the order that they are returned bycoll's iterator (optional operation).booleanaddFirst(E obj)Addsobjat the beginning of this list.booleanaddLast(E obj)Addsobjat the end of this list.(package private) voidcheckIncMinFreeIndex()Checks whether incrementingminFreeIndex()would cause overrun ofminFreeIndex().(package private) voidcheckMinFreeIndex(int numAdded)Checks whether addingnumAddedobjects to this list **** or at the tail of this list would cause overrun ofminFreeIndex().(package private) voidcheckRange(int ind)Checks whether indexindis in range and throws an appropriate exception if not.private voidcheckRange(int ind, TwoSidedList.Direction dir)Checks whether indexindis in range and throws an appropriate exception if not.private voidcheckRange(java.lang.String fromToNothing, int ind, int min, int maxP)Checks whether indexindis in range and throws an appropriate exception if not.voidclear()Removes all of the elements from this list (optional operation).booleancontains(java.lang.Object obj)Returns whether this list contains the specified element.booleancontainsAll(java.util.Collection<?> coll)Replaces the element at theindth position in this list with the specified element (optional operation).static <E> TwoSidedList<E>create(TwoSidedList<? extends E> other)static <E> TwoSidedList<E>create(java.util.List<? extends E> list)Creates a newTwoSidedListcontaining the elements oflistin proper sequence.static <E> TwoSidedList<E>create(java.util.List<? extends E> list, int firstIndex)Creates a newTwoSidedListcontaining the elements oflistin proper sequence with first index given byfirstIndex.private voiddecFirstIndex()DecrementsfirstIndexif possible; otherwise throws an exception.booleanequals(java.lang.Object obj)The given object equals this twosided list if and only if it is as well aTwoSidedList, the two lists wrappedlistcoincide and either as well the first indicesfirstIndexcoincide.intfirstIndex()ReturnsfirstIndex.voidfirstIndex(int firstIndex)SetsfirstIndexaccording to the parameter.Eget(int ind)Returns the element at the specified position in this list.inthashCode()Returns a hash code which conforms withequals(Object).private voidincFirstIndex()IncrementsfirstIndexif possible; otherwise throws an exception.intindexOf(java.lang.Object obj)Returns the index of the first occurrence of the specified elementobjin this list, orfirstIndex()-1 if this list does not contain the element.booleanisEmpty()Returns true if this list contains no elements.java.util.Iterator<E>iterator()intlastIndexOf(java.lang.Object obj)Returns the index of the last occurrence of the specified elementobjin this list, orfirstIndex()-1 if this list does not contain the element.java.util.List<E>list()java.util.ListIterator<E>listIterator()java.util.ListIterator<E>listIterator(int ind)****maybe to be endowed with a direction.static voidmain(java.lang.String[] args)intminFreeIndex()Eremove(int ind)Not supported by this implementation. **** breaks contractEremove(int ind, TwoSidedList.Direction dir)Removes the element at the specified position in this list (optional operation).booleanremove(java.lang.Object obj)Not supported by this implementation. **** breaks contractbooleanremoveAll(java.util.Collection<?> coll)Not supported by this implementation.booleanremoveAll(java.util.Collection<?> coll, TwoSidedList.Direction dir)Removes from this list all of its elements that are contained incoll(optional operation).booleanremoveFirst(java.lang.Object obj)Removes the first occurrence ofobjfrom this list, if present (optional operation).booleanremoveLast(java.lang.Object obj)Removes the last occurrence ofobjfrom this list, if present (optional operation).booleanretainAll(java.util.Collection<?> coll)Not supported by this implementation.booleanretainAll(java.util.Collection<?> coll, TwoSidedList.Direction dir)Retains only the elements in this list that are contained incoll(optional operation).Eset(int ind, E obj)Replaces the element at theindth position in this list with the specified element (optional operation).intshiftRight(int num)Shifts this listnumindices to the right.intsize()Returns the number of elements in this list.private voidsubFirstIndex(int numAdded)SubtractsnumAddedfromfirstIndexif possible; otherwise throws an exception.java.util.List<E>subList(int indStart, int indEnd)Returns a view of the portion of this twosided list as a list between the specifiedfromIndex, inclusive, andtoIndex, exclusive.TwoSidedList<E>subList2(int indStart, int indEnd)Returns a view of the portion of this twosided list between the specifiedfromIndex, inclusive, andtoIndex, exclusive.java.lang.Object[]toArray()Note that this generalizes the contract of the underlying interface: Instead ofthis.toArray[i] == this.get(i)now onlythis.toArray[i] == this.get(i-firstIndex()).<E> E[]toArray(E[] objArr)Note that this generalizes the contract of the underlying interface: Instead ofthis.toArray[i] == this.get(i)now onlythis.toArray[i] == this.get(i-firstIndex()).java.lang.StringtoString()
-
-
-
Field Detail
-
list
private java.util.List<E> list
The list backed by this two-sided list.
-
firstIndex
private int firstIndex
The first index in thisTwoSidedList. Note that this integer may well be negative. The inequalityis guaranteed. Casually, methods adding objects have to reject them in order not to hurt this alhough the backed listminFreeIndex()>=firstIndexlistwould allow adding further elements.
-
-
Constructor Detail
-
TwoSidedList
public TwoSidedList(java.util.List<E> list, int firstIndex)
Creates a newTwoSidedListcontaining the elements oflistin proper sequence with first index given byfirstIndex.Note the difference to reference implementations such as
java.util.ArrayListwhere the type of the list argument isList<? extends E>. We deviate from this solution for performance reason and provide as an alternative the factory methodcreate(List,int).CAUTION: This list backs
listand so changes to one of the list affect the other list.- Parameters:
list- the list wrapped by this twosided list.firstIndex- the index where this list starts growing.- Throws:
java.lang.IllegalStateException- iflistis so long andfirstIndexis so large thatminFreeIndex()would overrun.
-
TwoSidedList
public TwoSidedList(java.util.List<E> list)
Creates a newTwoSidedListfrom aListwithfirstIndex == 0. This is the canonical generalization of lists as mentioned in the documentation ofindexOf(Object)and oflastIndexOf(Object).Note the difference to reference implementations such as
java.util.ArrayListwhere the type of the list argument isList<? extends E>. We deviate from this solution for performance reason and provide as an alternative the factory methodcreate(List,int).CAUTION: Changes to
listinfluence this twosided list and may cause malfunction. Note that unlikeTwoSidedList(List, int)this constructor cannot throw anIllegalStateException.- Parameters:
list- the list wrapped by this twosided list.
-
TwoSidedList
public TwoSidedList(TwoSidedList<E> other)
Copy constructor with shallow copy of the wrapped listlist. As a consequence, modifications of the list created may affect the original one and the other way round. Note that unlikeTwoSidedList(List, int)this constructor cannot throw anIllegalStateException.- Parameters:
other- anotherTwoSidedList.
-
TwoSidedList
public TwoSidedList(int firstIndex)
Creates a new emptyTwoSidedListwhich starts growing with indexfirstIndex. Note that unlikeTwoSidedList(List, int)this constructor cannot throw anIllegalStateException.- Parameters:
firstIndex- the index where this list starts growing.
-
-
Method Detail
-
create
public static <E> TwoSidedList<E> create(java.util.List<? extends E> list, int firstIndex)
Creates a newTwoSidedListcontaining the elements oflistin proper sequence with first index given byfirstIndex.- Parameters:
list- the list wrapped by this twosided list. Changes tolistdo not influence this twosided list.firstIndex- the index where this list starts growing.- Throws:
java.lang.IllegalStateException- iflistis so long andfirstIndexis so large thatminFreeIndex()would overrun.
-
create
public static <E> TwoSidedList<E> create(java.util.List<? extends E> list)
Creates a newTwoSidedListcontaining the elements oflistin proper sequence.- Parameters:
list- the list wrapped by this twosided list. Changes tolistdo not influence this twosided list.
-
create
public static <E> TwoSidedList<E> create(TwoSidedList<? extends E> other)
Creates a newTwoSidedListas a copy ofothercopying also the wrapped listlist. As a consequence, the list created and the original one act independently. Note that unlikeTwoSidedList(List, int)this constructor cannot throw anIllegalStateException.- Parameters:
other- anotherTwoSidedList.
-
checkRange
void checkRange(int ind)
Checks whether indexindis in range and throws an appropriate exception if not.- Parameters:
ind- the index to be checked.- Throws:
java.lang.IndexOutOfBoundsException- if notfirstIndex() <= ind < minFreeIndex()with message"Index: <ind> Range: <firstIndex> - <minFreeIndex()> exclusively. ".
-
checkRange
private void checkRange(int ind, TwoSidedList.Direction dir)Checks whether indexindis in range and throws an appropriate exception if not.- Parameters:
ind- the index to be checked.dir- the direction in which this twosided list may grow.- Throws:
java.lang.IndexOutOfBoundsException-- for
dir == Left2Rightif notfirstIndex() <= ind < minFreeIndex()+1. - for
dir == Right2Leftif notfirstIndex()-1 <= ind < minFreeIndex().
"Index: <ind>Range:<firstIndex> - <minFreeIndex()>exclusively. ".- for
- See Also:
add(int, Object, Direction),addAll(int, Collection, Direction)
-
checkRange
private void checkRange(java.lang.String fromToNothing, int ind, int min, int maxP)Checks whether indexindis in range and throws an appropriate exception if not.- Parameters:
fromToNothing- either"","from"or"to". The latter two cases are used only insubList(int,int)to check the range of the start index and of the end index of the sublist to be created.ind- the index to be checked.min- the minimal value forindaccepted.maxP- one plus the maximal value forindaccepted.- Throws:
java.lang.IndexOutOfBoundsException- if notmin >= ind < maxP. The message is"Index: <ind> Range: <firstIndex> - <minFreeIndex()> exclusively. "preceeded byfromToNothing.
-
decFirstIndex
private void decFirstIndex()
DecrementsfirstIndexif possible; otherwise throws an exception. This method is used by methods adding objects at the head of this list. The error message is tailored to this usage.- Throws:
java.lang.IllegalStateException- ifadding an element would cause index underrun.firstIndex== Integer.MIN_VALUE
-
incFirstIndex
private void incFirstIndex()
IncrementsfirstIndexif possible; otherwise throws an exception. This method is used by methods removing objects from the head of this list. The error message is tailored to this usage.- Throws:
java.lang.IllegalStateException- ifremoving an element would cause index overrun.firstIndex== Integer.MAX_VALUE
-
checkIncMinFreeIndex
void checkIncMinFreeIndex()
Checks whether incrementingminFreeIndex()would cause overrun ofminFreeIndex().- Throws:
java.lang.IllegalStateException- if incrementingminFreeIndex()would cause overrun ofminFreeIndex().
-
subFirstIndex
private void subFirstIndex(int numAdded)
SubtractsnumAddedfromfirstIndexif possible; otherwise throws an exception. This method is used by methods adding objects at the head of this list. The error message is tailored to this usage.- Throws:
java.lang.IllegalStateException- if addingnumAddedobjects at the head of this list would cause underrun offirstIndex.
-
checkMinFreeIndex
void checkMinFreeIndex(int numAdded)
Checks whether addingnumAddedobjects to this list **** or at the tail of this list would cause overrun ofminFreeIndex().- Throws:
java.lang.IllegalStateException- if addingnumAddedobjects to this list **** or at the tail of this list would cause overrun ofminFreeIndex().
-
firstIndex
public int firstIndex()
ReturnsfirstIndex.
-
firstIndex
public void firstIndex(int firstIndex)
SetsfirstIndexaccording to the parameter.
-
minFreeIndex
public int minFreeIndex()
-
shiftRight
public int shiftRight(int num)
Shifts this listnumindices to the right.- Parameters:
num- the number of shifts to the right to be performed on this list. A negative sign signifies a shift to the left.- Returns:
- The new first index.
- Throws:
java.lang.IllegalStateException- if shifting would cause overrun ofminFreeIndex()(occuring for proper shift to the right) or underrun offirstIndex()(occuring for proper shift to the left).
-
list
public java.util.List<E> list()
-
size
public int size()
Returns the number of elements in this list. If this list contains more thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE. **** gives rise to malfunction. ****
-
isEmpty
public boolean isEmpty()
Returns true if this list contains no elements.
-
contains
public boolean contains(java.lang.Object obj)
Returns whether this list contains the specified element. More formally, returnstrueif and only if this list contains at least one elementobjsuch that(o==null ? e==null : o.equals(e)).- Specified by:
containsin interfacejava.util.Collection<E>- Specified by:
containsin interfacejava.util.List<E>- Parameters:
obj- anObjectvalue- Returns:
- a
booleanvalue - Throws:
java.lang.ClassCastException- if the class ofobjis incompatible withlist.java.lang.NullPointerException- ifobj == nullandlistdoes not permitnullelements.
-
indexOf
public int indexOf(java.lang.Object obj)
Returns the index of the first occurrence of the specified elementobjin this list, orfirstIndex()-1 if this list does not contain the element. More formally, returns the lowest indexisuch that(obj==null ? get(i)==null : obj.equals(get(i))), orfirstIndex()-1 if there is no such index.CAUTION:
-
This breaks **** "extends" contract for the interface List:
Test for "element
objnot in list" is no longerthis.indexOf(obj) == -1butthis.indexOf(obj) < this.firstIndex()-1. This is an extension in that wrapping an ordinary list in a twosided list is by specifyingfirstIndex() == 0(seeTwoSidedList(List list)). -
Note that for
firstIndex() ==Integer.MIN_VALUEfirstIndex()-1 > firstIndex().
- Specified by:
indexOfin interfacejava.util.List<E>- Parameters:
obj- anObjectvalue- Returns:
-
the index of the first occurrence of
objif this object is in this list. In this case, the return value is within the rangefirstIndex()..firstFreeIndex()-1. -
firstIndex()-1ifobjis not in this list.
-
the index of the first occurrence of
-
This breaks **** "extends" contract for the interface List:
Test for "element
-
lastIndexOf
public int lastIndexOf(java.lang.Object obj)
Returns the index of the last occurrence of the specified elementobjin this list, orfirstIndex()-1 if this list does not contain the element. More formally, returns the highest indexisuch that(obj==null ? get(i)==null : obj.equals(get(i))), orfirstIndex()-1 if there is no such index.CAUTION:
-
This breaks **** "extends" contract for the interface List:
Test for "element
objnot in list" is no longerthis.indexOf(obj) == -1butthis.indexOf(obj) < this.firstIndex()-1. This is an extension in that wrapping an ordinary list in a twosided list is by specifyingfirstIndex() == 0(seeTwoSidedList(List list)). -
Note that for
firstIndex() ==Integer.MIN_VALUEfirstIndex()-1 > firstIndex().
- Specified by:
lastIndexOfin interfacejava.util.List<E>- Parameters:
obj- anObjectvalue- Returns:
-
the index of the last occurrence of
objif this object is in this list. In this case, the return value is within the rangefirstIndex()..firstFreeIndex()-1. -
firstIndex()-1ifobjis not in this list.
-
the index of the last occurrence of
-
This breaks **** "extends" contract for the interface List:
Test for "element
-
toArray
public java.lang.Object[] toArray()
Note that this generalizes the contract of the underlying interface: Instead ofthis.toArray[i] == this.get(i)now onlythis.toArray[i] == this.get(i-firstIndex()). Equality includes that the left hand side throws an exception if and only if so does te right hand side.
-
toArray
public <E> E[] toArray(E[] objArr)
Note that this generalizes the contract of the underlying interface: Instead ofthis.toArray[i] == this.get(i)now onlythis.toArray[i] == this.get(i-firstIndex()). Equality includes that the left hand side throws an exception if and only if so does te right hand side.
-
get
public E get(int ind)
Returns the element at the specified position in this list.- Specified by:
getin interfacejava.util.List<E>- Parameters:
ind- the index of the element to return as anintvalue.- Returns:
- the element at the specified position in this list.
- Throws:
java.lang.IndexOutOfBoundsException- as described forcheckRange(int)
-
set
public E set(int ind, E obj)
Replaces the element at theindth position in this list with the specified element (optional operation).- Specified by:
setin interfacejava.util.List<E>- Parameters:
ind- the index of the element to replace as anintvalue.obj- the element to be stored at the specified position.- Returns:
- the element previously at the
indth position - Throws:
java.lang.UnsupportedOperationException- if thesetoperation is not supported bylist.java.lang.ClassCastException- if the class ofobjprevents it from being added tolist.java.lang.NullPointerException- ifobj == nullandlistdoes not permitnullelements.java.lang.IllegalArgumentException- if some property ofobjprevents it from being added tolist.java.lang.IndexOutOfBoundsException- as described forcheckRange(int)
-
add
public boolean add(E obj)
Not supported by this implementation. **** breaks contract- Specified by:
addin interfacejava.util.Collection<E>- Specified by:
addin interfacejava.util.List<E>- Throws:
java.lang.UnsupportedOperationException- useaddFirst(E)andaddLast(E)instead.
-
addFirst
public boolean addFirst(E obj)
Addsobjat the beginning of this list. The first index returned byfirstIndex()is decremented.- Parameters:
obj- theEobject to be added.- Returns:
trueby specification.- Throws:
java.lang.UnsupportedOperationException- if theadd(int,E)operation is not supported bylist.java.lang.ClassCastException- if the class ofobjprevents it from being added tolist.java.lang.NullPointerException- ifobj == nullandlistdoes not permitnullelements.java.lang.IllegalArgumentException- if some property ofobjprevents it from being added tolist.java.lang.IllegalStateException- ifadding an element would cause index underrun.firstIndex== Integer.MIN_VALUE
-
addLast
public boolean addLast(E obj)
Addsobjat the end of this list. The first index returned byfirstIndex()remains unchanged.- Parameters:
obj- theEobject to be added.- Returns:
trueby specification.- Throws:
java.lang.UnsupportedOperationException- if theadd(E)operation is not supported bylist.java.lang.ClassCastException- if the class ofobjprevents it from being added tolist.java.lang.NullPointerException- ifobj == nullandlistdoes not permitnullelements.java.lang.IllegalArgumentException- if some property ofobjprevents it from being added tolist.java.lang.IllegalStateException- if adding objects to this list would cause overrun ofminFreeIndex().
-
add
public void add(int ind, E obj)Not supported by this implementation. **** breaks contract- Specified by:
addin interfacejava.util.List<E>- Throws:
java.lang.UnsupportedOperationException- useadd(int,Object,Direction)instead.
-
add
public void add(int ind, E obj, TwoSidedList.Direction dir)Insertsobjat the specified positionindin this list (optional operation). Shifts the element currently at that position (if any) and any following/preceeding elements to the directiondir(increments/decrements their indices).- Parameters:
ind- the index where to insertobj. After having performed this operation,indis the index ofobj.obj- theEelement to be inserted.dir- determines the direction to shift the list.Left2Right: shifts all subsequent objects in this list starting with indexindto the right adding one to their indices.Right2Left: shifts all objects in this list to indexindto the left subtracting one from their indices.
- Throws:
java.lang.UnsupportedOperationException- if theaddoperation is not supported bylist.java.lang.ClassCastException- if the class ofobjprevents it from being added tolist.java.lang.NullPointerException- ifobj == nullandlistdoes not permitnullelements.java.lang.IllegalArgumentException- if some property ofobjprevents it from being added tolist.java.lang.IndexOutOfBoundsException- as described forcheckRange(int,Direction)
-
remove
public E remove(int ind)
Not supported by this implementation. **** breaks contract- Specified by:
removein interfacejava.util.List<E>- Throws:
java.lang.UnsupportedOperationException- useremove(int,Direction)instead.
-
remove
public E remove(int ind, TwoSidedList.Direction dir)
Removes the element at the specified position in this list (optional operation). Shifts any following/preceeding elements to the directiondir(decrements/increments their indices). Returns the element that was removed from the list.- Parameters:
ind- the index of the element to be removed as anintvalue.- Returns:
- the element previously at the specified position.
- Throws:
java.lang.UnsupportedOperationException- if theremove(int)operation is not supported bylist.java.lang.IndexOutOfBoundsException- as described forcheckRange(int)
-
remove
public boolean remove(java.lang.Object obj)
Not supported by this implementation. **** breaks contract- Specified by:
removein interfacejava.util.Collection<E>- Specified by:
removein interfacejava.util.List<E>- Throws:
java.lang.UnsupportedOperationException- useremoveFirst(Object)andremoveLast(Object)instead.
-
removeFirst
public boolean removeFirst(java.lang.Object obj)
Removes the first occurrence ofobjfrom this list, if present (optional operation). If this list does not containobj, it is unchanged. More formally, removes the element with the lowest indexisuch that(obj==null ? get(i)==null : obj.equals(get(i)))(if such an element exists). Returnstrueif this list contained the specified element (or equivalently, if this list changed as a result of the call). The first index returned byfirstIndex()remains unchanged.- Parameters:
obj- the element to be removed from this list, if present.- Returns:
- whether this list contained the specified element
- Throws:
java.lang.UnsupportedOperationException- if theremove(Object)operation is not supported bylist.
-
removeLast
public boolean removeLast(java.lang.Object obj)
Removes the last occurrence ofobjfrom this list, if present (optional operation). If this list does not containobj, it is unchanged. More formally, removes the element with the highest indexisuch that(obj==null ? get(i)==null : obj.equals(get(i)))(if such an element exists). Returnstrueif this list contained the specified element (or equivalently, if this list changed as a result of the call). The first index returned byfirstIndex()is incremented if really an object was removed.- Parameters:
obj- the element to be removed from this list, if present.- Returns:
- whether this list contained the specified element
- Throws:
java.lang.UnsupportedOperationException- if theremove(int)**** not remove(Object) operation is not supported bylist.
-
clear
public void clear()
Removes all of the elements from this list (optional operation). The list will be empty after this call returns andfirstIndexis unmodified.
-
addAll
public boolean addAll(java.util.Collection<? extends E> coll)
Not supported by this implementation.- Specified by:
addAllin interfacejava.util.Collection<E>- Specified by:
addAllin interfacejava.util.List<E>- Throws:
java.lang.UnsupportedOperationException- useaddAllFirst(java.util.Collection<? extends E>)andaddAllLast(java.util.Collection<? extends E>)instead.
-
addAllFirst
public boolean addAllFirst(java.util.Collection<? extends E> coll)
Addsobjat the beginning of this list. in the order that they are returned bycoll's iterator (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur ifcollis this list, and it's nonempty.) The first index returned byfirstIndex()is reduced bycoll's size.- Parameters:
coll- aCollectionvalue- Returns:
- whether this list changed as a result of the call
as a
booleanvalue. - Throws:
java.lang.UnsupportedOperationException- if the addAll operation is not supported by thislist.java.lang.ClassCastException- if the class of an element of the specified collection prevents it from being added tolist.java.lang.NullPointerException- if the specified collection contains one or morenullelements andlistdoes not permitnullelements or if the specified collection isnulljava.lang.IllegalArgumentException- if some property of an element of the specified collection prevents it from being added tolist.java.lang.IllegalStateException- if addingnumAddedobjects at the head of this list would cause underrun offirstIndex.
-
addAllLast
public boolean addAllLast(java.util.Collection<? extends E> coll)
Appends all of the elements incollto the end of this list, in the order that they are returned bycoll's iterator (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur ifcollis this list, and it's nonempty.) The first index returned byfirstIndex()remains unchanged.- Parameters:
coll- anotherCollection.- Returns:
- whether this list changed as a result of the call
as a
booleanvalue. - Throws:
java.lang.UnsupportedOperationException- if the addAll operation is not supported by thislist.java.lang.ClassCastException- if the class of an element of the specified collection prevents it from being added tolist.java.lang.NullPointerException- if the specified collection contains one or morenullelements andlistdoes not permitnullelements or if the specified collection isnulljava.lang.IllegalArgumentException- if some property of an element of the specified collection prevents it from being added tolist.java.lang.IllegalStateException- if addingnumAddedobjects to this list **** or at the tail of this list would cause overrun ofminFreeIndex().
-
addAll
public boolean addAll(int ind, java.util.Collection<? extends E> coll)Not supported by this implementation.- Specified by:
addAllin interfacejava.util.List<E>- Throws:
java.lang.UnsupportedOperationException- useaddAll(int,Collection,Direction)instead.
-
addAll
public boolean addAll(int ind, java.util.Collection<? extends E> coll, TwoSidedList.Direction dir)Inserts all of the elements incollinto this list at the specified position (optional operation). Shifts the elements currently at that positions (if any) and any following/preceeding elements to the directiondir(increasing/decreasing their indices bycoll's size). 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 undefined 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.)- Parameters:
ind- index at which to insert the first element fromcollas anintvalue. CAUTION: Note thatindalways references the first element incollindependent fromdir.coll- aCollectioncontaining elements to be added to this list.dir- determines the direction to shift the list.Left2Right: shifts all subsequent objects in this list starting with indexindto the right adding one to their indices.Right2Left: shifts all objects in this list to indexindto the left subtracting one from their indices.
- Returns:
- whether this list changed as a result of the call
as a
booleanvalue. - Throws:
java.lang.UnsupportedOperationException- if theaddAlloperation is not supported by thislist.java.lang.ClassCastException- if the class of an element ofcollprevents it from being added tolist.java.lang.NullPointerException- if thecollcontains at least onenullelement andlistdoes not permitnullelements or ifcoll == null.java.lang.IllegalArgumentException- if some property of an element of the specified collection prevents it from being added tolist.java.lang.IndexOutOfBoundsException- as described forcheckRange(int,Direction)java.lang.IllegalStateException- if addingcollto this list would cause underrun offirstIndexor overrun ofminFreeIndex().
-
iterator
public java.util.Iterator<E> iterator()
-
listIterator
public java.util.ListIterator<E> listIterator()
- Specified by:
listIteratorin interfacejava.util.List<E>
-
listIterator
public java.util.ListIterator<E> listIterator(int ind)
****maybe to be endowed with a direction.- Specified by:
listIteratorin interfacejava.util.List<E>- Parameters:
ind- anintvalue- Returns:
- a
ListIteratorvalue - Throws:
java.lang.IndexOutOfBoundsException- if notfirstIndex() <= ind <= minFreeIndex()with message"Index: <ind> Range: <firstIndex> - <minFreeIndex()> exclusively. ".
-
containsAll
public boolean containsAll(java.util.Collection<?> coll)
Replaces the element at theindth position in this list with the specified element (optional operation).- Specified by:
containsAllin interfacejava.util.Collection<E>- Specified by:
containsAllin interfacejava.util.List<E>- Parameters:
coll- aCollectionvalue- Returns:
- a
booleanvalue - Throws:
java.lang.ClassCastException- if the class of one or more elements ofcollis incompatible withlist.java.lang.NullPointerException- ifcollcontains at least onenullelement andlistis incompatible withnullelements or ifcoll == null.
-
removeAll
public boolean removeAll(java.util.Collection<?> coll)
Not supported by this implementation.- Specified by:
removeAllin interfacejava.util.Collection<E>- Specified by:
removeAllin interfacejava.util.List<E>- Throws:
java.lang.UnsupportedOperationException- useremoveAll(Collection,Direction)instead.
-
removeAll
public boolean removeAll(java.util.Collection<?> coll, TwoSidedList.Direction dir)Removes from this list all of its elements that are contained incoll(optional operation). Shifts the elements currently at that position (if any) and any following/preceeding elements to the directiondir(decreasing/increasing their indices by the number of elements removed).- Parameters:
coll- aCollectioncontaining elements to be removed from this list.dir- determines the direction to shift the list.Left2Right: to close gaps by removing elements shifts all objects preceeding gaps to the right adding one to their indices.Right2Left: to close gaps by removing elements shifts all objects following gaps to the left subtracting one from their indices.
- Returns:
- whether this list changed as a result of the call
as a
booleanvalue. - Throws:
java.lang.UnsupportedOperationException- if theremoveAlloperation is not supported by thislist.java.lang.ClassCastException- if the class of an element ofcollis incompatible withlist.java.lang.NullPointerException- if thecollcontains at least onenullelement andlistdoes not permitnullelements or ifcoll == null.
-
retainAll
public boolean retainAll(java.util.Collection<?> coll)
Not supported by this implementation.- Specified by:
retainAllin interfacejava.util.Collection<E>- Specified by:
retainAllin interfacejava.util.List<E>- Throws:
java.lang.UnsupportedOperationException- useretainAll(Collection,Direction)instead.
-
retainAll
public boolean retainAll(java.util.Collection<?> coll, TwoSidedList.Direction dir)Retains only the elements in this list that are contained incoll(optional operation). In other words, removes from this list all the elements that are not contained incoll. Shifts the elements currently at that position (if any) and any following/preceeding elements to the directiondir(decreasing/increasing their indices by the number of elements removed).- Parameters:
coll- aCollectioncontaining elements to be retained in this list.dir- determines the direction to shift the list.Left2Right: to close gaps by removing elements shifts all objects preceeding gaps to the right adding one to their indices.Right2Left: to close gaps by removing elements shifts all objects following gaps to the left subtracting one from their indices.
- Returns:
- whether this list changed as a result of the call
as a
booleanvalue. - Throws:
java.lang.UnsupportedOperationException- if theretainAlloperation is not supported by thislist.java.lang.ClassCastException- if the class of an element ofcollis incompatible withlist.java.lang.NullPointerException- if thecollcontains at least onenullelement andlistdoes not permitnullelements or ifcoll == null.
-
subList
public java.util.List<E> subList(int indStart, int indEnd)
Returns a view of the portion of this twosided list as a list between the specifiedfromIndex, inclusive, andtoIndex, exclusive.- Specified by:
subListin interfacejava.util.List<E>- Parameters:
indStart- low endpoint (inclusive) of the subList to be returned.indEnd- high endpoint (exclusive) of the subList to be returned.- Returns:
- view of the specified range within this list. The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.
- Throws:
java.lang.IndexOutOfBoundsException- if notfirstIndex() <= indStart <= indEnd <= minFreeIndex()
-
subList2
public TwoSidedList<E> subList2(int indStart, int indEnd)
Returns a view of the portion of this twosided list between the specifiedfromIndex, inclusive, andtoIndex, exclusive.- Parameters:
indStart- low endpoint (inclusive) of the subList to be returned.indEnd- high endpoint (exclusive) of the subList to be returned.- Returns:
- view of the specified range within this list. The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.
- Throws:
java.lang.IndexOutOfBoundsException- seesubList(int,int)
-
equals
public boolean equals(java.lang.Object obj)
The given object equals this twosided list if and only if it is as well aTwoSidedList, the two lists wrappedlistcoincide and either as well the first indicesfirstIndexcoincide. CAUTION: Note that two empty lists with different first index are not equal. This is justified by the fact, that these two become different when the same first element is added to both lists.
-
hashCode
public int hashCode()
Returns a hash code which conforms withequals(Object).
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
main
public static void main(java.lang.String[] args)
-
-