File: Modifier.py

package info (click to toggle)
jython 2.5.1-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 41,624 kB
  • ctags: 101,579
  • sloc: python: 351,444; java: 204,338; xml: 1,316; sh: 330; ansic: 126; perl: 114; makefile: 94
file content (50 lines) | stat: -rw-r--r-- 1,213 bytes parent folder | download | duplicates (2)
1
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
41
42
43
44
45
46
47
48
49
50
# Copyright (c) Corporation for National Research Initiatives
names = {
    1024: 'abstract',
      16: 'final',
     512: 'interface',
     256: 'native',
       2: 'private',
       4: 'protected',
       1: 'public',
       8: 'static',
      32: 'synchronized',
     128: 'transient',
      64: 'volatile'
    }

numbers = {}
for number, name in names.items():
    numbers[name] = number

def Modifier(base=0, **kw):
    """Construct the appropriate integer to represent
    modifiers for a method, class, or field declaration.
    This should probably do some error checking:
    "public private" doesn't make much sense.
    """
    for name, value in kw.items():
        if value:
            base = base | numbers[name]
    return base

def ModifierString(modifier):
    if type(modifier) == type(""): return modifier
    text = []
    base = 1
    while base <= modifier:
        if modifier & base:
            text.append(names[base])
        base = base * 2
    SPACE = ' '
    return SPACE.join(text)



if __name__ == '__main__':
    m0 = Modifier(0)
    m1 = Modifier(public=1, final=1)
    m2 = Modifier(1023)
    print ModifierString(m0)
    print ModifierString(m1)
    print ModifierString(m2)