jivago.lang.nullable module

exception jivago.lang.nullable.EmptyNullableException[source]

Bases: Exception

class jivago.lang.nullable.Nullable(nullable: T | None)[source]

Bases: Generic[T]

Nullable class which wraps Optional types.

Parameters:

nullable (Optional) – Item which can be None.

static empty() Nullable[source]
filter(predicate: Callable[[T], bool] | Callable[[...], bool]) Nullable[T][source]

Filters item given a criterion.

Parameters:

predicate (Callable) – Invoked with a non-nil parameter. Should return a boolean.

get() T[source]

Gets the item if present.

Raises:

EmptyNullableException – Attempting to get a missing item.

ifPresent(consumer: Callable[[T], None] | Callable[[...], None]) Nullable[T][source]

Invoke function if value is present; otherwise does nothing.

Parameters:

consumer (Callable) – Function to be invoked with a non-nil parameter.

isPresent() bool[source]

Returns True if item is not None.

map(callable: Callable[[T], S] | Callable[[...], S]) Nullable[S][source]

Maps the item when present.

Parameters:

callable (Callable) – Invoked with a non-nil parameter.

orElse(default_value: T) T[source]

Returns the item if present, else return the supplied default value.

Parameters:

default_value – Value to return instead of a None value.

orElseFetch(supplier: Callable[[], T]) T[source]

Returns if present, invoke callable if missing.

Parameters:

supplier (Callable) – Supplied return value will be return in place of a None value. Should not require parameters.

orElseThrow(exception: Exception | Callable[[], Exception]) T[source]

Returns if present, raises exception if missing.

Parameters:

exception – Either an exception, or a callable which returns an exception.