KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > comparators > JavaElementComparator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.java.comparators;
21
22 import java.util.Comparator JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import org.openide.src.*;
26
27 public abstract class JavaElementComparator implements Comparator JavaDoc {
28
29     protected int type;
30     private static HashMap JavaDoc element2comparator;
31
32     /* comparator types */
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 JavaDoc o1, Object JavaDoc o2);
54
55     public boolean equals(Object JavaDoc ob) {
56         return getClass().isInstance(ob) && type==((JavaElementComparator)ob).getType();
57     }
58         
59     public static Comparator JavaDoc createIdentifierComparator(boolean source) {
60         return IdentifierComparator.createComparator(source?SOURCE:0);
61     }
62
63     public static Comparator JavaDoc createTypeComparator(boolean source) {
64         return TypeComparator.createComparator(source?SOURCE:0);
65     }
66     
67     public static Comparator JavaDoc createClassComparator(boolean source, int ordering[]) {
68         return createComparator(ClassElement.class,source,ordering);
69     }
70
71     public static Comparator JavaDoc createConstructorComparator(boolean source, int ordering[]) {
72         return createComparator(ConstructorElement.class,source,ordering);
73     }
74
75     public static Comparator JavaDoc createFiledComparator(boolean source, int ordering[]) {
76         return createComparator(FieldElement.class,source,ordering);
77     }
78
79     public static Comparator JavaDoc createMParameterComparator(boolean source, int ordering[]) {
80         return createComparator(MethodParameter.class,source,ordering);
81     }
82
83     public static Comparator JavaDoc createMethodComparator(boolean source, int ordering[]) {
84         return createComparator(MethodElement.class,source,ordering);
85     }
86
87     public static Comparator JavaDoc createComparator(Object JavaDoc element,boolean source, int ordering[]) {
88         return createComparator(element.getClass(),source,ordering);
89     }
90     
91     public static Comparator JavaDoc createComparator(Class JavaDoc elementClass, boolean source, int ordering[]) {
92         int i;
93         int sourceType=source?SOURCE:0;
94         Method JavaDoc createCompMethod;
95         Comparator JavaDoc comparators[] = new Comparator JavaDoc[ordering.length];
96
97         if (element2comparator==null) {
98             synchronized (JavaElementComparator.class) {
99                 if (element2comparator == null)
100                     createElementMap();
101             }
102         }
103         createCompMethod=(Method JavaDoc)element2comparator.get(elementClass);
104         if (createCompMethod==null)
105             return null; // unsupported Class elementClass
106
if (ordering==null || ordering.length==0) {
107             try {
108                 Integer JavaDoc type=new Integer JavaDoc(sourceType);
109                 return (Comparator JavaDoc)createCompMethod.invoke(null,new Object JavaDoc[] {type});
110             } catch (Exception JavaDoc 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 JavaDoc type=new Integer JavaDoc(ordering[i]|sourceType);
118                 comparators[i]=(Comparator JavaDoc)createCompMethod.invoke(null,new Object JavaDoc[] {type});
119             } catch (Exception JavaDoc 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 JavaDoc args[] = new Class JavaDoc[] { Integer.TYPE };
129
130         element2comparator=new HashMap JavaDoc();
131         try {
132             element2comparator.put(ClassElement.class,ClassComparator.class.getDeclaredMethod("createComparator",args)); // NOI18N
133
element2comparator.put(ConstructorElement.class,ConstructorComparator.class.getDeclaredMethod("createComparator",args)); // NOI18N
134
element2comparator.put(FieldElement.class,FieldComparator.class.getDeclaredMethod("createComparator",args)); // NOI18N
135
element2comparator.put(Identifier.class,IdentifierComparator.class.getDeclaredMethod("createComparator",args)); // NOI18N
136
element2comparator.put(MethodParameter.class,MParameterComparator.class.getDeclaredMethod("createComparator",args)); // NOI18N
137
element2comparator.put(MethodElement.class,MethodComparator.class.getDeclaredMethod("createComparator",args)); // NOI18N
138
element2comparator.put(Type.class,TypeComparator.class.getDeclaredMethod("createComparator",args)); // NOI18N
139
} catch (NoSuchMethodException JavaDoc ex) {
140             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, ex);
141         }
142     }
143     
144 }
145
146
Popular Tags