Class DBusPromise<T>

java.lang.Object
com.lucimber.dbus.util.DBusPromise<T>

public class DBusPromise<T> extends Object
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;
     });
 
  • Method Details

    • from

      public static DBusPromise<InboundMessage> from(CompletionStage<InboundMessage> messageStage)
      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

      public static <U> DBusPromise<U> completed(U value)
      Creates a DBusPromise from a value.
      Type Parameters:
      U - the value type
      Parameters:
      value - the value
      Returns:
      a completed DBusPromise
    • failed

      public static <U> DBusPromise<U> failed(Throwable error)
      Creates a failed DBusPromise.
      Type Parameters:
      U - the value type
      Parameters:
      error - the error
      Returns:
      a failed DBusPromise
    • timeout

      public DBusPromise<T> timeout(Duration duration)
      Applies a timeout to the operation.
      Parameters:
      duration - the timeout duration
      Returns:
      a new DBusPromise with timeout
    • map

      public <U> DBusPromise<U> map(Function<? super T,? extends U> mapper)
      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

      public DBusPromise<List<DBusType>> mapReturn()
      Maps an InboundMessage to its return payload. Automatically handles error responses.
      Returns:
      a new DBusPromise with the payload
    • firstAs

      public <U extends DBusType> DBusPromise<U> firstAs(Class<U> type)
      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

      public <U> DBusPromise<U> as(Class<U> type)
      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

      public DBusPromise<Void> thenAccept(Consumer<? super T> action)
      Handles both success and failure cases.
      Parameters:
      action - the action to perform
      Returns:
      a new DBusPromise
    • exceptionally

      public DBusPromise<T> exceptionally(Function<Throwable,? extends T> fn)
      Handles exceptions.
      Parameters:
      fn - the exception handler
      Returns:
      a new DBusPromise
    • toCompletableFuture

      public CompletableFuture<T> toCompletableFuture()
      Converts to a CompletableFuture.
      Returns:
      the underlying CompletableFuture
    • get

      public T get() throws Exception
      Gets the result synchronously.
      Returns:
      the result
      Throws:
      Exception - if the operation fails
    • get

      public T get(long timeout, TimeUnit unit) throws Exception
      Gets the result synchronously with a timeout.
      Parameters:
      timeout - the timeout value
      unit - the timeout unit
      Returns:
      the result
      Throws:
      Exception - if the operation fails or times out