Class ContextData<T>
java.lang.Object
io.github.somesourcecode.someguiapi.scene.data.ContextData<T>
- Type Parameters:
T- the type of data stored
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 Summary
ConstructorsConstructorDescriptionContextData(String id, Class<T> type) Constructs a new ContextData instance with the given id and class.ContextData(String id, Class<T> type, T data) Constructs a new ContextData instance with the given id, class and data. -
Method Summary
Modifier and TypeMethodDescriptionget()Returns the stored data.<E> EReturns the stored data as the given type.booleanReturns the stored data as a boolean.byteReturns the stored data as a byte.charReturns the stored data as a char.doubleReturns the stored data as a double.floatReturns the stored data as a float.intgetAsInt()Returns the stored data as an integer.longReturns the stored data as a long.shortReturns the stored data as a short.Returns the stored data as a string.getId()Returns the id of the data.<E> EReturns the stored data as an unchecked type.booleanisSet()Returns whether the data is set.booleanSets the data.voidunset()Unsets the data.
-
Constructor Details
-
ContextData
Constructs a new ContextData instance with the given id and class.- Parameters:
id- the id of the datatype- 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
Constructs a new ContextData instance with the given id, class and data.- Parameters:
id- the id of the datatype- the type of the datadata- 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
Returns the id of the data.- Returns:
- the id of the data
- Since:
- 2.0.0
-
set
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
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
Returns the stored data as the given type. If the data is not of the given type, this method will throw aClassCastException.- 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
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
-