1 24 package org.riotfamily.common.collection; 25 26 import java.util.Comparator ; 27 28 43 public class TypeDifferenceComparator implements Comparator { 44 45 private Class targetClass; 46 47 public TypeDifferenceComparator(Class targetClass) { 48 this.targetClass = targetClass; 49 } 50 51 public int compare(Object o1, Object o2) { 52 return getTypeDifference((Class ) o1, targetClass) 53 - getTypeDifference((Class ) o2, targetClass); 54 } 55 56 69 public static int getTypeDifference(Class baseClass, Class subClass) { 70 if (!baseClass.isAssignableFrom(subClass)) { 71 return Integer.MAX_VALUE; 72 } 73 int result = 0; 74 Class superClass = subClass.getSuperclass(); 75 while (superClass != null) { 76 if (baseClass.isAssignableFrom(superClass)) { 77 result++; 78 superClass = superClass.getSuperclass(); 79 } 80 else { 81 superClass = null; 82 } 83 } 84 return result; 85 } 86 87 } 88 | Popular Tags |