structure5
Class ComparableAssociation<K extends java.lang.Comparable<K>,V>

java.lang.Object
  extended by structure5.Association<K,V>
      extended by structure5.ComparableAssociation<K,V>
All Implemented Interfaces:
java.lang.Comparable<ComparableAssociation<K,V>>, java.util.Map.Entry<K,V>

public class ComparableAssociation<K extends java.lang.Comparable<K>,V>
extends Association<K,V>
implements java.lang.Comparable<ComparableAssociation<K,V>>, java.util.Map.Entry<K,V>

A class implementing a comparable key-value pair. This class associates an immutable key with a mutable value. Useful for many other structures. Example usage:

To print out a list of professors sorted by the number of classes a particular student took from each, we could use the following:

 public static void main(String[] argv){
      //initialize a new fib heap
      FibHeap classesTaken = new FibHeap();

      //add professors and classes taken to a heap
      classesTaken.add(new ComparableAssociation(new Integer(5), "Andrea"));
      classesTaken.add(new ComparableAssociation(new Integer(1), "Barbara"));
      classesTaken.add(new ComparableAssociation(new Integer(3), "Bill"));
      classesTaken.add(new ComparableAssociation(new Integer(2), "Duane"));   
      classesTaken.add(new ComparableAssociation(new Integer(1), "Tom"));     

      //print out classes taken
      while(!classesTaken.isEmpty()){
          ComparableAssociation p = (ComparableAssociation)classesTaken.remove();
          System.out.println(p.getValue() + " is " + p.getKey() + " years old.");
      }
 }
 


Field Summary
 
Fields inherited from class structure5.Association
theKey, theValue
 
Constructor Summary
ComparableAssociation(K key)
          Construct an association that can be ordered, from only a key.
ComparableAssociation(K key, V value)
          Construct a key-value association that can be ordered.
 
Method Summary
 int compareTo(ComparableAssociation<K,V> that)
          Determine the order of two comparable associations, based on key.
 java.lang.String toString()
          Construct a string representation of the ComparableAssociation.
 
Methods inherited from class structure5.Association
equals, getKey, getValue, hashCode, setValue
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map.Entry
equals, getKey, getValue, hashCode, setValue
 

Constructor Detail

ComparableAssociation

public ComparableAssociation(K key)
Construct an association that can be ordered, from only a key. The value is set to null.

Parameters:
key - The (comparable) key.
Precondition:
key is non-null
Postcondition:
constructs comparable association with null value

ComparableAssociation

public ComparableAssociation(K key,
                             V value)
Construct a key-value association that can be ordered.

Parameters:
key - The (comparable) key.
value - The (possibly comparable) associated value.
Precondition:
key is non-null
Postcondition:
constructs association between a comparable key and a value
Method Detail

compareTo

public int compareTo(ComparableAssociation<K,V> that)
Determine the order of two comparable associations, based on key.

Specified by:
compareTo in interface java.lang.Comparable<ComparableAssociation<K extends java.lang.Comparable<K>,V>>
Parameters:
other - The other comparable association.
Returns:
Value less-than equal to or greater than zero based on comparison
Precondition:
other is non-null ComparableAssociation
Postcondition:
returns integer representing relation between values

toString

public java.lang.String toString()
Construct a string representation of the ComparableAssociation.

Overrides:
toString in class Association<K extends java.lang.Comparable<K>,V>
Returns:
The string representing the ComparableAssociation.
Postcondition:
returns string representation