Class Components

java.lang.Object
org.jgrapes.core.Components

public class Components extends Object
This class provides some utility functions.
  • Method Details

    • useVirtualThreads

      public static boolean useVirtualThreads()
      JGrapes uses virtual thread by default.

      However, as of 2024, some debuggers still have problems with virtual threads. Therefore it is possible to switch back to platform threads by starting the JVM with property -Djgrapes.useVirtualThreads=false.

      Returns:
      true, if successful
    • defaultExecutorService

      Return the default executor service for the framework.
      Returns:
      the defaultExecutorService
    • setDefaultExecutorService

      public static void setDefaultExecutorService(ExecutorService defaultExecutorService)
      Set the default executor service for the framework.

      The default value is a cached thread pool (see @link Executors.newCachedThreadPool()) with daemon threads.

      Parameters:
      defaultExecutorService - the executor service to set
    • manager

      public static Manager manager(ComponentType component)
      Returns a component’s manager.

      For a component that inherits from Component this method simply returns the component as it is its own manager.

      For components that implement ComponentType but don’t inherit from Component the method returns the value of the attribute annotated as manager slot. If this attribute is still empty, this method makes the component the root of a new tree and returns its manager.

      Parameters:
      component - the component
      Returns:
      the component (with its manager attribute set)
    • manager

      public static Manager manager(ComponentType component, Channel componentChannel)
      Returns a component’s manager like manager(ComponentType).

      If the manager slot attribute is empty, the component is initialized with its component channel set to the given parameter. Invoking this method overrides any channel set in the ComponentManager annotation.

      This method is usually invoked by the constructor of a class that implements ComponentType.

      Parameters:
      component - the component
      componentChannel - the channel that the component’s handlers listen on by default and that Manager.fire(Event, Channel...) sends the event to
      Returns:
      the component (with its manager attribute set)
      See Also:
    • start

      public static void start(ComponentType application) throws InterruptedException
      Fires a Start event with an associated Started completion event on the broadcast channel of the given application and wait for the completion of the Start event.
      Parameters:
      application - the application to start
      Throws:
      InterruptedException - if the execution was interrupted
    • awaitExhaustion

      public static void awaitExhaustion() throws InterruptedException
      Wait until all generators and event queues are exhausted.

      When this stage is reached, nothing can happen anymore unless a new event is sent from an external thread.

      Throws:
      InterruptedException - if the current thread was interrupted while waiting
    • awaitExhaustion

      public static boolean awaitExhaustion(long timeout) throws InterruptedException
      Wait until all generators and event queues are exhausted or the maximum wait time has expired.
      Parameters:
      timeout - the wait time in milliseconds
      Returns:
      true if exhaustion state was reached
      Throws:
      InterruptedException - if the execution was interrupted
      See Also:
    • checkAssertions

      public static void checkAssertions()
      Utility method that checks if an assertion error has occurred while executing handlers.

      If so, the error is thrown and the assertion error store is reset.

      This method is intended for junit tests. It enables easy propagation of assertion failures to the main thread.

      Throws:
      AssertionError - if an assertion error occurred while executing the application
    • fullObjectName

      public static String fullObjectName(Object object)
      Returns the full name of the object’s class together with an id (see objectId(Object)).

      The result can be used as a unique human readable identifier for arbitrary objects.

      Parameters:
      object - the object
      Returns:
      the object’s name
    • simpleObjectName

      public static String simpleObjectName(Object object)
      Returns the simple name of the object’s class together with an id (see objectId(Object)).

      Can be used to create a human readable, though not necessarily unique, label for an object.

      Parameters:
      object - the object
      Returns:
      the object’s name
    • objectName

      public static String objectName(Object object)
      Returns the name of the object’s class together with an id (see objectId(Object)).

      May be used to implement toString() with identifiable objects. If the log level is “finer”, the full class name will be used for the returned value, else the simple name.

      Parameters:
      object - the object
      Returns:
      the object’s name
    • className

      public static String className(Class<?> clazz)
      Returns the full name or simple name of the class depending on the log level.
      Parameters:
      clazz - the class
      Returns:
      the name
    • simpleClassName

      public static String simpleClassName(Class<?> clazz)
      Returns the simple name of a class.

      Contrary to Class.getSimpleName(), this method returns the last segement of the full name for anonymous classes (instead of an empty string).

      Parameters:
      clazz - the class
      Returns:
      the name
    • objectId

      public static String objectId(Object object)
      Returns an id of the object that is unique within a specific scope.

      Ids are generated and looked up in the scope of the object’s class unless the class implements Components.IdInfoProvider.

      Parameters:
      object - the object
      Returns:
      the object’s name
    • timerExecutorService

      Returns the executor service used for executing timers.
      Returns:
      the timer executor service
    • setTimerExecutorService

      public static void setTimerExecutorService(ExecutorService timerExecutorService)
      Sets the executor service used for executing timers.

      Defaults to the defaultExecutorService().

      Parameters:
      timerExecutorService - the timerExecutorService to set
    • schedule

      public static Components.Timer schedule(Components.TimeoutHandler timeoutHandler, Instant scheduledFor)
      Schedules the given timeout handler for the given instance.
      Parameters:
      timeoutHandler - the handler
      scheduledFor - the instance in time
      Returns:
      the timer
    • schedule

      public static Components.Timer schedule(Components.TimeoutHandler timeoutHandler, Duration scheduledFor)
      Schedules the given timeout handler for the given offset from now.
      Parameters:
      timeoutHandler - the handler
      scheduledFor - the time to wait
      Returns:
      the timer
    • put

      public static <K, V> Map<K,V> put(Map<K,V> map, K key, V value)
      Puts the given key and value in the given Map and returns the map.

      Looks ugly when nested, but comes in handy sometimes.

      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      map - the map
      key - the key
      value - the value
      Returns:
      the map
    • mapOf

      @Deprecated public static <K, V> Map<K,V> mapOf()
      Deprecated.
      Provisional replacement for method available in Java 9.
      Returns:
      an empty map
    • mapOf

      @Deprecated public static <K, V> Map<K,V> mapOf(K k1, V v1)
      Deprecated.
      Provisional replacement for method available in Java 9.
      Returns:
      an immutable map filled with the given values
    • mapOf

      @Deprecated public static <K, V> Map<K,V> mapOf(K k1, V v1, K k2, V v2)
      Deprecated.
      Provisional replacement for method available in Java 9.
      Returns:
      an immutable map filled with the given values
    • mapOf

      @Deprecated public static <K, V> Map<K,V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3)
      Deprecated.
      Provisional replacement for method available in Java 9.
      Returns:
      an immutable map filled with the given values
    • mapOf

      @Deprecated public static <K, V> Map<K,V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
      Deprecated.
      Provisional replacement for method available in Java 9.
      Returns:
      an immutable map filled with the given values
    • mapOf

      @Deprecated public static <K, V> Map<K,V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
      Deprecated.
      Provisional replacement for method available in Java 9.
      Returns:
      an immutable map filled with the given values
    • mapOf

      @Deprecated public static <K, V> Map<K,V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)
      Deprecated.
      Provisional replacement for method available in Java 9.
      Returns:
      an immutable map filled with the given values
    • mapOf

      @Deprecated public static <K, V> Map<K,V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)
      Deprecated.
      Provisional replacement for method available in Java 9.
      Returns:
      an immutable map filled with the given values
    • mapOf

      @Deprecated public static <K, V> Map<K,V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)
      Deprecated.
      Provisional replacement for method available in Java 9.
      Returns:
      an immutable map filled with the given values
    • mapOf

      @Deprecated public static <K, V> Map<K,V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)
      Deprecated.
      Provisional replacement for method available in Java 9.
      Returns:
      an immutable map filled with the given values
    • mapOf

      @Deprecated public static <K, V> Map<K,V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)
      Deprecated.
      Provisional replacement for method available in Java 9.
      Returns:
      an immutable map filled with the given values