structure5
Class OrderedList<E extends java.lang.Comparable<E>>

java.lang.Object
  extended by structure5.AbstractStructure<E>
      extended by structure5.OrderedList<E>
All Implemented Interfaces:
java.lang.Iterable<E>, OrderedStructure<E>, Structure<E>

public class OrderedList<E extends java.lang.Comparable<E>>
extends AbstractStructure<E>
implements OrderedStructure<E>

A class that implements a collection of values that are kept in order. Base values must be comparable. Unlike Lists there is no notion of head or tail.

Example Usage:

To determine the effect of the original Starwars™ movie on the careers of its stars, we could place ComparableAssociations between each star's name and the number of movies they have been in since Starwars™ into an ordered vector and print our the results, as follows:

 public static void main(String[] argv){
      //instantiate an ordered vector
      OrderedList> v = new #OrderedList>();
      
      //add the cast members of the original star wars along with
      //the number of films in which the have subsequently appeared
      v.add(new ComparableAssociation(new Integer(12),"Sir Alec Guiness"));
      v.add(new ComparableAssociation(new Integer(24),"Carrie Fisher"));
      v.add(new ComparableAssociation(new Integer(28),"Harrison Ford")); 
      v.add(new ComparableAssociation(new Integer(28),"Mark Hamill"));

      //print out the results
      for(Iterator> i = v.iterator(); i.hasNext();){
          ComparableAssociation actor = i.next();
          System.out.println(actor.getValue() + " has been in " + 
                             actor.getKey() + " movies since Star Wars"); 
      }
   }
 

See Also:
structure.Vector

Field Summary
protected  int count
          Number of elements in list
protected  Node<E> data
          Pointer to the smallest element, maintained as a singly linked list
protected  java.util.Comparator<? super E> ordering
          The ordereding used to arange the values
 
Constructor Summary
OrderedList()
          Construct an empty ordered list
OrderedList(java.util.Comparator<? super E> ordering)
          Construct an empty ordered list with alternative ordering
 
Method Summary
 void add(E value)
          Add a value to the ordered list, keeping values in order
 void clear()
          Remove all the elements from the ordered list
 boolean contains(E value)
          Determine if the ordered list contains a value
 boolean isEmpty()
          Determine if the list is empty
 java.util.Iterator<E> iterator()
          Construct an iterator for traversing elements of ordered list in ascending order
 E remove(E value)
          Remove a value from the ordered list.
 int size()
          Determine the number of elements in the list
 java.lang.String toString()
          Generate string representation of the ordered list
 
Methods inherited from class structure5.AbstractStructure
elements, hashCode, values
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface structure5.Structure
elements, values
 

Field Detail

data

protected Node<E extends java.lang.Comparable<E>> data
Pointer to the smallest element, maintained as a singly linked list


count

protected int count
Number of elements in list


ordering

protected java.util.Comparator<? super E extends java.lang.Comparable<E>> ordering
The ordereding used to arange the values

Constructor Detail

OrderedList

public OrderedList()
Construct an empty ordered list

Postcondition:
constructs an empty ordered list

OrderedList

public OrderedList(java.util.Comparator<? super E> ordering)
Construct an empty ordered list with alternative ordering

Parameters:
ordering - the Comparator to be used in comparison
Postcondition:
constructs an empty ordered list ordered by ordering
Method Detail

clear

public void clear()
Remove all the elements from the ordered list

Specified by:
clear in interface Structure<E extends java.lang.Comparable<E>>
Postcondition:
the ordered list is empty

add

public void add(E value)
Add a value to the ordered list, keeping values in order

Specified by:
add in interface Structure<E extends java.lang.Comparable<E>>
Parameters:
value - The value to be added to the list
Precondition:
value is non-null
Postcondition:
value is added to the list, leaving it in order

contains

public boolean contains(E value)
Determine if the ordered list contains a value

Specified by:
contains in interface Structure<E extends java.lang.Comparable<E>>
Overrides:
contains in class AbstractStructure<E extends java.lang.Comparable<E>>
Parameters:
value - The value sought in the list
Returns:
The actual value found, or null, if not
Precondition:
value is a non-null comparable object
Postcondition:
returns true iff contains value

remove

public E remove(E value)
Remove a value from the ordered list. At most one value is removed.

Specified by:
remove in interface Structure<E extends java.lang.Comparable<E>>
Parameters:
value - The value to be removed
Returns:
The actual value removed from the list
Precondition:
value is non-null
Postcondition:
an instance of value is removed, if in list

size

public int size()
Determine the number of elements in the list

Specified by:
size in interface Structure<E extends java.lang.Comparable<E>>
Returns:
The number of elements in the list
Precondition:
returns the number of elements in the ordered list

isEmpty

public boolean isEmpty()
Determine if the list is empty

Specified by:
isEmpty in interface Structure<E extends java.lang.Comparable<E>>
Overrides:
isEmpty in class AbstractStructure<E extends java.lang.Comparable<E>>
Returns:
True if the ordered list is empty
Postcondition:
returns true iff the size is non-zero

iterator

public java.util.Iterator<E> iterator()
Construct an iterator for traversing elements of ordered list in ascending order

Specified by:
iterator in interface java.lang.Iterable<E extends java.lang.Comparable<E>>
Specified by:
iterator in interface Structure<E extends java.lang.Comparable<E>>
Returns:
An iterator traversing elements in ascending order
See Also:
AbstractIterator, Iterator, Enumeration, Structure.elements()
Postcondition:
returns an iterator over ordered list

toString

public java.lang.String toString()
Generate string representation of the ordered list

Overrides:
toString in class java.lang.Object
Returns:
String representing ordered list
Postcondition:
returns string representation of list