Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pycommons/base/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .objectutils import ObjectUtils
from .utils import UtilityClass

__all__ = ["ObjectUtils"]
__all__ = ["ObjectUtils", "UtilityClass"]
30 changes: 30 additions & 0 deletions pycommons/base/utils/objectutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,33 @@ def require_not_none(cls, t: Optional[_T], e: Optional[_E] = None) -> None:
def get_not_none(cls, t: Optional[_T], e: Optional[_E] = None) -> _T:
cls.require_not_none(t, e)
return typing.cast(_T, t)

@classmethod
def is_any_none(cls, *args: Optional[_T]) -> bool:
return not cls.is_all_not_none(*args)

@classmethod
def is_any_not_none(cls, *args: Optional[_T]) -> bool:
return cls.first_not_none(*args) is not None

@classmethod
def is_all_none(cls, *args: Optional[_T]) -> bool:
return cls.first_not_none(*args) is None

@classmethod
def is_all_not_none(cls, *args: Optional[_T]) -> bool:
for arg in args:
if arg is None:
return False
return True

@classmethod
def first_not_none(cls, *args: Optional[_T]) -> Optional[_T]:
for arg in args:
if arg is not None:
return arg
return None

@classmethod
def default_if_none(cls, obj: Optional[_T], default: _T) -> _T:
return default if obj is None else obj
30 changes: 30 additions & 0 deletions tests/pycommons/base/utils/test_objectutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@


class TestObjectUtils(TestCase):
def test_constructor(self):
with self.assertRaises(ValueError):
ObjectUtils()

def test_require_not_none_without_error(self):
with self.assertRaises(ValueError):
ObjectUtils.require_not_none(None)
Expand All @@ -19,3 +23,29 @@ def test_require_not_none_with_non_null_value(self):
def test_get_not_none(self):
obj = object()
self.assertEqual(obj, ObjectUtils.get_not_none(obj))

def test_any_none_when_none_of_the_object_is_none(self):
obj1 = object()
obj2 = object()
obj3 = object()
self.assertFalse(ObjectUtils.is_any_none(obj1, obj2, obj3))
self.assertTrue(ObjectUtils.is_any_not_none(obj1, obj2, obj3))
self.assertTrue(ObjectUtils.is_all_not_none(obj1, obj2, obj3))

def test_any_none_when_one_of_the_object_is_none(self):
obj1 = object()
obj2 = None
self.assertTrue(ObjectUtils.is_any_none(obj1, obj2))
self.assertTrue(ObjectUtils.is_any_not_none(obj1, obj2))

def test_any_not_none_when_all_of_the_object_is_none(self):
self.assertFalse(ObjectUtils.is_any_not_none(None, None, None))
self.assertTrue(ObjectUtils.is_all_none(None, None, None))
self.assertFalse(ObjectUtils.is_all_not_none(None, None, None))

def test_default_if_none_when_object_is_not_none(self):
obj = object()
self.assertEqual(obj, ObjectUtils.default_if_none(obj, "DEFAULT"))

def test_default_if_none_when_object_is_none(self):
self.assertEqual("DEFAULT", ObjectUtils.default_if_none(None, "DEFAULT"))