Selfie
Loading...
Searching...
No Matches
Atomic.py
Go to the documentation of this file.
1from typing import Callable, Generic, TypeVar
2
3T = TypeVar("T")
4
5
6class AtomicReference(Generic[T]):
7 """
8 This has the same API as Java's AtomicReference, but it doesn't make any sense in the Python runtime.
9 The point of keeping it is that it makes the port from Kotlin more 1:1
10 """
11
12 def __init__(self, initial_value: T):
13 self.value: T = initial_value
14
15 def get(self) -> T:
16 return self.value
17
18 def set(self, new_value: T) -> None:
19 self.value = new_value
20
21 def get_and_set(self, new_value: T) -> T:
22 old_value = self.value
23 self.value = new_value
24 return old_value
25
26 def compare_and_set(self, expected_value: T, new_value: T) -> bool:
27 if self.value == expected_value:
28 self.value = new_value
29 return True
30 return False
31
32 def get_and_update(self, update_function: Callable[[T], T]) -> T:
33 old_value = self.value
34 self.value = update_function(self.value)
35 return old_value
36
37 def update_and_get(self, update_function: Callable[[T], T]) -> T:
38 self.value = update_function(self.value)
39 return self.value
40
41 def get_and_accumulate(self, x: T, accumulator: Callable[[T, T], T]) -> T:
42 old_value = self.value
43 self.value = accumulator(self.value, x)
44 return old_value
45
46 def accumulate_and_get(self, x: T, accumulator: Callable[[T, T], T]) -> T:
47 self.value = accumulator(self.value, x)
48 return self.value
T get_and_update(self, Callable[[T], T] update_function)
Definition Atomic.py:32
bool compare_and_set(self, T expected_value, T new_value)
Definition Atomic.py:26
T get_and_accumulate(self, T x, Callable[[T, T], T] accumulator)
Definition Atomic.py:41
T get_and_set(self, T new_value)
Definition Atomic.py:21
__init__(self, T initial_value)
Definition Atomic.py:12
T update_and_get(self, Callable[[T], T] update_function)
Definition Atomic.py:37
None set(self, T new_value)
Definition Atomic.py:18
T accumulate_and_get(self, T x, Callable[[T, T], T] accumulator)
Definition Atomic.py:46