1 7 package org.jboss.cache; 8 9 import java.util.Comparator ; 10 11 23 public class FqnComparator implements Comparator <Fqn> 24 { 25 public static final FqnComparator INSTANCE = new FqnComparator(); 26 27 30 public FqnComparator() 31 { 32 } 33 34 38 public int compare(Fqn fqn1, Fqn fqn2) 39 { 40 int s1 = fqn1.size(); 41 int s2 = fqn2.size(); 42 43 if (s1 == 0) 44 { 45 return (s2 == 0) ? 0 : -1; 46 } 47 48 if (s2 == 0) 49 { 50 return 1; 51 } 52 53 int size = Math.min(s1, s2); 54 55 for (int i = 0; i < size; i++) 56 { 57 Object e1 = fqn1.get(i); 58 Object e2 = fqn2.get(i); 59 if (e1 == e2) 60 continue; 61 if (e1 == null) 62 return 0; 63 if (e2 == null) 64 return 1; 65 if (!e1.equals(e2)) 66 { 67 int c = compareElements(e1, e2); 68 if (c != 0) 69 return c; 70 } 71 } 72 73 return s1 - s2; 74 } 75 76 82 private int compareElements(Object e1, Object e2) 83 { 84 if (e1.getClass() == e2.getClass() && e1 instanceof Comparable ) 85 { 86 return ((Comparable ) e1).compareTo(e2); 87 } 88 else 89 { 90 return e1.toString().compareTo(e2.toString()); 91 } 92 } 93 94 95 } 96 | Popular Tags |