ch.bfh.algo
Interface Sequence<E>

All Superinterfaces:
Collection<E>, Container<E>, Iterable<E>, List<E>
All Known Subinterfaces:
GenericSequence<E,P>
All Known Implementing Classes:
ArraySequence, DefaultBinaryForest, DefaultForest, DefaultGraph, GenericAdjacencyListGraph, GenericArraySequence, GenericBinaryForestGraph, GenericForestGraph, GenericLinkedSequence, LinkedSequence, ObserverSequence

public interface Sequence<E>
extends Container<E>, List<E>

The Sequence interface is used to store elements in a flat datastructure. Its implementation can be done using a doubly linked list or an array. In the following example, we use two positions to visit a sequence

Position<Integer> pos1 = seq.first();
while(pos1!=seq.last()){
    pos2=seq.after(pos1);
    if( pos1.element()> pos2.element()){
        Integer tmp = pos1.element();
        seq.replace(pos1,pos2.element());
        seq.replace(pos2,tmp);
    }
}
 
But since a sequence is also a java.util.List, one can use the programs written for such datastructures. We can for instance use the foreach loops:
Sequence<String> seq = ...;
for(String s : seq){
   System.out.prinln(s);
}
 

Version:
1.0
Author:
Juerg Kraehenbuehl (code) and Emmanuel Benoist (Javadoc)

Method Summary
 Position<E> after(Position<?> position)
          returns the Position that is placed after position in the sequence
 Position<E> before(Position<?> position)
          This method returns the position that is placed before the one given as argument in the sequence.
 Position<E> first()
          The method first() returns the Position containing the first element of the sequence.
 Position<E> insertAfter(Position<?> position, E element)
          This method insert element in the sequence.
 Position<E> insertBefore(Position<?> position, E element)
          This method insert element in the sequence.
 Position<E> last()
          The method last() returns the Position containing the last element of the sequence.
 Position<E> position(int rank)
          The position method returns the Position corresponding to a given rank.
 ListIterator<Position<E>> positionListIterator()
          The positionListIterator method creates and return a ListIterator for the Positions contained in the sequence.
 ListIterator<Position<E>> positionListIterator(int rank)
          The positionListIterator method creates and return a ListIterator for the Positions contained in the sequence.
 int rank(Position<?> position)
          The rank method returns the rank of the given position, analogous to the method indexOf in the List interface.
 
Methods inherited from interface ch.bfh.algo.Container
delete, element, encloses, insert, positionIterator, replace, swap
 
Methods inherited from interface java.util.List
add, add, addAll, addAll, clear, contains, containsAll, equals, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, retainAll, set, size, subList, toArray, toArray
 

Method Detail

first

Position<E> first()
                  throws EmptySequenceException
The method first() returns the Position containing the first element of the sequence. It throws an EmptySequenceException if the sequence is empty.

Returns:
the first Position
Throws:
EmptySequenceException - if the sequence is empty.

last

Position<E> last()
                 throws EmptySequenceException
The method last() returns the Position containing the last element of the sequence. It throws an EmptySequenceException if the sequence is empty.

Returns:
the last Position in the sequence.
Throws:
EmptySequenceException - if the sequence is empty.

before

Position<E> before(Position<?> position)
                   throws InvalidAccessorException,
                          BoundaryViolationException
This method returns the position that is placed before the one given as argument in the sequence.

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

after

Position<E> after(Position<?> position)
                  throws InvalidAccessorException,
                         BoundaryViolationException
returns the Position that is placed after position in the sequence

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

insertBefore

Position<E> insertBefore(Position<?> position,
                         E element)
                         throws InvalidAccessorException
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.

Parameters:
position - a Position<?>
Returns:
the new Position containing element
Throws:
InvalidAccessorException - if position does not belong to this sequence.

insertAfter

Position<E> insertAfter(Position<?> position,
                        E element)
                        throws InvalidAccessorException
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.

Parameters:
position - a Position<?>
Returns:
the new Position containing element
Throws:
InvalidAccessorException - if position does not belong to this sequence.

position

Position<E> position(int rank)
                     throws IndexOutOfBoundsException
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.

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

int rank(Position<?> position)
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.

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.

positionListIterator

ListIterator<Position<E>> positionListIterator()
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.

Returns:
a ListIterator<Position<E>> of all the Positions in the sequence.

positionListIterator

ListIterator<Position<E>> positionListIterator(int rank)
                                               throws IndexOutOfBoundsException
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.

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.
Throws:
IndexOutOfBoundsException - if rank is smaller than 0 or larger than the number of elements in the sequence.