Package core :: Module javautils |
[hide private]
[frames] | no frames]

# Source Code for Module core.javautils

      1  # Copyright 2011-2012 the original author or authors.
      2  #
      3  # Licensed under the Apache License, Version 2.0 (the "License");
      4  # you may not use this file except in compliance with the License.
      5  # You may obtain a copy of the License at
      6  #
      7  #      http://www.apache.org/licenses/LICENSE-2.0
      8  #
      9  # Unless required by applicable law or agreed to in writing, software
     10  # distributed under the License is distributed on an "AS IS" BASIS,
     11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  # See the License for the specific language governing permissions and
     13  # limitations under the License.
     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
     30 -def map_map_from_java (map):
     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
     39 -def map_set_from_java (set_):
     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
     47 -def map_collection_from_java (coll):
     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
     55 -def map_from_java (object):
     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
     64 -def map_seq_to_java (seq):
     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
     71 -def map_list_to_java (list):
     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
     78 -def map_list_to_java_vector (list):
     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
     85 -def map_dict_to_java (dict):
     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
     92 -def map_to_java (object):
     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