public class Components extends Object
This class provides some utility functions.
Modifier and Type | Class and Description |
---|---|
static interface |
Components.IdInfoProvider
Implemented by classes that want a special class (scope) to be used
for looking up their id or want to map to another object for getting the
id (see
objectId(Object) ). |
static class |
Components.PoolingIndex<K,V>
An index of pooled items.
|
static interface |
Components.TimeoutHandler
Instances are added to the scheduler in order to be invoked
at a given time.
|
static class |
Components.Timer
Represents a timer as created by
schedule(TimeoutHandler, Instant) . |
Modifier and Type | Method and Description |
---|---|
static void |
awaitExhaustion()
Wait until all generators and event queues are exhausted.
|
static boolean |
awaitExhaustion(long timeout)
Wait until all generators and event queues are exhausted or
the maximum wait time has expired.
|
static void |
checkAssertions()
Utility method that checks if an assertion error has occurred
while executing handlers.
|
static String |
className(Class<?> clazz)
Returns the full name or simple name of the class depending
on the log level.
|
static ExecutorService |
defaultExecutorService()
Return the default executor service for the framework.
|
static String |
fullObjectName(Object object)
Returns the full name of the object’s class together with an id (see
objectId(Object) ). |
static Manager |
manager(ComponentType component)
Returns a component’s manager.
|
static Manager |
manager(ComponentType component,
Channel componentChannel)
Returns a component’s manager like
manager(ComponentType) . |
static <K,V> Map<K,V> |
mapOf()
Provisional replacement for method available in Java 9.
|
static <K,V> Map<K,V> |
mapOf(K k1,
V v1)
Provisional replacement for method available in Java 9.
|
static <K,V> Map<K,V> |
mapOf(K k1,
V v1,
K k2,
V v2)
Provisional replacement for method available in Java 9.
|
static <K,V> Map<K,V> |
mapOf(K k1,
V v1,
K k2,
V v2,
K k3,
V v3)
Provisional replacement for method available in Java 9.
|
static <K,V> Map<K,V> |
mapOf(K k1,
V v1,
K k2,
V v2,
K k3,
V v3,
K k4,
V v4)
Provisional replacement for method available in Java 9.
|
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)
Provisional replacement for method available in Java 9.
|
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)
Provisional replacement for method available in Java 9.
|
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)
Provisional replacement for method available in Java 9.
|
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)
Provisional replacement for method available in Java 9.
|
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)
Provisional replacement for method available in Java 9.
|
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)
Provisional replacement for method available in Java 9.
|
static String |
objectId(Object object)
Returns an id of the object that is unique within a specific scope.
|
static String |
objectName(Object object)
Returns the name of the object’s class together with an id (see
objectId(Object) ). |
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. |
static Components.Timer |
schedule(Components.TimeoutHandler timeoutHandler,
Duration scheduledFor)
Schedules the given timeout handler for the given
offset from now.
|
static Components.Timer |
schedule(Components.TimeoutHandler timeoutHandler,
Instant scheduledFor)
Schedules the given timeout handler for the given instance.
|
static void |
setDefaultExecutorService(ExecutorService defaultExecutorService)
Set the default executor service for the framework.
|
static void |
setTimerExecutorService(ExecutorService timerExecutorService)
Sets the executor service used for executing timers.
|
static String |
simpleClassName(Class<?> clazz)
Returns the simple name of a class.
|
static String |
simpleObjectName(Object object)
Returns the simple name of the object’s class together with an id
(see
objectId(Object) ). |
static void |
start(ComponentType application)
|
static ExecutorService |
timerExecutorService()
Returns the executor service used for executing timers.
|
public static ExecutorService defaultExecutorService()
Return the default executor service for the framework.
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.
defaultExecutorService
- the executor service to setpublic 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.
component
- the componentpublic 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
.
component
- the componentcomponentChannel
- the channel that the component’s
handlers listen on by default and that
Manager.fire(Event, Channel...)
sends the event toComponent.Component(Channel)
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.
application
- the application to startInterruptedException
- if the execution was interruptedpublic 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.
InterruptedException
- if the current thread was interrupted
while waitingpublic static boolean awaitExhaustion(long timeout) throws InterruptedException
Wait until all generators and event queues are exhausted or the maximum wait time has expired.
timeout
- the wait time in millisecondstrue
if exhaustion state was reachedInterruptedException
- if the execution was interruptedawaitExhaustion()
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.
AssertionError
- if an assertion error occurred while
executing the applicationpublic 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.
object
- the objectpublic 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.
object
- the objectpublic 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.
object
- the objectpublic static String className(Class<?> clazz)
Returns the full name or simple name of the class depending on the log level.
clazz
- the classpublic 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).
clazz
- the classpublic 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
.
object
- the objectpublic static ExecutorService timerExecutorService()
Returns the executor service used for executing timers.
public static void setTimerExecutorService(ExecutorService timerExecutorService)
Sets the executor service used for executing timers.
Defaults to the defaultExecutorService()
.
timerExecutorService
- the timerExecutorService to setpublic static Components.Timer schedule(Components.TimeoutHandler timeoutHandler, Instant scheduledFor)
Schedules the given timeout handler for the given instance.
timeoutHandler
- the handlerscheduledFor
- the instance in timepublic static Components.Timer schedule(Components.TimeoutHandler timeoutHandler, Duration scheduledFor)
Schedules the given timeout handler for the given offset from now.
timeoutHandler
- the handlerscheduledFor
- the time to waitpublic 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.
K
- the key typeV
- the value typemap
- the mapkey
- the keyvalue
- the valuepublic static <K,V> Map<K,V> mapOf()
Provisional replacement for method available in Java 9.
public static <K,V> Map<K,V> mapOf(K k1, V v1)
Provisional replacement for method available in Java 9.
public static <K,V> Map<K,V> mapOf(K k1, V v1, K k2, V v2)
Provisional replacement for method available in Java 9.
public static <K,V> Map<K,V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3)
Provisional replacement for method available in Java 9.
public static <K,V> Map<K,V> mapOf(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
Provisional replacement for method available in Java 9.
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)
Provisional replacement for method available in Java 9.
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)
Provisional replacement for method available in Java 9.
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)
Provisional replacement for method available in Java 9.
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)
Provisional replacement for method available in Java 9.
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)
Provisional replacement for method available in Java 9.