1 package net.sourceforge.jenesis4java.impl.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 import java.io.Serializable;
41
42 import net.sourceforge.jenesis4java.ClassDeclaration;
43 import net.sourceforge.jenesis4java.Constructor;
44 import net.sourceforge.jenesis4java.Field;
45 import net.sourceforge.jenesis4java.Interface;
46 import net.sourceforge.jenesis4java.Member;
47 import net.sourceforge.jenesis4java.Method;
48 import net.sourceforge.jenesis4java.StaticInitializer;
49
50
51
52
53 public class MemberComparator implements java.util.Comparator<Member>, Serializable {
54
55 private static final long serialVersionUID = 2419912166943712377L;
56
57 private static final int CONSTANT = 3;
58
59 private static final int INTERFACE = 4;
60
61 private static final int CONSTRUCTOR = 5;
62
63 private static final int METHOD = 6;
64
65 private static final int FIELD = 7;
66
67 private static final int CLASS = 8;
68
69 private static final int STATIC_INIT = 9;
70
71 private static final int OTHER = 10;
72
73 private static final int EQUAL = 0;
74
75 private static final int LESS = -1;
76
77 private static final int GREATER = 1;
78
79 public MemberComparator() {
80 }
81
82 @Override
83 public int compare(Member o1, Member o2) {
84
85 int i1 = assignInt(o1);
86 int i2 = assignInt(o2);
87
88
89 if (i1 == i2) {
90
91
92
93 switch (i1) {
94 case CLASS:
95 return compareClasses((ClassDeclaration) o1, (ClassDeclaration) o2);
96 case METHOD:
97 return compareMethods((Method) o1, (Method) o2);
98 case FIELD:
99 return compareFields((Field) o1, (Field) o2);
100 case CONSTRUCTOR:
101 return compareConstructors((Constructor) o1, (Constructor) o2);
102 case INTERFACE:
103 return compareInterfaces((Interface) o1, (Interface) o2);
104 default:
105 return MemberComparator.EQUAL;
106 }
107
108
109
110 } else {
111 return i1 < i2 ? MemberComparator.LESS : MemberComparator.GREATER;
112 }
113 }
114
115 private int assignInt(Object o) {
116 if (o instanceof Interface) {
117 return MemberComparator.INTERFACE;
118 } else if (o instanceof Constructor) {
119 return MemberComparator.CONSTRUCTOR;
120 } else if (o instanceof Method) {
121 return MemberComparator.METHOD;
122 } else if (o instanceof Field) {
123
124
125 Field f = (Field) o;
126 return f.isStatic() && f.isFinal() ? MemberComparator.CONSTANT : MemberComparator.FIELD;
127 } else if (o instanceof ClassDeclaration) {
128 return MemberComparator.CLASS;
129 } else if (o instanceof StaticInitializer) {
130 return MemberComparator.STATIC_INIT;
131 } else {
132 return MemberComparator.OTHER;
133 }
134 }
135
136
137
138
139
140 private int compareBooleans(boolean b1, boolean b2) {
141
142 if (b1 ^ b2) {
143 return b1 ? MemberComparator.GREATER : MemberComparator.LESS;
144 } else {
145 return MemberComparator.EQUAL;
146 }
147 }
148
149 private int compareClasses(ClassDeclaration o1, ClassDeclaration o2) {
150
151 int x = compareBooleans(o1.isAbstract(), o2.isAbstract());
152 if (x != MemberComparator.EQUAL) {
153 return x;
154 }
155
156 return compareMembers(o1, o2);
157 }
158
159 private int compareConstructors(Constructor o1, Constructor o2) {
160
161 int x = compareInts(o1.getAccess().ordinal(), o2.getAccess().ordinal());
162 if (x != MemberComparator.EQUAL) {
163 return x;
164 }
165
166
167
168 return x;
169 }
170
171 private int compareFields(Field o1, Field o2) {
172 return compareMembers(o1, o2);
173 }
174
175 private int compareInterfaces(Interface o1, Interface o2) {
176
177 int x = compareStrings(o1.getName(), o2.getName());
178 if (x != MemberComparator.EQUAL) {
179 return x;
180 }
181
182
183 return x;
184 }
185
186
187
188
189
190 private int compareInts(int i1, int i2) {
191 if (i1 == i2) {
192 return MemberComparator.EQUAL;
193 } else {
194 return i1 < i2 ? MemberComparator.LESS : MemberComparator.GREATER;
195 }
196 }
197
198 private int compareMembers(Member o1, Member o2) {
199
200 int x = compareInts(o1.getAccess().ordinal(), o2.getAccess().ordinal());
201 if (x != MemberComparator.EQUAL) {
202 return x;
203 }
204
205
206 x = compareBooleans(o1.isStatic(), o2.isStatic());
207 if (x != MemberComparator.EQUAL) {
208 return x;
209 }
210
211
212 x = compareBooleans(o1.isFinal(), o2.isFinal());
213 if (x != MemberComparator.EQUAL) {
214 return x;
215 }
216
217
218 x = compareStrings(o1.getName(), o2.getName());
219 if (x != MemberComparator.EQUAL) {
220 return x;
221 }
222
223
224 return x;
225 }
226
227 private int compareMethods(Method o1, Method o2) {
228 return compareMembers(o1, o2);
229 }
230
231
232
233
234
235 private int compareStrings(String s1, String s2) {
236
237 return s1.compareTo(s2);
238 }
239 }