javaclass

java/util.py

146:1dc70ce13680
2005-01-23 Paul Boddie Added more tests.
     1 #!/usr/bin/env python     2      3 import java.lang     4      5 # Interfaces.     6      7 class Collection(java.lang.Object):     8     pass     9     10 class Iterator(java.lang.Object):    11     pass    12     13 class List(java.lang.Object):    14     pass    15     16 class Map(java.lang.Object):    17     pass    18     19 class Set(Collection):    20     pass    21     22 # Abstract classes.    23     24 class AbstractMap(Map):    25     pass    26     27 class Dictionary(java.lang.Object):    28     pass    29     30 # Exceptions.    31     32 class NoSuchElementException(java.lang.Exception):    33     pass    34     35 # Special Python classes.    36     37 class _Iterator(Iterator):    38     def __init__(self, iterator):    39         self.iterator = iterator    40         self.current = None    41     42     def hasNext(self):    43         if self.current is None:    44             try:    45                 self.current = self.iterator.next()    46             except StopIteration:    47                 self.current = None    48                 return 0    49         return 1    50     hasNext___ = hasNext    51     52     def next(self):    53         if self.hasNext():    54             current = self.current    55             self.current = None    56             return current    57         raise Exception, NoSuchElementException()    58     next___ = next    59     60 # Classes.    61     62 class EventObject(java.lang.Object):    63     def __init__(self, source):    64         self.source = source    65     def getSource(self):    66         return self.source    67     def toString(self):    68         # NOTE: Use Python conventions.    69         return str(self)    70     71 class Hashtable(Dictionary):    72     def __init__(self, *args):    73         # NOTE: To be implemented.    74         pass    75     76 setattr(Hashtable, "__init_____", Hashtable.__init__)    77     78 class HashMap(AbstractMap):    79     def __init__(self, *args):    80         self.d = {}    81     82     def init_____java__util__Map(self, map):    83         self.d = {}    84         iterator = map.keySet().iterator()    85         while iterator.hasNext():    86             key = iterator.next()    87             value = map.get(key)    88             self.d[key] = value    89     90     def get(self, key):    91         return self.d[key]    92     get___java__lang__Object = get    93     94     def keySet(self):    95         return HashSet(self.d.keys())    96     keySet___ = keySet    97     98     def put(self, key, value):    99         self.d[key] = value   100     put___java__lang__Object___java__lang__Object = put   101    102     # Python helper methods.   103    104     def __getitem__(self, key):   105         return self.d[key]   106    107     def __setitem__(self, key, value):   108         self.d[key] = value   109    110     def keys(self):   111         return self.d.keys()   112    113     def values(self):   114         return self.d.values()   115    116     def items(self):   117         return self.d.items()   118    119     # NOTE: Private interface for cases where the above methods are not enough.   120    121     # def as_dict(self):   122         # return self.d   123    124 setattr(HashMap, "__init_____", HashMap.__init__)   125 setattr(HashMap, "__init_____java__util__Map", HashMap.init_____java__util__Map)   126    127 class HashSet(Set):   128     def __init__(self):   129         self.s = []   130    131     def iterator(self):   132         return _Iterator(iter(self.s))   133     iterator___ = iterator   134    135 setattr(HashSet, "__init_____", HashSet.__init__)   136    137 class ResourceBundle(java.lang.Object):   138     def __init__(self, *args):   139         # NOTE: To be implemented.   140         pass   141    142     def getBundle(self, *args):   143         # getBundle(self, baseName)   144         # getBundle(self, baseName, locale)   145         # getBundle(self, baseName, locale, loader)   146         # NOTE: Obviously not the correct implementation.   147         return ResourceBundle(args)   148     getBundle = staticmethod(getBundle)   149     getBundle___java__lang__String = getBundle   150     getBundle___java__lang__String___java__util__Locale = getBundle   151     getBundle___java__lang__String___java__util__Locale___java__lang__ClassLoader = getBundle   152    153     def getObject(self, key):   154         # NOTE: To be implemented.   155         return None   156     getObject___java__lang__String = getObject   157    158     def getString(self, key):   159         # NOTE: To be implemented.   160         return None   161     getString___java__lang__String = getString   162    163     def getStringArray(self, key):   164         # NOTE: To be implemented.   165         return None   166     getStringArray___java__lang__String = getStringArray   167    168     def getLocale(self, key):   169         # NOTE: To be implemented.   170         return None   171     getLocale___ = getLocale   172    173 class AbstractCollection(Collection):   174     pass   175    176 class AbstractList(AbstractCollection, List):   177     pass   178    179 class Vector(AbstractList):   180     pass   181    182 # vim: tabstop=4 expandtab shiftwidth=4