ch.bfh.algo.core.sequence
Class GenericArraySequence<E,P extends GenericArrayPosition<E,P>>

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

public class GenericArraySequence<E,P extends GenericArrayPosition<E,P>>
extends AbstractList<E>
implements GenericSequence<E,P>


Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
GenericArraySequence(int size, SequenceFactory<P> factory, ArrayStrategy<P> strategy)
           
 
Method Summary
 void add(int index, E element)
           
 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.
 E get(int index)
           
 P insert(E element)
           Inserts the specified element into this Container.
 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.
 P last()
          The method last() returns the Position containing the last element of the sequence.
 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 remove(int index)
           
 E replace(P position, E element)
           
 E replace(Position<?> position, E element)
           Replaces the element at the specified position with the specified element.
 E set(int index, E element)
           
 int size()
           
 void swap(Position<?> position0, Position<?> position1)
           
 void swap(P position0, P position1)
           
 
Methods inherited from class java.util.AbstractList
add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, 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, addAll, addAll, clear, contains, containsAll, equals, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, removeAll, retainAll, subList, toArray, toArray
 

Constructor Detail

GenericArraySequence

public GenericArraySequence(int size,
                            SequenceFactory<P> factory,
                            ArrayStrategy<P> strategy)
Method Detail

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)

get

public E get(int index)
      throws IndexOutOfBoundsException
Specified by:
get in interface List<E>
Specified by:
get in class AbstractList<E>
Throws:
IndexOutOfBoundsException

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>

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 GenericArrayPosition<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 GenericArrayPosition<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 GenericArrayPosition<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 GenericArrayPosition<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

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 GenericArrayPosition<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

add

public void add(int index,
                E element)
         throws IndexOutOfBoundsException
Specified by:
add in interface List<E>
Overrides:
add in class AbstractList<E>
Throws:
IndexOutOfBoundsException

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 GenericArrayPosition<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 GenericArrayPosition<E,P>>
Parameters:
element - The element to be inserted into this Container.
Returns:
The Position of the inserted element.

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 GenericArrayPosition<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)

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

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)

set

public E set(int index,
             E element)
      throws IndexOutOfBoundsException
Specified by:
set in interface List<E>
Overrides:
set in class AbstractList<E>
Throws:
IndexOutOfBoundsException

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

remove

public E remove(int index)
         throws IndexOutOfBoundsException
Specified by:
remove in interface List<E>
Overrides:
remove in class AbstractList<E>
Throws:
IndexOutOfBoundsException

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

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 GenericArrayPosition<E,P>>
Returns:
an Iterator over all positions in this ch.bfh.algo.Container

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 GenericArrayPosition<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 GenericArrayPosition<E,P>>
Specified by:
positionListIterator in interface Sequence<E>
Returns:
a ListIterator<Position<E>> of all the Positions in the sequence.