Class ContextDataHolder

java.lang.Object
io.github.somesourcecode.someguiapi.scene.data.ContextDataHolder

public class ContextDataHolder extends Object
A container for context data. It is used to register, store and retrieve context data. The data is represented by ContextData objects and can be accessed by their id.
Since:
2.0.0
  • Constructor Details

    • ContextDataHolder

      public ContextDataHolder()
  • Method Details

    • register

      public ContextDataHolder register(ContextData<?> data)
      Registers the given context data.

      If the id is already registered, this method will fail silently.

      Parameters:
      data - the data to register
      Returns:
      the holder for method chaining
      Since:
      2.0.0
    • register

      public <T> ContextDataHolder register(String id, Class<T> type)
      Registers a new context data with the given id, class and data. This method is a shorthand for register(ContextData).

      If the id is already registered, this method will fail silently.

      Type Parameters:
      T - the type of data
      Parameters:
      id - the id of the data
      type - the type of the data
      Returns:
      the holder for method chaining
      Since:
      2.0.0
    • register

      public <T> ContextDataHolder register(String id, Class<T> type, T data)
      Registers a new context data with the given id, class and data. This method is a shorthand for register(ContextData).

      If the id is already registered, this method will fail silently.

      Type Parameters:
      T - the type of the data
      Parameters:
      id - the id of the data
      type - the type of the data
      data - the initial data
      Returns:
      the holder for method chaining
      Since:
      2.0.0
    • getData

      public ContextData<?> getData(String id)
      Returns the context data with the given id.
      Parameters:
      id - the id of the data
      Returns:
      the data with the given id, or null if no data with the given id is registered
      Since:
      2.0.0
    • getDataUnchecked

      public <T> ContextData<T> getDataUnchecked(String id)
      Returns the context data with the given id and an unchecked data type. This is useful if the type of the data is known.
      Type Parameters:
      T - the type the data is cast to
      Parameters:
      id - the id of the data
      Returns:
      the data with the given id
      Throws:
      ClassCastException - if the data is not of the correct type
      Since:
      2.0.0
    • getOptionalData

      public Optional<ContextData<?>> getOptionalData(String id)
      Returns the context data with the given id as an optional.
      Parameters:
      id - the id of the data
      Returns:
      the data with the given id as an optional
      Since:
      2.0.0
    • unregister

      public ContextDataHolder unregister(String id)
      Unregisters the context data with the given id.
      Parameters:
      id - the id of the data
      Returns:
      the holder for method chaining
      Since:
      2.0.0
    • unregisterAll

      public ContextDataHolder unregisterAll()
      Unregisters all context data.
      Returns:
      the holder for method chaining
      Since:
      2.0.0