1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """Simple two-way map."""
17
18
20 """C{KeyMapper} is the simple two-way map for generating textual keys
21 for objects and retrieving the objects later with the key.
22
23 @author: Vaadin Ltd.
24 @author: Richard Lincoln
25 @version: 1.1.2
26 """
27
29 self._lastKey = 0
30 self._objectKeyMap = dict()
31 self._keyObjectMap = dict()
32
33
35 """Gets key for an object.
36
37 @param o: the object.
38 """
39 if o is None:
40 return 'null'
41
42
43 key = self._objectKeyMap.get(o)
44 if key is not None:
45 return key
46
47
48 self._lastKey += 1
49 key = str(self._lastKey)
50 self._objectKeyMap[o] = key
51 self._keyObjectMap[key] = o
52 return key
53
54
56 """Retrieves object with the key.
57
58 @param key:
59 the name with the desired value.
60 @return: the object with the key.
61 """
62 return self._keyObjectMap.get(key)
63
64
66 """Removes object from the mapper.
67
68 @param removeobj:
69 the object to be removed.
70 """
71 key = self._objectKeyMap.get(removeobj)
72 if key is not None:
73 del self._objectKeyMap[removeobj]
74 if key in self._keyObjectMap:
75 del self._keyObjectMap[key]
76
77
79 """Removes all objects from the mapper."""
80 self._objectKeyMap.clear()
81 self._keyObjectMap.clear()
82