1 19 20 package org.netbeans.api.java.comparators; 21 22 import java.util.Comparator ; 23 import java.util.HashMap ; 24 import java.lang.reflect.Method ; 25 import org.openide.src.*; 26 27 public abstract class JavaElementComparator implements Comparator { 28 29 protected int type; 30 private static HashMap element2comparator; 31 32 33 public static final int SOURCE = 1; 34 public static final int RETURN_TYPE = 2; 35 public static final int NAME = 4; 36 public static final int PARAMETERS = 8; 37 public static final int EXCEPTIONS = 16; 38 public static final int TYPE = 32; 39 public static final int MODIFIERS = 64; 40 public static final int ISCLASS = 128; 41 public static final int PARAM_NAME = 256; 42 public static final int PARAM_MODIFIERS = 512; 43 public static final int PARAM_TYPE = 1024; 44 45 protected JavaElementComparator(int t) { 46 type=t; 47 } 48 49 protected int getType() { 50 return type; 51 } 52 53 public abstract int compare(Object o1, Object o2); 54 55 public boolean equals(Object ob) { 56 return getClass().isInstance(ob) && type==((JavaElementComparator)ob).getType(); 57 } 58 59 public static Comparator createIdentifierComparator(boolean source) { 60 return IdentifierComparator.createComparator(source?SOURCE:0); 61 } 62 63 public static Comparator createTypeComparator(boolean source) { 64 return TypeComparator.createComparator(source?SOURCE:0); 65 } 66 67 public static Comparator createClassComparator(boolean source, int ordering[]) { 68 return createComparator(ClassElement.class,source,ordering); 69 } 70 71 public static Comparator createConstructorComparator(boolean source, int ordering[]) { 72 return createComparator(ConstructorElement.class,source,ordering); 73 } 74 75 public static Comparator createFiledComparator(boolean source, int ordering[]) { 76 return createComparator(FieldElement.class,source,ordering); 77 } 78 79 public static Comparator createMParameterComparator(boolean source, int ordering[]) { 80 return createComparator(MethodParameter.class,source,ordering); 81 } 82 83 public static Comparator createMethodComparator(boolean source, int ordering[]) { 84 return createComparator(MethodElement.class,source,ordering); 85 } 86 87 public static Comparator createComparator(Object element,boolean source, int ordering[]) { 88 return createComparator(element.getClass(),source,ordering); 89 } 90 91 public static Comparator createComparator(Class elementClass, boolean source, int ordering[]) { 92 int i; 93 int sourceType=source?SOURCE:0; 94 Method createCompMethod; 95 Comparator comparators[] = new Comparator [ordering.length]; 96 97 if (element2comparator==null) { 98 synchronized (JavaElementComparator.class) { 99 if (element2comparator == null) 100 createElementMap(); 101 } 102 } 103 createCompMethod=(Method )element2comparator.get(elementClass); 104 if (createCompMethod==null) 105 return null; if (ordering==null || ordering.length==0) { 107 try { 108 Integer type=new Integer (sourceType); 109 return (Comparator )createCompMethod.invoke(null,new Object [] {type}); 110 } catch (Exception ex) { 111 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, ex); 112 } 113 return null; 114 } 115 for (i=0;i<ordering.length;i++) { 116 try { 117 Integer type=new Integer (ordering[i]|sourceType); 118 comparators[i]=(Comparator )createCompMethod.invoke(null,new Object [] {type}); 119 } catch (Exception ex) { 120 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, ex); 121 return null; 122 } 123 } 124 return new ArrayComparator(comparators); 125 } 126 127 private static void createElementMap() { 128 Class args[] = new Class [] { Integer.TYPE }; 129 130 element2comparator=new HashMap (); 131 try { 132 element2comparator.put(ClassElement.class,ClassComparator.class.getDeclaredMethod("createComparator",args)); element2comparator.put(ConstructorElement.class,ConstructorComparator.class.getDeclaredMethod("createComparator",args)); element2comparator.put(FieldElement.class,FieldComparator.class.getDeclaredMethod("createComparator",args)); element2comparator.put(Identifier.class,IdentifierComparator.class.getDeclaredMethod("createComparator",args)); element2comparator.put(MethodParameter.class,MParameterComparator.class.getDeclaredMethod("createComparator",args)); element2comparator.put(MethodElement.class,MethodComparator.class.getDeclaredMethod("createComparator",args)); element2comparator.put(Type.class,TypeComparator.class.getDeclaredMethod("createComparator",args)); } catch (NoSuchMethodException ex) { 140 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, ex); 141 } 142 } 143 144 } 145 146 | Popular Tags |