|
1 | 1 | class Optional: |
2 | 2 |
|
| 3 | + ''' |
| 4 | + A container object which may or may not contain a non-null value. If a value is present, isPresent() returns true. If no value is present, the object is considered empty and isPresent() returns false. |
| 5 | +
|
| 6 | + Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (returns a default value if no value is present) and ifPresent() (performs an action if a value is present). |
| 7 | + ''' |
| 8 | + |
| 9 | + ''' |
| 10 | + STATIC METHODS |
| 11 | + ''' |
| 12 | + |
| 13 | + @staticmethod |
| 14 | + def empty(): |
| 15 | + ''' |
| 16 | + Returns an empty Optional instance. No value is present for this Optional. |
| 17 | +
|
| 18 | + :return: an empty Optional |
| 19 | + ''' |
| 20 | + return Optional(None) |
| 21 | + |
3 | 22 | @staticmethod |
4 | 23 | def of(elem): |
| 24 | + ''' |
| 25 | + Returns an Optional describing the given non-null value. |
| 26 | +
|
| 27 | + :param T elem: the value to describe, which must be non-null |
| 28 | + :return: an Optional with the value present |
| 29 | + :raise: Exception if the value is null |
| 30 | + ''' |
5 | 31 | if elem is None: |
6 | 32 | raise Exception("Optional Empty") |
7 | 33 | return Optional(elem) |
8 | 34 |
|
9 | 35 | @staticmethod |
10 | 36 | def ofNullable(elem): |
| 37 | + ''' |
| 38 | + Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional. |
| 39 | +
|
| 40 | + :param T elem: the possibly-null value to describe |
| 41 | + :return: an Optional with a present value if the specified value is non-null, otherwise an empty Optional |
| 42 | + ''' |
11 | 43 | return Optional(elem) |
12 | 44 |
|
| 45 | + ''' |
| 46 | + NON STATIC METHODS |
| 47 | + ''' |
| 48 | + |
13 | 49 | def __init__(self, elem): |
14 | 50 | self.__elem = elem |
15 | 51 |
|
| 52 | + def get(self): |
| 53 | + ''' |
| 54 | + If a value is present, returns the value, otherwise raise an Exception. |
| 55 | +
|
| 56 | + :return: the non-null value described by this Optional |
| 57 | + :raise: Exception if no value is present |
| 58 | + ''' |
| 59 | + if self.isEmpty(): |
| 60 | + raise Exception("Optional Empty") |
| 61 | + return self.__elem |
| 62 | + |
16 | 63 | def isPresent(self): |
| 64 | + ''' |
| 65 | + If a value is present, returns true, otherwise false. |
| 66 | +
|
| 67 | + :return: true if a value is present, otherwise false |
| 68 | + ''' |
17 | 69 | return not self.isEmpty() |
18 | 70 |
|
19 | 71 | def isEmpty(self): |
| 72 | + ''' |
| 73 | + If a value is not present, returns true, otherwise false. |
| 74 | +
|
| 75 | + :return: true if a value is not present, otherwise false |
| 76 | + ''' |
20 | 77 | return self.__elem is None |
21 | 78 |
|
22 | | - def get(self): |
23 | | - if self.isEmpty(): |
24 | | - raise Exception("Optional Empty") |
25 | | - return self.__elem |
| 79 | + def ifPresent(self, action): |
| 80 | + ''' |
| 81 | + If a value is present, performs the given action with the value, otherwise does nothing. |
| 82 | +
|
| 83 | + :param Function action: the action to be performed, if a value is present |
| 84 | + :raise Exception if value is present and the given action is null |
| 85 | + ''' |
| 86 | + if self.isPresent(): |
| 87 | + action(self.get()) |
| 88 | + |
| 89 | + def ifPresentOrElse(self, action, emptyAction): |
| 90 | + ''' |
| 91 | + If a value is present, performs the given action with the value, otherwise performs the given empty-based action. |
| 92 | +
|
| 93 | + :param Function action: the action to be performed, if a value is present |
| 94 | + :param Function emptyAction: the empty-based action to be performed, if no value is present |
| 95 | + :raise Exception if a value is present and the given action is null, or no value is present and the given empty-based action is null. |
| 96 | + ''' |
| 97 | + if self.isPresent(): |
| 98 | + action(self.get()) |
| 99 | + else: |
| 100 | + emptyAction() |
| 101 | + |
| 102 | + def filter(self, predicate): |
| 103 | + ''' |
| 104 | + If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional. |
| 105 | +
|
| 106 | + :param Predicate predicate: the predicate to apply to a value, if present |
| 107 | + :return: an Optional describing the value of this Optional, if a value is present and the value matches the given predicate, otherwise an empty Optional |
| 108 | + ''' |
| 109 | + if self.isPresent() and predicate(self.get()): |
| 110 | + return self |
| 111 | + else: |
| 112 | + return Optional.empty() |
| 113 | + |
| 114 | + def map(self, mapper): |
| 115 | + ''' |
| 116 | + If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional. |
| 117 | + If the mapping function returns a null result then this method returns an empty Optional. |
| 118 | +
|
| 119 | + :param Mapper mapper: the mapping function to apply to a value, if present |
| 120 | + :return: an Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise an empty Optional |
| 121 | + ''' |
| 122 | + if self.isPresent(): |
| 123 | + return Optional.ofNullable(mapper(self.get())) |
| 124 | + else: |
| 125 | + return Optional.empty() |
0 commit comments