1 import java.util.ArrayList;
  2 import java.util.List;
  3 
  4 /**
  5  * This is just an example of a Node object which is comparable to other Node objects
  6  * based on the comparison result of the data which it contains.  The trickiest part
  7  * of this implementation is really just getting the generic typing specified correctly.
  8  * Wading though multiple layers of type abstraction requires very careful thought and
  9  * is not necessarily trivial, though the following type declarations should make sense.
 10  *
 11  * This is Node contains items of type T which are comparable to each other and each
 12  * Node is comparable to other Nodes which also contain items comparable the same type.
 13  * The "? super T" portion could be replaced with just "T", but it would force all
 14  * comparable nodes to contain the exact same type, and not allow for the comparison
 15  * of nodes containing "polymorphic types" of each other.
 16  *
 17  * @author Adam J. Conover, D.Sc. <aconover@towson.edu>
 18  * @param <T>  The type of data contained within the node.
 19  */
 20 class Node<T extends Comparable<? super T>> implements Comparable<Node<T>> {
 21     // The data
 22 
 23     private T data;
 24 
 25     /* simple constructor to set the node data to the contructor parameter */
 26     public Node(T data) {
 27         this.data = data;
 28     }
 29 
 30     /* Get the data element from the node */
 31     public T getData() {
 32         return data;
 33     }
 34 
 35     /* Compare to other nodes by comparing the data elements. */
 36     @Override
 37     public int compareTo(Node<T> other) {
 38         // return the result of the comparison of the data elements.
 39         return (this.data.compareTo(other.data));
 40     }
 41 
 42     /* When displaying the node, display the contents and the node info (for debugging) */
 43     @Override
 44     public String toString() {
 45         String str = String.format("%s (%s)", data.toString(), super.toString());
 46         return str;
 47     }
 48 }
 49 
 50 
 51 
 52 
 53 /**
 54  * Comparability example:  This example uses the Node defined above to
 55  * illustrate the usage and comparison of nodes.
 56  *
 57  * @author Adam J. Conover, D.Sc. <aconover@towson.edu>
 58  */
 59 public class ComparabilityExample {
 60 
 61     public static void main(String[] args) {
 62         // Create some nodes
 63         Node<String> s1 = new Node<String>("Apple");
 64         Node<String> s2 = new Node<String>("Zucchini");
 65         Node<Integer> i1 = new Node<Integer>(3);
 66         Node<Integer> i2 = new Node<Integer>(7);
 67 
 68         // Compare some nodes
 69         Node<String> largeString = getLargestNode(s1, s2);
 70         System.out.println("Largest String is: " + largeString);
 71 
 72         // Compare some nodes
 73         Node<Integer> largeInt = getLargestNode(i1, i2);
 74         System.out.println("Largest Integer is: " + largeInt);
 75 
 76         // Sorry... wont compile!  (Uncomment to test)
 77 //        System.out.println("Largest ? is: " + getLargestNode(s1, i1));
 78 
 79 
 80         //Create a list of nodes
 81         List<Node<String>> list = new ArrayList<Node<String>>(2);
 82         list.add(s1);
 83         list.add(s2);
 84         list.add(new Node<String>("zebra"));
 85 
 86         System.out.println("Largest List member is: " + getLargestNode(list));
 87     }
 88 
 89     /* This method accepts two comparable nodes which contain data elements which
 90      * are comparable to each other. */
 91     static <T extends Comparable<? super T>> Node<T> getLargestNode(Node<T> node1, Node<T> node2) {
 92         if (node1.compareTo(node2) >= 0) {
 93             return node1;
 94         } else {
 95             return node2;
 96         }
 97     }
 98 
 99     /* This method accepts a list of nodes and returns the largest. */
100     static <T extends Comparable<? super T>> Node<T> getLargestNode(List<Node<T>> nodeList) {
101         Node<T> largest = nodeList.get(0);
102 
103         // Find largest
104         for (Node<T> node : nodeList) {
105             if (node.compareTo(largest) > 0) {
106                 largest = node;
107             }
108         }
109 
110         return largest;
111     }
112 }
113