KickJava   Java API By Example, From Geeks To Geeks.

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


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.ConstructorElement;
23 import org.openide.src.MethodParameter;
24 import org.openide.src.Identifier;
25 import java.util.Comparator JavaDoc;
26
27 class ConstructorComparator extends MemberNameComparator {
28     protected Comparator JavaDoc idComparator;
29     protected Comparator JavaDoc paramComparator;
30
31     protected ConstructorComparator(int type) {
32         super(type);
33         int source_type=type&SOURCE;
34         int paramType = source_type;
35
36         if ((type & PARAM_NAME) > 0)
37             paramType |= NAME;
38         if ((type & PARAM_MODIFIERS) > 0)
39             paramType |= MODIFIERS;
40         if ((type & PARAM_TYPE) > 0)
41             paramType |= TYPE;
42         
43         if ((type&EXCEPTIONS)!=0) idComparator=IdentifierComparator.createComparator(source_type);
44         if ((type&PARAMETERS)!=0) paramComparator=MParameterComparator.createComparator(paramType);
45     }
46
47     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
48         ConstructorElement ce1=(ConstructorElement)o1;
49         ConstructorElement ce2=(ConstructorElement)o2;
50         int result,i;
51
52         if ((type&NAME)!=0) {
53             result=super.compare(ce1,ce2);
54             if (result!=0) return result;
55         }
56         if ((type&EXCEPTIONS)!=0) {
57             Identifier[] excA = ce1.getExceptions();
58             Identifier[] excB = ce2.getExceptions();
59
60             if (excA.length != excB.length) {
61                 return excA.length-excB.length>0?1:-1;
62             }
63             for (i = 0; i < excA.length; i++) {
64                 result=idComparator.compare(excA[i],excB[i]);
65                 if (result!=0) return result;
66             }
67         }
68         if (paramComparator!=null) {
69             MethodParameter[] paramA = ce1.getParameters();
70             MethodParameter[] paramB = ce2.getParameters();
71
72             if (paramA.length != paramB.length) {
73                 return paramA.length-paramB.length>0?1:-1;
74             }
75             for (i = 0; i < paramA.length; i++) {
76                 result=paramComparator.compare(paramA[i],paramB[i]);
77                 if (result!=0) return result;
78             }
79         }
80         return 0;
81     }
82         
83     static Comparator JavaDoc createComparator(int type) {
84         return new ConstructorComparator(type);
85     }
86     
87 }
88
89
Popular Tags