Class ScreenContext

java.lang.Object
no.ntnu.principes.controller.screen.ScreenContext

public class ScreenContext extends Object
Manages a context for a specific screen by storing and retrieving parameters tied to a unique screen identifier.
  • Constructor Details

    • ScreenContext

      public ScreenContext(String screenId)
      Initializes a new screen context identified by a unique screen ID and prepares storage for parameters associated with the screen.
      Parameters:
      screenId - The unique identifier for the screen. Must not be null or empty. This ID is used to differentiate between contexts for different screens.
  • Method Details

    • setParameter

      public void setParameter(String key, Object value)
      Adds or updates a parameter in the context associated with the screen.
      Parameters:
      key - A non-null string that identifies the parameter. Must not be empty.
      value - The value to associate with the key. Can be any object, including null, which will overwrite any existing value for the key.
    • getParameter

      public <T> T getParameter(String key)
      Retrieves the value associated with the specified key from the parameter map, cast to the expected type.
      Type Parameters:
      T - The expected type of the value.
      Parameters:
      key - The key to look up in the parameter map. Must not be null. If the key does not exist, this method returns null.
      Returns:
      The value associated with the key, cast to the expected type, or null if the key is not present in the map.
      Throws:
      ClassCastException - If the value associated with the key cannot be cast to the specified type.
    • clearParameters

      public void clearParameters()
      Removes all parameters stored in the current screen context.
    • getParameters

      public Map<String,Object> getParameters()
      Returns an unmodifiable view of the parameters map.
      Returns:
      a Map<String, Object> containing the current parameters; modifications to the returned map are not allowed.
    • getParameters

      public Map<String,Object> getParameters(String... keys)
      Retrieves a filtered map of parameters based on the specified keys. Only includes entries where the key exists in the `parameters` map.
      Parameters:
      keys - The keys to filter and retrieve from the parameters map. Non-existent keys are skipped.
      Returns:
      A map containing key-value pairs for all valid keys found in the `parameters` map. If no keys match, returns an empty map.
    • toString

      public String toString()
      Overrides:
      toString in class Object