Class NanoBase<T extends NanoBase<T>>

java.lang.Object
org.nanonative.nano.core.NanoBase<T>
Type Parameters:
T - The payload of the NanoBase implementation, used for method chaining.
Direct Known Subclasses:
NanoThreads

public abstract class NanoBase<T extends NanoBase<T>> extends Object
The abstract base class for Nano framework providing the core functionalities.
  • Field Details

  • Constructor Details

    • NanoBase

      protected NanoBase(Map<Object,Object> configs, String... args)
      Initializes the NanoBase with provided configurations and arguments.
      Parameters:
      configs - Configuration settings in a key-value map.
      args - Command line arguments.
  • Method Details

    • stop

      public abstract T stop(Class<?> clazz)
      Initiates the shutdown process for the Nano instance.
      Parameters:
      clazz - class for which the Context is to be created.
      Returns:
      Self for chaining
    • stop

      public abstract T stop(Context context)
      Initiates the shutdown process for the Nano instance.
      Parameters:
      context - The Context in which Nano instance shuts down.
      Returns:
      The current instance of Nano for method chaining.
    • listeners

      public Map<Integer,Set<Consumer<? super Event<?,?>>>> listeners()
      Retrieves the registered event listeners.
      Returns:
      A map of event types to their respective listeners.
    • subscribeEvent

      public <C, R> T subscribeEvent(Channel<C,R> channel, Consumer<? super Event<C,R>> listener)
      Registers an event listener with a typed payload.
      Type Parameters:
      C - The payload
      R - The return payload
      Parameters:
      channel - The channel to be subscribed.
      listener - The bi-consumer to receive the Event and its payload.
      Returns:
      Self for chaining
    • subscribeEvent

      public <C, R> Consumer<Event<C,R>> subscribeEvent(Channel<C,R> channel, BiConsumer<? super Event<C,R>,C> listener)
      Registers an event listener with a typed payload.
      Type Parameters:
      C - The payload
      R - The return payload
      Parameters:
      channel - The channel to be subscribed.
      listener - The bi-consumer to receive the Event and its payload.
      Returns:
      A consumer function that can be used to unsubscribe the listener later.
    • subscribeError

      public <C, R> Consumer<Event<Object,Void>> subscribeError(Channel<C,R> channel, Consumer<Event<C,R>> listener)
      Registers for global error handling.
      Type Parameters:
      C - The payload
      R - The return payload
      Parameters:
      channel - The channel which should be handled on error.
      listener - The bi-consumer to receive the Event and its payload.
      Returns:
      A consumer function that can be used to unsubscribe the listener later.
    • subscribeError

      public <C, R> Consumer<Event<Object,Void>> subscribeError(Consumer<Event<Object,Void>> listener)
      Registers for global error handling.
      Parameters:
      listener - The consumer to receive the Event and its payload.
      Returns:
      A consumer function that can be used to unsubscribe the listener later.
    • unsubscribeEvent

      public <C, R> T unsubscribeEvent(int channelId, Consumer<Event<C,R>> listener)
      Removes a registered event listener for a specific event payload.
      Parameters:
      channelId - The integer identifier of the event payload.
      listener - The consumer function to be removed.
      Returns:
      Self for chaining
    • pid

      public long pid()
      Retrieves the process ID of the current instance.
      Returns:
      The process ID.
    • usedMemoryMB

      public double usedMemoryMB()
      Calculates the memory usage of the application in megabytes.
      Returns:
      Memory usage in megabytes, rounded to two decimal places.
    • heapMemoryUsage

      public double heapMemoryUsage()
      Calculates the memory usage of the application in percentage.
      Returns:
      Memory usage in percentage, rounded to two decimal places.
    • createdAtMs

      public long createdAtMs()
      Retrieves the creation timestamp of the instance.
      Returns:
      The timestamp of creation in milliseconds.
    • isReady

      public boolean isReady()
      Checks whether the instance is ready for operations.
      Returns:
      readiness state.
    • eventCount

      public int eventCount()
      Performs the eventCount operation.
      Returns:
      the result
    • displayHelpMenu

      protected void displayHelpMenu()
      Displays a help menu with available configuration keys and their descriptions and exits.
    • readConfigs

      protected Context readConfigs(String... args)
      Reads and initializes Context based on provided arguments.
      Parameters:
      args - Command-line arguments.
      Returns:
      The Context initialized with the configurations.
    • standardiseKey

      public static String standardiseKey(Object key)
      Standardizes a config key.
      Parameters:
      key - The config key to be standardized.