From 7c6fe31032f1a6f7621746af1acb02853cb62c14 Mon Sep 17 00:00:00 2001 From: Matt Nash Date: Mon, 14 Apr 2014 14:48:24 -0500 Subject: [PATCH] Issue #3019 modify JUtil to support sets and frozensets Change-Id: Ia72d8c1670c0adb1529937b1943fa9e2d1195692 Former-commit-id: 02e801b359271f67d952c11e2242821911da73f8 [formerly 795cba334038d920beab93f5c872b02acf29c7f9] Former-commit-id: ef3879c4a389835830b54aa321a34cc02601ccb5 --- .../common_static/base/python/JUtilHandler.py | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/edexOsgi/com.raytheon.uf.common.python/utility/common_static/base/python/JUtilHandler.py b/edexOsgi/com.raytheon.uf.common.python/utility/common_static/base/python/JUtilHandler.py index 7fef449aa2..1ae332971f 100644 --- a/edexOsgi/com.raytheon.uf.common.python/utility/common_static/base/python/JUtilHandler.py +++ b/edexOsgi/com.raytheon.uf.common.python/utility/common_static/base/python/JUtilHandler.py @@ -171,7 +171,7 @@ fallbackBasics = OrderedDict({Number:_toPythonFloat}) The following methods will handle Python and Java collection conversion. ''' from java.lang import Object -from java.util import Collections, HashMap, LinkedHashMap, ArrayList +from java.util import Collections, HashMap, LinkedHashMap, ArrayList, HashSet from java.util import Date from java.lang.reflect import Array from java.util import List, Set, Map @@ -226,23 +226,24 @@ def _toPythonDict(obj, customConverter=None): ''' Converts to a Python dict. ''' - return __toPythonDict(obj, {}, customConverter) + return __toPythonDictInternal(obj, {}, customConverter) def _toPythonSet(obj, customConverter=None): ''' Converts to a Python set. ''' retVal = set() - size = obj.size() - for i in range(size): - retVal.add(JUtil.javaObjToPyVal(obj.get(i), customConverter)) + itr = obj.iterator() + while itr.hasNext(): + val = itr.next() + retVal.add(JUtil.javaObjToPyVal(val, customConverter)) return retVal def _toPythonOrderedDict(obj, customConverter=None): ''' Converts to a Python OrderedDict. ''' - return __toPythonDict(obj, OrderedDict(), customConverter) + return __toPythonDictInternal(obj, OrderedDict(), customConverter) def _fromJavaArray(obj, customConverter=None): ''' @@ -264,7 +265,7 @@ def _fromJepArray(obj, customConverter=None): retVal.append(JUtil.javaObjToPyVal(obj[i], customConverter)) return retVal -def __toPythonDict(javaMap, pyDict, customConverter=None): +def __toPythonDictInternal(javaMap, pyDict, customConverter=None): ''' Converts to a Python dict. Passed in the dict type, and then handles the key conversion. ''' @@ -311,15 +312,37 @@ def _toJavaLinkedMap(val): ''' Turns a Python OrderedDict to a Java LinkedHashMap ''' - return __toJavaMap(val, LinkedHashMap()) + return __toJavaMapInternal(val, LinkedHashMap()) def _toJavaMap(val): ''' Turns a Python dict to a Java HashMap ''' - return __toJavaMap(val, HashMap()) + return __toJavaMapInternal(val, HashMap()) -def __toJavaMap(pyDict, jmap): +def _toJavaSet(val): + ''' + Turns a Python set to a Java set + ''' + return __toJavaSetInternal(val) + +def _toJavaUnmodifiableSet(val): + ''' + Turns a Python frozenset to a Java unmodifiableset + ''' + return Collections.unmodifiableSet(__toJavaSetInternal(val)) + +def __toJavaSetInternal(val): + ''' + Does the actual conversion of the elements inside of the set or frozenset to Set + ''' + retObj = HashSet() + for v in val : + retObj.add(JUtil.pyValToJavaObj(v)) + return retObj + + +def __toJavaMapInternal(pyDict, jmap): ''' Does the actual conversion of the elements inside of the dict to Map ''' @@ -328,7 +351,7 @@ def __toJavaMap(pyDict, jmap): return jmap javaCollections = OrderedDict({'java.util.ArrayList':_toPythonList, 'java.util.Arrays$ArrayList':_toPythonList, 'java.util.Collections$UnmodifiableRandomAccessList':_toPythonTuple, 'java.util.HashMap':_toPythonDict, 'java.util.LinkedHashMap':_toPythonOrderedDict}) -pythonCollections = OrderedDict({ list:_toJavaList, tuple:_toJavaUnmodifiableList, OrderedDict:_toJavaLinkedMap, dict:_toJavaMap }) +pythonCollections = OrderedDict({ list:_toJavaList, tuple:_toJavaUnmodifiableList, OrderedDict:_toJavaLinkedMap, dict:_toJavaMap, set:_toJavaSet, frozenset:_toJavaUnmodifiableSet }) fallbackCollections = OrderedDict({ List:_toPythonList, Map:_toPythonDict, Set:_toPythonSet }) '''