View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.dcd.graph;
5   
6   import java.util.Comparator;
7   
8   /**
9    * Compares ClassNodes by the name of the class.  Can also compare String class
10   * names to ClassNodes.
11   */
12  public class ClassNodeComparator implements Comparator {
13  
14  	public static final ClassNodeComparator INSTANCE = new ClassNodeComparator();
15  
16  	private ClassNodeComparator() {
17  	}
18  
19  	public int compare(Object obj1, Object obj2) {
20  		if (obj1 instanceof String && obj2 instanceof String) {
21  			return ((String)obj1).compareTo((String)obj2);
22  		} else if (obj1 instanceof String) {
23  			return ((String)obj1).compareTo(((ClassNode)obj2).getName());
24  		} else if (obj2 instanceof String) {
25  			return ((ClassNode)obj1).getName().compareTo((String)obj2);
26  		} else {
27  			return ((ClassNode)obj1).compareTo((ClassNode)obj2);
28  		}
29  	}
30  }