Skip to content

Commit ffb3b5b

Browse files
committed
Refactored PR #27 + added comparison operators
1 parent caf73bf commit ffb3b5b

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

javaobj/core.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
# Standard library
3737
import collections
38+
import functools
3839
import logging
3940
import os
4041
import struct
@@ -1712,32 +1713,35 @@ def __extra_loading__(self, unmarshaller, ident=0):
17121713
# Lists have their content in there annotations
17131714
self.extend(self.annotations[1:])
17141715

1715-
class JavaBool(JavaObject):
1716+
@functools.total_ordering
1717+
class JavaPrimitiveClass(JavaObject):
1718+
"""
1719+
Parent of Java classes matching a primitive (Bool, Integer, Long, ...)
1720+
"""
17161721
def __init__(self, unmarshaller):
17171722
JavaObject.__init__(self)
17181723
self.value = None
1719-
pass
17201724

17211725
def __str__(self):
1722-
return self.value.__str__()
1726+
return str(self.value)
17231727

17241728
def __repr__(self):
1725-
return self.value.__repr__()
1726-
1727-
def __bool__(self):
1728-
return self.value
1729+
return repr(self.value)
17291730

1730-
class JavaInt(JavaObject):
1731-
def __init__(self, unmarshaller):
1732-
self.value = None
1733-
JavaObject.__init__(self)
1731+
def __hash__(self):
1732+
return hash(self.value)
17341733

1735-
def __str__(self):
1736-
return self.value.__str__()
1734+
def __eq__(self, other):
1735+
return self.value == other
17371736

1738-
def __repr__(self):
1739-
return self.value.__repr__()
1740-
1737+
def __lt__(self, other):
1738+
return self.value < other
1739+
1740+
class JavaBool(JavaPrimitiveClass):
1741+
def __bool__(self):
1742+
return self.value
1743+
1744+
class JavaInt(JavaPrimitiveClass):
17411745
def __int__(self):
17421746
return self.value
17431747

0 commit comments

Comments
 (0)