Class ContextData<T>

java.lang.Object
io.github.somesourcecode.someguiapi.scene.data.ContextData<T>
Type Parameters:
T - the type of data stored

public class ContextData<T> extends Object
This class is used to store data. It contains and id and the data. The type of data stored must be specified upon creation.
Since:
2.0.0
  • Constructor Details

    • ContextData

      public ContextData(String id, Class<T> type)
      Constructs a new ContextData instance with the given id and class.
      Parameters:
      id - the id of the data
      type - the type of the data
      Throws:
      IllegalArgumentException - if the id is null or blank, or if the type is null or primitive
      Since:
      2.0.0
    • ContextData

      public ContextData(String id, Class<T> type, T data)
      Constructs a new ContextData instance with the given id, class and data.
      Parameters:
      id - the id of the data
      type - the type of the data
      data - the initial data
      Throws:
      IllegalArgumentException - if the id is null or blank, or if the type is null or primitive
      Since:
      2.0.0
  • Method Details

    • getId

      public String getId()
      Returns the id of the data.
      Returns:
      the id of the data
      Since:
      2.0.0
    • set

      public boolean set(Object data)
      Sets the data. If the data is not of the correct type, this method will fail silently.
      Parameters:
      data - the data to set
      Returns:
      true if the data was set, false if the data is not of the correct type
      Since:
      2.0.0
    • isSet

      public boolean isSet()
      Returns whether the data is set.
      Returns:
      true if the data is set, false otherwise
      Since:
      2.0.0
    • unset

      public void unset()
      Unsets the data.
      Since:
      2.0.0
    • get

      public T get()
      Returns the stored data.
      Returns:
      the stored data
      Since:
      2.0.0
    • getUnchecked

      public <E> E getUnchecked()
      Returns the stored data as an unchecked type. This is useful if the type of the data is known.
      Type Parameters:
      E - the type of the data
      Returns:
      the stored data
      Throws:
      ClassCastException - if the data is not of the correct type
      Since:
      2.0.0
    • getAs

      public <E> E getAs(Class<E> type)
      Returns the stored data as the given type. If the data is not of the given type, this method will throw a ClassCastException.
      Type Parameters:
      E - the type of the data
      Parameters:
      type - the type of the data
      Returns:
      the stored data as the given type
      Throws:
      ClassCastException - if the data is not of the given type
      Since:
      2.0.0
    • getAsString

      public String getAsString()
      Returns the stored data as a string. If the data is not a string, it will automatically be converted.
      Returns:
      the stored data as a string
      Since:
      2.0.0
    • getAsInt

      public int getAsInt()
      Returns the stored data as an integer.
      Returns:
      the stored data as an integer
      Throws:
      ClassCastException - if the data cannot be converted to an integer
      Since:
      2.0.0
    • getAsLong

      public long getAsLong()
      Returns the stored data as a long.
      Returns:
      the stored data as a long
      Throws:
      ClassCastException - if the data cannot be converted to a long
      Since:
      2.0.0
    • getAsDouble

      public double getAsDouble()
      Returns the stored data as a double.
      Returns:
      the stored data as a double
      Throws:
      ClassCastException - if the data cannot be converted to a double
      Since:
      2.0.0
    • getAsFloat

      public float getAsFloat()
      Returns the stored data as a float.
      Returns:
      the stored data as a float
      Throws:
      ClassCastException - if the data cannot be converted to a float
      Since:
      2.0.0
    • getAsBoolean

      public boolean getAsBoolean()
      Returns the stored data as a boolean.
      Returns:
      the stored data as a boolean
      Throws:
      ClassCastException - if the data cannot be converted to a boolean
      Since:
      2.0.0
    • getAsByte

      public byte getAsByte()
      Returns the stored data as a byte.
      Returns:
      the stored data as a byte
      Throws:
      ClassCastException - if the data cannot be converted to a byte
      Since:
      2.0.0
    • getAsShort

      public short getAsShort()
      Returns the stored data as a short.
      Returns:
      the stored data as a short
      Throws:
      ClassCastException - if the data cannot be converted to a short
      Since:
      2.0.0
    • getAsChar

      public char getAsChar()
      Returns the stored data as a char.
      Returns:
      the stored data as a char
      Throws:
      ClassCastException - if the data cannot be converted to a char
      Since:
      2.0.0