diff --git a/pycommons/base/utils/__init__.py b/pycommons/base/utils/__init__.py index 5ee629e..c09427e 100644 --- a/pycommons/base/utils/__init__.py +++ b/pycommons/base/utils/__init__.py @@ -1,3 +1,4 @@ from .objectutils import ObjectUtils +from .utils import UtilityClass -__all__ = ["ObjectUtils"] +__all__ = ["ObjectUtils", "UtilityClass"] diff --git a/pycommons/base/utils/objectutils.py b/pycommons/base/utils/objectutils.py index f501347..82bbc05 100644 --- a/pycommons/base/utils/objectutils.py +++ b/pycommons/base/utils/objectutils.py @@ -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 diff --git a/tests/pycommons/base/utils/test_objectutils.py b/tests/pycommons/base/utils/test_objectutils.py index cec2593..401802a 100644 --- a/tests/pycommons/base/utils/test_objectutils.py +++ b/tests/pycommons/base/utils/test_objectutils.py @@ -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) @@ -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"))