Coverage for src/kwai/core/domain/value_objects/identifier.py: 95%
20 statements
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
1"""Module that defines identifiers."""
3from abc import ABC, abstractmethod
6class Identifier[T](ABC):
7 """Abstract and generic class for an identifier."""
9 def __init__(self, id_: T):
10 self._id = id_
12 @property
13 def value(self) -> T:
14 """Return the id."""
15 return self._id
17 def __eq__(self, other: "Identifier"):
18 """Check the equality of identifiers."""
19 return self._id == other._id
21 def __hash__(self):
22 """Create a hash for an identifier."""
23 return hash(self._id)
25 @abstractmethod
26 def is_empty(self) -> bool:
27 """Return true when the identifier is not set."""
28 raise NotImplementedError
30 def __str__(self) -> str:
31 """Return the string representation of the id."""
32 return str(self._id)
35class IntIdentifier(Identifier[int]):
36 """Class that implements an identifier with an integer."""
38 def __init__(self, id_: int = 0):
39 super().__init__(id_)
41 def is_empty(self) -> bool:
42 """Return True when the id equals 0."""
43 return self.value == 0
45 def __repr__(self):
46 """Return a string representation of the identifier."""
47 return f"IntIdentifier={self.value}"