ch.bfh.algo.core.sequence
Class GenericLinkedSequence<E,P extends GenericLinkedPosition<E,P>>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.AbstractSequentialList<E>
              extended by ch.bfh.algo.core.sequence.GenericLinkedSequence<E,P>
All Implemented Interfaces:
Container<E>, GenericContainer<E,P>, GenericSequence<E,P>, Sequence<E>, Iterable<E>, Collection<E>, List<E>
Direct Known Subclasses:
GenericAdjacencyListGraph, LinkedSequence, ObserverSequence

public class GenericLinkedSequence<E,P extends GenericLinkedPosition<E,P>>
extends AbstractSequentialList<E>
implements GenericSequence<E,P>


Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
GenericLinkedSequence(SequenceFactory<P> factory)
           
 
Method Summary
 P after(P position)
           
 P after(Position<?> position)
          returns the Position that is placed after position in the sequence
 P before(P position)
           
 P before(Position<?> position)
          This method returns the position that is placed before the one given as argument in the sequence.
 E delete(P position)
           
 E delete(Position<?> position)
           Deletes the specified position and returns the corresponding element.
 E element(P position)
           
 E element(Position<?> position)
           This Container determines if it contains the position and -- if it is found -- the corresponding element is returned.
 boolean encloses(P position)
           
 boolean encloses(Position<?> position)
           Tests if the specified position is contained in this ch.bfh.algo.Container.
 P first()
          The method first() returns the Position containing the first element of the sequence.
protected  P head()
           
 P insert(E element)
           Inserts the specified element into this Container.
protected  P insert(E element, P newPosition)
           
 P insertAfter(P position, E element)
           
 P insertAfter(Position<?> position, E element)
          This method insert element in the sequence.
 P insertBefore(P position, E element)
           
 P insertBefore(Position<?> position, E element)
          This method insert element in the sequence.
protected  P insertBetween(P previous, P next, E element)
           
protected  P insertBetween(P previous, P next, E element, P position)
           
 P last()
          The method last() returns the Position containing the last element of the sequence.
 ListIterator<E> listIterator(int index)
           
 P position(int rank)
          The position method returns the Position corresponding to a given rank.
 PositionIterator<E,P> positionIterator()
           An Iterator over all positions is created.
 PositionListIterator<E,P> positionListIterator()
          The positionListIterator method creates and return a ListIterator for the Positions contained in the sequence.
 PositionListIterator<E,P> positionListIterator(int rank)
          The positionListIterator method creates and return a ListIterator for the Positions contained in the sequence.
 int rank(P position)
           
 int rank(Position<?> position)
          The rank method returns the rank of the given position, analogous to the method indexOf in the List interface.
 E replace(P position, E element)
           
 E replace(Position<?> position, E element)
           Replaces the element at the specified position with the specified element.
 int size()
           
 void swap(Position<?> position0, Position<?> position1)
           
 void swap(P position0, P position1)
           
protected  P tail()
           
protected  void validate(P position)
           
 
Methods inherited from class java.util.AbstractSequentialList
add, addAll, get, iterator, remove, set
 
Methods inherited from class java.util.AbstractList
add, clear, equals, hashCode, indexOf, lastIndexOf, listIterator, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, 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.List
add, add, addAll, addAll, clear, contains, containsAll, equals, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, remove, remove, removeAll, retainAll, set, subList, toArray, toArray
 

Constructor Detail

GenericLinkedSequence

public GenericLinkedSequence(SequenceFactory<P> factory)
Method Detail

size

public int size()
Specified by:
size in interface Collection<E>
Specified by:
size in interface List<E>
Specified by:
size in class AbstractCollection<E>

head

protected P head()

tail

protected P tail()

first

public P first()
                                           throws EmptySequenceException
Description copied from interface: Sequence
The method first() returns the Position containing the first element of the sequence. It throws an EmptySequenceException if the sequence is empty.

Specified by:
first in interface GenericSequence<E,P extends GenericLinkedPosition<E,P>>
Specified by:
first in interface Sequence<E>
Returns:
the first Position
Throws:
EmptySequenceException - if the sequence is empty.

last

public P last()
                                          throws EmptySequenceException
Description copied from interface: Sequence
The method last() returns the Position containing the last element of the sequence. It throws an EmptySequenceException if the sequence is empty.

Specified by:
last in interface GenericSequence<E,P extends GenericLinkedPosition<E,P>>
Specified by:
last in interface Sequence<E>
Returns:
the last Position in the sequence.
Throws:
EmptySequenceException - if the sequence is empty.

before

public P before(Position<?> position)
                                            throws InvalidAccessorException,
                                                   BoundaryViolationException
Description copied from interface: Sequence
This method returns the position that is placed before the one given as argument in the sequence.

Specified by:
before in interface GenericSequence<E,P extends GenericLinkedPosition<E,P>>
Specified by:
before in interface Sequence<E>
Parameters:
position - Position
Returns:
the Position that is before the given one
Throws:
InvalidAccessorException - if position does not belong to this sequence.
BoundaryViolationException - if the given position is the first one (there is nothing before it in this case).

before

public P before(P position)
                                            throws InvalidAccessorException,
                                                   BoundaryViolationException
Throws:
InvalidAccessorException
BoundaryViolationException

after

public P after(Position<?> position)
                                           throws InvalidAccessorException,
                                                  BoundaryViolationException
Description copied from interface: Sequence
returns the Position that is placed after position in the sequence

Specified by:
after in interface GenericSequence<E,P extends GenericLinkedPosition<E,P>>
Specified by:
after in interface Sequence<E>
Parameters:
position - a Position<?>
Returns:
the Position after position
Throws:
InvalidAccessorException - if position does not belong to this sequence.
BoundaryViolationException - if the given position is the last one (there is nothing after it in this case).

after

public P after(P position)
                                           throws InvalidAccessorException,
                                                  BoundaryViolationException
Throws:
InvalidAccessorException
BoundaryViolationException

insertBetween

protected P insertBetween(P previous,
                          P next,
                          E element)

insertBetween

protected P insertBetween(P previous,
                          P next,
                          E element,
                          P position)

insertBefore

public P insertBefore(Position<?> position,
                      E element)
                                                  throws InvalidAccessorException
Description copied from interface: Sequence
This method insert element in the sequence. It creates a new Position that is inserted before position. The new position contains the new element and is returned by the method.

Specified by:
insertBefore in interface GenericSequence<E,P extends GenericLinkedPosition<E,P>>
Specified by:
insertBefore in interface Sequence<E>
Parameters:
position - a Position<?>
Returns:
the new Position containing element
Throws:
InvalidAccessorException - if position does not belong to this sequence.

insertBefore

public P insertBefore(P position,
                      E element)
                                                  throws InvalidAccessorException
Throws:
InvalidAccessorException

insertAfter

public P insertAfter(Position<?> position,
                     E element)
                                                 throws InvalidAccessorException
Description copied from interface: Sequence
This method insert element in the sequence. It creates a new Position that is inserted after position. The new position contains the new element and is returned by the method.

Specified by:
insertAfter in interface GenericSequence<E,P extends GenericLinkedPosition<E,P>>
Specified by:
insertAfter in interface Sequence<E>
Parameters:
position - a Position<?>
Returns:
the new Position containing element
Throws:
InvalidAccessorException - if position does not belong to this sequence.

insertAfter

public P insertAfter(P position,
                     E element)
                                                 throws InvalidAccessorException
Throws:
InvalidAccessorException

insert

public P insert(E element)
Description copied from interface: Container

Inserts the specified element into this Container.

Specified by:
insert in interface Container<E>
Specified by:
insert in interface GenericContainer<E,P extends GenericLinkedPosition<E,P>>
Parameters:
element - The element to be inserted into this Container.
Returns:
The Position of the inserted element.

insert

protected P insert(E element,
                   P newPosition)

replace

public E replace(Position<?> position,
                 E element)
          throws InvalidAccessorException
Description copied from interface: Container

Replaces the element at the specified position with the specified element.

Specified by:
replace in interface Container<E>
Parameters:
position - The Position where the replacement is carried out.
element - The new element at the specified position.
Returns:
The old element (which is being replaced)
Throws:
InvalidAccessorException - if the position does not belong to this Container

replace

public E replace(P position,
                 E element)
          throws InvalidAccessorException
Throws:
InvalidAccessorException

delete

public E delete(Position<?> position)
         throws InvalidAccessorException
Description copied from interface: Container

Deletes the specified position and returns the corresponding element.

Specified by:
delete in interface Container<E>
Parameters:
position - The Position to be deleted
Returns:
The element contained in the position being deleted
Throws:
InvalidAccessorException - if the position does not belong to this Container

delete

public E delete(P position)
         throws InvalidAccessorException
Throws:
InvalidAccessorException

swap

public void swap(Position<?> position0,
                 Position<?> position1)
          throws InvalidAccessorException
Specified by:
swap in interface Container<E>
Throws:
InvalidAccessorException

swap

public void swap(P position0,
                 P position1)
          throws InvalidAccessorException
Throws:
InvalidAccessorException

encloses

public boolean encloses(Position<?> position)
Description copied from interface: Container

Tests if the specified position is contained in this ch.bfh.algo.Container.

Specified by:
encloses in interface Container<E>
Parameters:
position - This Container determines if it contains the position.
Returns:
True iff the position is contained in this Container, false otherwise

encloses

public boolean encloses(P position)

element

public E element(Position<?> position)
Description copied from interface: Container

This Container determines if it contains the position and -- if it is found -- the corresponding element is returned.

Specified by:
element in interface Container<E>
Parameters:
position - The position from which the element is to be returned.
Returns:
The element at the position position

element

public E element(P position)

validate

protected void validate(P position)
                 throws InvalidAccessorException
Throws:
InvalidAccessorException

position

public P position(int rank)
                                              throws IndexOutOfBoundsException
Description copied from interface: Sequence
The position method returns the Position corresponding to a given rank. This method is a bridge between the methods of the List interface and the ones of the Sequence.

Specified by:
position in interface GenericSequence<E,P extends GenericLinkedPosition<E,P>>
Specified by:
position in interface Sequence<E>
Parameters:
rank - an int that denotes the index of the researched position in the sequence.
Returns:
the new Position containing element
Throws:
IndexOutOfBoundsException - if rank is smaller than 0 or larger than or equal to the number of elements in the sequence.

rank

public int rank(Position<?> position)
Description copied from interface: Sequence
The rank method returns the rank of the given position, analogous to the method indexOf in the List interface. This method is a bridge between the methods of the List interface and the ones of the Sequence.

Specified by:
rank in interface Sequence<E>
Parameters:
position - a Position<?> of the sequence
Returns:
the rank of this position (i.e. its index in the sequence). If position is not in the sequence then its rank is -1.

rank

public int rank(P position)

positionListIterator

public PositionListIterator<E,P> positionListIterator(int rank)
Description copied from interface: Sequence
The positionListIterator method creates and return a ListIterator for the Positions contained in the sequence. This iterator is initially placed at the rank given as a parmeter This method gives the possibility to iterate over all the Positions in the sequence.

Specified by:
positionListIterator in interface GenericSequence<E,P extends GenericLinkedPosition<E,P>>
Specified by:
positionListIterator in interface Sequence<E>
Parameters:
rank - an int denoting the initial position of the pointer of the iterator.
Returns:
a ListIterator<Position<E>> of all the Positions in the sequence.

positionListIterator

public PositionListIterator<E,P> positionListIterator()
Description copied from interface: Sequence
The positionListIterator method creates and return a ListIterator for the Positions contained in the sequence. This method gives the possibility to iterate over all the Positions in the sequence.

Specified by:
positionListIterator in interface GenericSequence<E,P extends GenericLinkedPosition<E,P>>
Specified by:
positionListIterator in interface Sequence<E>
Returns:
a ListIterator<Position<E>> of all the Positions in the sequence.

listIterator

public ListIterator<E> listIterator(int index)
                             throws IndexOutOfBoundsException
Specified by:
listIterator in interface List<E>
Specified by:
listIterator in class AbstractSequentialList<E>
Throws:
IndexOutOfBoundsException

positionIterator

public PositionIterator<E,P> positionIterator()
Description copied from interface: Container

An Iterator over all positions is created.

Specified by:
positionIterator in interface Container<E>
Specified by:
positionIterator in interface GenericContainer<E,P extends GenericLinkedPosition<E,P>>
Returns:
an Iterator over all positions in this ch.bfh.algo.Container