Class ScreenContextManager.ScreenContextHolder

java.lang.Object
no.ntnu.principes.controller.screen.ScreenContextManager.ScreenContextHolder
Enclosing class:
ScreenContextManager

public static class ScreenContextManager.ScreenContextHolder extends Object
Manages a collection of ScreenContext objects, each identified by a unique screen ID.

This class provides thread-safe operations for creating, retrieving, and managing screen-specific contexts within the application.

  • Constructor Details

    • ScreenContextHolder

      public ScreenContextHolder()
      Creates a new ScreenContextHolder for managing multiple ScreenContext objects.

      Initializes an internal thread-safe ConcurrentHashMap to store screen contexts, with each context mapped to a unique screen ID.

  • Method Details

    • getOrCreateContext

      public ScreenContext getOrCreateContext(String screenId)
      Retrieves an existing ScreenContext for the given screen ID or creates a new one if it does not already exist.

      This method ensures that each screen ID is associated with a unique ScreenContext. If the context for the provided screen ID is not already stored, it will initialize and map a new instance of ScreenContext to the given ID.

      Parameters:
      screenId - The unique identifier for the screen. Must not be null or empty. Used to retrieve or initialize the corresponding ScreenContext.
      Returns:
      The ScreenContext object associated with the given screen ID. If the context does not already exist, a new one is created and returned.
    • clearContext

      public void clearContext(String screenId)
      Removes the ScreenContext associated with the provided screen ID from the internal storage.

      After this operation, the screen ID will no longer be mapped to a ScreenContext. If no context exists for the specified screen ID, the method will have no effect.

      Parameters:
      screenId - The unique identifier for the screen. Must not be null or empty. Used to locate and remove the associated ScreenContext.
      Throws:
      NullPointerException - If screenId is null.
    • clearAllContexts

      public void clearAllContexts()
      Removes all screen contexts stored in this ScreenContextHolder.

      This method clears the internal storage of all ScreenContext objects, effectively resetting the state of the ScreenContextHolder. Any further access to previously stored contexts will result in their recreation using getOrCreateContext(String).