Package com.lucimber.dbus.util
Class DBusPromise<T>
java.lang.Object
com.lucimber.dbus.util.DBusPromise<T>
Promise-style utilities for working with D-Bus asynchronous operations.
This class provides a fluent API for handling D-Bus responses with better error handling and type conversion.
Example usage:
DBusPromise.from(connection.sendRequest(methodCall))
.timeout(Duration.ofSeconds(5))
.mapReturn(payload -> payload.get(0))
.as(DBusString.class)
.thenAccept(result -> System.out.println("Result: " + result))
.exceptionally(error -> {
System.err.println("Failed: " + error.getMessage());
return null;
});
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Exception thrown when a D-Bus error is received.static class
Exception thrown when a D-Bus operation times out. -
Method Summary
Modifier and TypeMethodDescription<U> DBusPromise<U>
Casts the result to a specific type.static <U> DBusPromise<U>
completed
(U value) Creates a DBusPromise from a value.exceptionally
(Function<Throwable, ? extends T> fn) Handles exceptions.static <U> DBusPromise<U>
Creates a failed DBusPromise.<U extends DBusType>
DBusPromise<U>Maps the first element of a payload list.static DBusPromise<InboundMessage>
from
(CompletionStage<InboundMessage> messageStage) Creates a DBusPromise from a D-Bus message completion stage.get()
Gets the result synchronously.Gets the result synchronously with a timeout.<U> DBusPromise<U>
Maps the result using a function.Maps an InboundMessage to its return payload.thenAccept
(Consumer<? super T> action) Handles both success and failure cases.Applies a timeout to the operation.Converts to a CompletableFuture.
-
Method Details
-
from
Creates a DBusPromise from a D-Bus message completion stage.- Parameters:
messageStage
- the completion stage from a D-Bus request- Returns:
- a new DBusPromise
-
completed
Creates a DBusPromise from a value.- Type Parameters:
U
- the value type- Parameters:
value
- the value- Returns:
- a completed DBusPromise
-
failed
Creates a failed DBusPromise.- Type Parameters:
U
- the value type- Parameters:
error
- the error- Returns:
- a failed DBusPromise
-
timeout
Applies a timeout to the operation.- Parameters:
duration
- the timeout duration- Returns:
- a new DBusPromise with timeout
-
map
Maps the result using a function.- Type Parameters:
U
- the new type- Parameters:
mapper
- the mapping function- Returns:
- a new DBusPromise with the mapped value
-
mapReturn
Maps an InboundMessage to its return payload. Automatically handles error responses.- Returns:
- a new DBusPromise with the payload
-
firstAs
Maps the first element of a payload list.- Type Parameters:
U
- the D-Bus type- Parameters:
type
- the expected D-Bus type class- Returns:
- a new DBusPromise with the first element
-
as
Casts the result to a specific type.- Type Parameters:
U
- the target type- Parameters:
type
- the target type class- Returns:
- a new DBusPromise with the cast value
-
thenAccept
Handles both success and failure cases.- Parameters:
action
- the action to perform- Returns:
- a new DBusPromise
-
exceptionally
Handles exceptions.- Parameters:
fn
- the exception handler- Returns:
- a new DBusPromise
-
toCompletableFuture
Converts to a CompletableFuture.- Returns:
- the underlying CompletableFuture
-
get
Gets the result synchronously.- Returns:
- the result
- Throws:
Exception
- if the operation fails
-
get
Gets the result synchronously with a timeout.- Parameters:
timeout
- the timeout valueunit
- the timeout unit- Returns:
- the result
- Throws:
Exception
- if the operation fails or times out
-