KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.openide.src.Type;
23 import java.util.Comparator JavaDoc;
24
25 class TypeComparator extends JavaElementComparator {
26
27     private Comparator JavaDoc idComparator;
28
29     protected TypeComparator(int type) {
30         super(type);
31     }
32
33     // this duplicate functionality of org.openide.src.Type because kind is private filed, and there is no access method
34
private static final int T_BOOLEAN = 0x0001;
35     private static final int T_INT = 0x0002;
36     private static final int T_CHAR = 0x0003;
37     private static final int T_BYTE = 0x0004;
38     private static final int T_SHORT = 0x0005;
39     private static final int T_LONG = 0x0006;
40     private static final int T_FLOAT = 0x0007;
41     private static final int T_DOUBLE = 0x0008;
42     private static final int T_VOID = 0x0009;
43
44     private static final int T_CLASS = 0x0010;
45     private static final int T_ARRAY = 0x0020;
46     
47     private int getKind(Type t)
48     { if (t.isArray())
49             return T_ARRAY;
50         else if (t.isClass())
51             return T_CLASS;
52         else if (t==Type.BOOLEAN)
53             return T_BOOLEAN;
54         else if (t==Type.BYTE)
55             return T_BYTE;
56         else if (t==Type.CHAR)
57             return T_CHAR;
58         else if (t==Type.DOUBLE)
59             return T_DOUBLE;
60         else if (t==Type.FLOAT)
61             return T_FLOAT;
62         else if (t==Type.INT)
63             return T_INT;
64         else if (t==Type.LONG)
65             return T_LONG;
66         else if (t==Type.SHORT)
67             return T_SHORT;
68         else if (t==Type.VOID)
69             return T_VOID;
70         return 0; // nonsence kind
71
}
72     
73     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
74         Type t1=(Type)o1;
75         Type t2=(Type)o2;
76         int t1_kind=getKind(t1);
77         int t2_kind=getKind(t2);
78         
79         if (t1_kind!=t2_kind) {
80             if (t1_kind>t2_kind) return 1;
81             else return -1;
82         }
83         if (t1.isArray())
84             return compare(t1.getElementType(),t2.getElementType());
85         else if (t1.isClass()) {
86             if (idComparator == null)
87                 idComparator=IdentifierComparator.createComparator(type);
88             return idComparator.compare(t1.getClassName(),t2.getClassName());
89         }
90         else
91             return 0;
92     }
93     
94     static Comparator JavaDoc createComparator(int type) {
95         return new TypeComparator(type);
96     }
97     
98 }
99
100
Popular Tags