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
86 changes: 28 additions & 58 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pycommons/lang/atomic/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .atomic import Atomic

__all__ = ["Atomic"]
31 changes: 31 additions & 0 deletions pycommons/lang/atomic/atomic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from __future__ import annotations

from typing import TypeVar, Generic, Any, Optional

from pycommons.lang.function import Supplier

_T = TypeVar("_T")


class Atomic(Generic[_T], Supplier[Optional[_T]]):
def get(self) -> Optional[_T]:
return self._object

def __init__(self, t: Optional[_T] = None):
self._object: Optional[_T] = t

def set(self, t: _T) -> None:
self._object = t

def set_and_get(self, t: Optional[_T]) -> Optional[_T]:
self._object = t
return self._object

def get_and_set(self, t: Optional[_T]) -> Optional[_T]:
old_object = self._object
self._object = t
return old_object

@classmethod
def with_none(cls) -> Atomic[Any]:
return cls()
30 changes: 30 additions & 0 deletions pycommons/lang/atomic/boolean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from __future__ import annotations

import typing

from pycommons.lang.atomic.atomic import Atomic


class AtomicBoolean(Atomic[bool]):
def __init__(self, flag: bool = False):
super().__init__(flag)

def true(self) -> bool:
return typing.cast(bool, self.set_and_get(True))

def false(self) -> bool:
return typing.cast(bool, self.set_and_get(False))

def compliment(self) -> bool:
return typing.cast(bool, self.set_and_get(not self.get()))

@classmethod
def with_true(cls) -> AtomicBoolean:
return cls(True)

@classmethod
def with_false(cls) -> AtomicBoolean:
return cls(False)

def get(self) -> bool:
return typing.cast(bool, super().get())
56 changes: 56 additions & 0 deletions pycommons/lang/atomic/integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import typing

from pycommons.lang.atomic.atomic import Atomic


class AtomicInteger(Atomic[int]):
def __init__(self, value: int = 0):
super().__init__(value)

def add(self, val: int) -> None:
self.set(self.get() + val)

def add_and_get(self, val: int) -> int:
return typing.cast(int, self.set_and_get(self.get() + val))

def get_and_add(self, val: int) -> int:
return typing.cast(int, self.get_and_set(self.get() + val))

def increment(self) -> None:
return self.add(1)

def increment_and_get(self) -> int:
return self.add_and_get(1)

def get_and_increment(self) -> int:
return self.get_and_add(1)

def subtract(self, val: int) -> None:
return self.add(-val)

def subtract_and_get(self, val: int) -> int:
return self.add_and_get(-val)

def get_and_subtract(self, val: int) -> int:
return self.get_and_add(-val)

def get(self) -> int:
return typing.cast(int, super().get())

def __int__(self) -> int:
return self.get()

def __le__(self, other: int) -> bool:
return self.get() <= other

def __lt__(self, other: int) -> bool:
return self.get() < other

def __ge__(self, other: int) -> bool:
return self.get() >= other

def __gt__(self, other: int) -> bool:
return self.get() > other

def __eq__(self, other: object) -> bool:
return self.get() == other
48 changes: 48 additions & 0 deletions pycommons/lang/function/comparator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from __future__ import annotations

from abc import abstractmethod
from typing import TypeVar, Generic, Callable, Any

_T = TypeVar("_T")
_U = TypeVar("_U")


class Comparator(Generic[_T, _U]):
@classmethod
def of(cls, comparator: Callable[[_T, _U], int]) -> Comparator[_T, _U]:
class BasicComparator(Comparator[_T, _U]):
def compare_to(self, t: _T, u: _U) -> int:
return comparator(t, u)

return BasicComparator()

@abstractmethod
def compare_to(self, t: _T, u: _U) -> int:
...

def reversed(self) -> Comparator[_T, _U]:
return ReverseOrderComparator(self)

def __call__(self, t: _T, u: _U, *args: Any, **kwargs: Any) -> int:
return self.compare_to(t, u)


class NaturalOrderComparator(Comparator[_T, _U]):
def compare_to(self, t: _T, u: _U) -> int:
if t < u: # type: ignore
return -1
if t == u:
return 0
return 1


class ReverseOrderComparator(Comparator[_T, _U]):
def __init__(self, comparator: Comparator[_T, _U]):
self.comparator = comparator

def compare_to(self, t: _T, u: _U) -> int:
if t < u: # type: ignore
return 1
if t == u:
return 0
return -1
6 changes: 4 additions & 2 deletions pycommons/lang/function/consumer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from abc import abstractmethod
from typing import TypeVar, Generic, Callable, Any

_T = TypeVar("_T")
Expand All @@ -15,6 +16,7 @@ def accept(self, value: _T) -> None:

return BasicConsumer()

@abstractmethod
def accept(self, value: _T) -> None:
pass

Expand All @@ -25,8 +27,8 @@ def _impl(_t: _T) -> None:

return Consumer.of(_impl)

def __call__(self, *args: _T, **kwargs: Any) -> None:
self.accept(args[0])
def __call__(self, t: _T, *args: Any, **kwargs: Any) -> None:
self.accept(t)


class BiConsumer(Generic[_T, _U]):
Expand Down
6 changes: 4 additions & 2 deletions pycommons/lang/function/function.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from abc import abstractmethod
from typing import TypeVar, Generic, Callable, Any

_T = TypeVar("_T")
Expand All @@ -15,8 +16,9 @@ def apply(self, t: _T) -> _U:

return BasicFunction()

@abstractmethod
def apply(self, t: _T) -> _U:
pass

def __call__(self, *args: Any, **kwargs: Any) -> _U:
return self.apply(args[0])
def __call__(self, t: _T, *args: Any, **kwargs: Any) -> _U:
return self.apply(t)
16 changes: 14 additions & 2 deletions pycommons/lang/function/predicate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from abc import abstractmethod
from typing import TypeVar, Generic, Callable, Any

from pycommons.lang.objectutils import ObjectUtils
Expand All @@ -18,8 +19,19 @@ def test(self, value: _T) -> bool:

return BasicPredicate()

@abstractmethod
def test(self, value: _T) -> bool:
pass

def __call__(self, *args: _T, **kwargs: Any) -> bool:
return self.test(args[0])
def __call__(self, t: _T, *args: Any, **kwargs: Any) -> bool:
return self.test(t)


class PassingPredicate(Predicate[_T]):
def test(self, value: _T) -> bool:
return True


class FailingPredicate(Predicate[_T]):
def test(self, value: _T) -> bool:
return False
Loading