KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > types > Flags


1 package polyglot.types;
2
3 import polyglot.util.InternalCompilerError;
4 import polyglot.util.Enum;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.*;
8
9 /**
10  * <code>Flags</code> is an immutable set of class, method, or field modifiers.
11  * We represent package scope as the absence of private, public and protected
12  * scope modifiers.
13  */

14 public class Flags implements Serializable JavaDoc
15 {
16     /** List of all flag bits in order in which they should be printed. */
17     static final int[] print_order = new int[64];
18     static int next_bit = 0;
19
20     /** Names of 1-bit flags indexed by flag bit. */
21     static final String JavaDoc[] flag_names = new String JavaDoc[64];
22
23     public static final Flags NONE = new Flags(0);
24     public static final Flags PUBLIC = createFlag("public", null);
25     public static final Flags PRIVATE = createFlag("private", null);
26     public static final Flags PROTECTED = createFlag("protected", null);
27     public static final Flags STATIC = createFlag("static", null);
28     public static final Flags FINAL = createFlag("final", null);
29     public static final Flags SYNCHRONIZED = createFlag("synchronized", null);
30     public static final Flags TRANSIENT = createFlag("transient", null);
31     public static final Flags NATIVE = createFlag("native", null);
32     public static final Flags INTERFACE = createFlag("interface", null);
33     public static final Flags ABSTRACT = createFlag("abstract", null);
34     public static final Flags VOLATILE = createFlag("volatile", null);
35     public static final Flags STRICTFP = createFlag("strictfp", null);
36
37     /** All access flags. */
38     protected static final Flags ACCESS_FLAGS = PUBLIC.set(PRIVATE).set(PROTECTED);
39
40     /** Bit set use to implement a flag set. */
41     protected long bits;
42
43     /**
44      * Return a new Flags object with a new name. Should be called only once
45      * per name.
46      *
47      * @param name the name of the new flag
48      * @param after the flags after which this flag should be printed;
49      * Flags.NONE to print before all other flags, null
50      * if we should print at the end.
51      */

52     public static Flags createFlag(String JavaDoc name, Flags after) {
53         if (next_bit >= flag_names.length)
54             throw new InternalCompilerError("too many flags");
55         if (print_order[next_bit] != 0)
56             throw new InternalCompilerError("print_order and next_bit " +
57                                             "inconsistent");
58         if (flag_names[next_bit] != null)
59             throw new InternalCompilerError("flag_names and next_bit " +
60                                             "inconsistent");
61
62         int bit = next_bit++;
63         flag_names[bit] = name;
64
65         if (after == null) {
66             print_order[bit] = bit;
67         }
68         else {
69             for (int i = bit; i > 0; i--) {
70                 if ((after.bits & print_order[i]) != 0)
71                     break;
72
73                 // shift up and fill in the gap with f
74
print_order[i] = print_order[i-1];
75                 print_order[i-1] = bit;
76             }
77         }
78
79         return new Flags(1L << bit);
80     }
81
82     /**
83      * Effects: returns a new accessflags object with no accessflags set.
84      */

85     protected Flags(long bits) {
86         this.bits = bits;
87     }
88
89     /**
90      * Create new flags with the flags in <code>other</code> also set.
91      */

92     public Flags set(Flags other) {
93         return new Flags(bits | other.bits);
94     }
95
96     /**
97      * Create new flags with the flags in <code>other</code> cleared.
98      */

99     public Flags clear(Flags other) {
100         return new Flags(bits & ~other.bits);
101     }
102
103     /**
104      * Create new flags with only flags in <code>other</code> set.
105      */

106     public Flags retain(Flags other) {
107         return new Flags(bits & other.bits);
108     }
109
110     /**
111      * Check if <i>any</i> flags in <code>other</code> are set.
112      */

113     public boolean intersects(Flags other) {
114         return (bits & other.bits) != 0;
115     }
116
117     /**
118      * Check if <i>all</i> flags in <code>other</code> are set.
119      */

120     public boolean contains(Flags other) {
121         return (bits & other.bits) == other.bits;
122     }
123
124     /**
125      * Return a copy of this <code>this</code> with the <code>public</code>
126      * flag set.
127      */

128     public Flags Public() {
129     return set(PUBLIC);
130     }
131
132     /**
133      * Return a copy of this <code>this</code> with the <code>public</code>
134      * flag clear.
135      */

136     public Flags clearPublic() {
137     return clear(PUBLIC);
138     }
139
140     /**
141      * Return true if <code>this</code> has the <code>public</code> flag set.
142      */

143     public boolean isPublic() {
144     return contains(PUBLIC);
145     }
146
147     /**
148      * Return a copy of this <code>this</code> with the <code>private</code>
149      * flag set.
150      */

151     public Flags Private() {
152     return set(PRIVATE);
153     }
154
155     /**
156      * Return a copy of this <code>this</code> with the <code>private</code>
157      * flag clear.
158      */

159     public Flags clearPrivate() {
160     return clear(PRIVATE);
161     }
162
163     /**
164      * Return true if <code>this</code> has the <code>private</code> flag set.
165      */

166     public boolean isPrivate() {
167     return contains(PRIVATE);
168     }
169
170     /**
171      * Return a copy of this <code>this</code> with the <code>protected</code>
172      * flag set.
173      */

174     public Flags Protected() {
175     return set(PROTECTED);
176     }
177
178     /**
179      * Return a copy of this <code>this</code> with the <code>protected</code>
180      * flag clear.
181      */

182     public Flags clearProtected() {
183     return clear(PROTECTED);
184     }
185
186     /**
187      * Return true if <code>this</code> has the <code>protected</code> flag set.
188      */

189     public boolean isProtected() {
190     return contains(PROTECTED);
191     }
192
193     /**
194      * Return a copy of this <code>this</code> with no access flags
195      * (<code>public</code>, <code>private</code>, <code>protected</code>) set.
196      */

197     public Flags Package() {
198         return clear(ACCESS_FLAGS);
199     }
200
201     /**
202      * Return true if <code>this</code> has the no access flags
203      * (<code>public</code>, <code>private</code>, <code>protected</code>) set.
204      */

205     public boolean isPackage() {
206         return ! intersects(ACCESS_FLAGS);
207     }
208
209     /**
210      * Return a copy of this <code>this</code> with the <code>static</code>
211      * flag set.
212      */

213     public Flags Static() {
214     return set(STATIC);
215     }
216
217     /**
218      * Return a copy of this <code>this</code> with the <code>static</code>
219      * flag clear.
220      */

221     public Flags clearStatic() {
222     return clear(STATIC);
223     }
224
225     /**
226      * Return true if <code>this</code> has the <code>static</code> flag set.
227      */

228     public boolean isStatic() {
229     return contains(STATIC);
230     }
231
232     /**
233      * Return a copy of this <code>this</code> with the <code>final</code>
234      * flag set.
235      */

236     public Flags Final() {
237     return set(FINAL);
238     }
239
240     /**
241      * Return a copy of this <code>this</code> with the <code>final</code>
242      * flag clear.
243      */

244     public Flags clearFinal() {
245     return clear(FINAL);
246     }
247
248     /**
249      * Return true if <code>this</code> has the <code>final</code> flag set.
250      */

251     public boolean isFinal() {
252     return contains(FINAL);
253     }
254
255     /**
256      * Return a copy of this <code>this</code> with the
257      * <code>synchronized</code> flag set.
258      */

259     public Flags Synchronized() {
260     return set(SYNCHRONIZED);
261     }
262
263     /**
264      * Return a copy of this <code>this</code> with the
265      * <code>synchronized</code> flag clear.
266      */

267     public Flags clearSynchronized() {
268     return clear(SYNCHRONIZED);
269     }
270
271     /**
272      * Return true if <code>this</code> has the <code>synchronized</code> flag
273      * set.
274      */

275     public boolean isSynchronized() {
276     return contains(SYNCHRONIZED);
277     }
278
279     /**
280      * Return a copy of this <code>this</code> with the <code>transient</code>
281      * flag set.
282      */

283     public Flags Transient() {
284     return set(TRANSIENT);
285     }
286
287     /**
288      * Return a copy of this <code>this</code> with the <code>transient</code>
289      * flag clear.
290      */

291     public Flags clearTransient() {
292     return clear(TRANSIENT);
293     }
294
295     /**
296      * Return true if <code>this</code> has the <code>transient</code> flag set.
297      */

298     public boolean isTransient() {
299     return contains(TRANSIENT);
300     }
301
302     /**
303      * Return a copy of this <code>this</code> with the <code>native</code>
304      * flag set.
305      */

306     public Flags Native() {
307     return set(NATIVE);
308     }
309
310     /**
311      * Return a copy of this <code>this</code> with the <code>native</code>
312      * flag clear.
313      */

314     public Flags clearNative() {
315     return clear(NATIVE);
316     }
317
318     /**
319      * Return true if <code>this</code> has the <code>native</code> flag set.
320      */

321     public boolean isNative() {
322     return contains(NATIVE);
323     }
324
325     /**
326      * Return a copy of this <code>this</code> with the <code>interface</code>
327      * flag set.
328      */

329     public Flags Interface() {
330     return set(INTERFACE);
331     }
332
333     /**
334      * Return a copy of this <code>this</code> with the <code>interface</code>
335      * flag clear.
336      */

337     public Flags clearInterface() {
338     return clear(INTERFACE);
339     }
340
341     /**
342      * Return true if <code>this</code> has the <code>interface</code> flag set.
343      */

344     public boolean isInterface() {
345     return contains(INTERFACE);
346     }
347
348     /**
349      * Return a copy of this <code>this</code> with the <code>abstract</code>
350      * flag set.
351      */

352     public Flags Abstract() {
353     return set(ABSTRACT);
354     }
355
356     /**
357      * Return a copy of this <code>this</code> with the <code>abstract</code>
358      * flag clear.
359      */

360     public Flags clearAbstract() {
361     return clear(ABSTRACT);
362     }
363
364     /**
365      * Return true if <code>this</code> has the <code>abstract</code> flag set.
366      */

367     public boolean isAbstract() {
368     return contains(ABSTRACT);
369     }
370
371     /**
372      * Return a copy of this <code>this</code> with the <code>volatile</code>
373      * flag set.
374      */

375     public Flags Volatile() {
376     return set(VOLATILE);
377     }
378
379     /**
380      * Return a copy of this <code>this</code> with the <code>volatile</code>
381      * flag clear.
382      */

383     public Flags clearVolatile() {
384     return clear(VOLATILE);
385     }
386
387     /**
388      * Return true if <code>this</code> has the <code>volatile</code> flag set.
389      */

390     public boolean isVolatile() {
391     return contains(VOLATILE);
392     }
393
394     /**
395      * Return a copy of this <code>this</code> with the <code>strictfp</code>
396      * flag set.
397      */

398     public Flags StrictFP() {
399     return set(STRICTFP);
400     }
401
402     /**
403      * Return a copy of this <code>this</code> with the <code>strictfp</code>
404      * flag clear.
405      */

406     public Flags clearStrictFP() {
407     return clear(STRICTFP);
408     }
409
410     /**
411      * Return true if <code>this</code> has the <code>strictfp</code> flag set.
412      */

413     public boolean isStrictFP() {
414     return contains(STRICTFP);
415     }
416
417     /**
418      * Return true if <code>this</code> has more restrictive access flags than
419      * <code>f</code>.
420      */

421     public boolean moreRestrictiveThan(Flags f) {
422         if (isPrivate() && (f.isProtected() || f.isPackage() || f.isPublic())) {
423             return true;
424         }
425
426         if (isPackage() && (f.isProtected() || f.isPublic())) {
427             return true;
428         }
429
430         if (isProtected() && f.isPublic()) {
431             return true;
432         }
433
434         return false;
435     }
436
437     public String JavaDoc toString() {
438         return translate().trim();
439     }
440
441     /**
442      * Return "" if no flags set, or toString() + " " if some flags are set.
443      */

444     public String JavaDoc translate() {
445         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
446
447         for (int i = 0; i < next_bit; i++) {
448             int bit = print_order[i];
449             if ((bits & (1L << bit)) != 0) {
450                 sb.append(flag_names[bit]);
451                 sb.append(" ");
452             }
453         }
454
455         return sb.toString();
456     }
457
458     public int hashCode() {
459         return (int) (bits >> 32 | bits) * 37;
460     }
461
462     public boolean equals(Object JavaDoc o) {
463     return o instanceof Flags && bits == ((Flags) o).bits;
464     }
465 }
466
Popular Tags