1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """
17 This module defines several functions to ease interfacing with Java code.
18
19 Initially based on resources in the following article http://www.ibm.com/developerworks/java/tutorials/j-jython2/index.html
20 """
21
22 import sys
23 from types import *
24 from java import util
25
26 __author__ = "Scott Horn"
27 __email__ = "scott@hornmicro.com"
28 __credits__ = "Based entirely on work by Tim Fox http://tfox.org"
29
31 """ Convert a Map to a Dictionary. """
32 result = {}
33 iter = map.keySet().iterator()
34 while iter.hasNext():
35 key = iter.next()
36 result[map_from_java(key)] = map_from_java(map.get(key))
37 return result
38
40 """ Convert a Set to a set. """
41 result = set()
42 iter = set_.iterator()
43 while iter.hasNext():
44 result.add(map_from_java(iter.next()))
45 return result
46
48 """ Convert a Collection to a List. """
49 result = []
50 iter = coll.iterator()
51 while iter.hasNext():
52 result.append(map_from_java(iter.next()))
53 return result
54
56 """ Convert a Java type to a Jython type. """
57 if object is None: return object
58 if isinstance(object, util.Map): result = map_map_from_java(object)
59 elif isinstance(object, util.Set): result = map_set_from_java(object)
60 elif isinstance(object, util.Collection): result = map_collection_from_java(object)
61 else: result = object
62 return result
63
65 """ Convert a seqence to a Java ArrayList. """
66 result = util.ArrayList(len(seq))
67 for e in seq:
68 result.add(map_to_java(e));
69 return result
70
72 """ Convert a List to a Java ArrayList. """
73 result = util.ArrayList(len(list))
74 for e in list:
75 result.add(map_to_java(e));
76 return result
77
79 """ Convert a List to a Java Vector. """
80 result = util.Vector(len(list))
81 for e in list:
82 result.add(map_to_java(e));
83 return result
84
86 """ Convert a Dictionary to a Java HashMap. """
87 result = util.HashMap()
88 for key, value in dict.items():
89 result.put(map_to_java(key), map_to_java(value))
90 return result
91
93 """ Convert a Jython type to a Java type. """
94 if object is None: return object
95 t = type(object)
96 if t == TupleType: result = map_seq_to_java(object)
97 elif t == ListType: result = map_seq_to_java(object)
98 elif t == DictType: result = map_dict_to_java(object)
99 else: result = object
100 return result
101