Package dev.cashire.cashconfig.items
Class BaseConfigItem<T>
java.lang.Object
dev.cashire.cashconfig.items.BaseConfigItem<T>
- Type Parameters:
T
- type stored in this item. Supported types listed inBaseConfigItem.Type
- Direct Known Subclasses:
ConfigBoolean
,ConfigGroup
,ConfigList
,ConfigNumber
,ConfigString
Base type of all Configuration Items.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Type representing the value of what is stored inBaseConfigItem
Is a list of all supported types. -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
BaseConfigItem(@NotNull String key, @NotNull BaseConfigItem.Type type)
BaseConfigItem constructor. -
Method Summary
Modifier and TypeMethodDescriptionGets this item as a boolean item.asGroup()
Gets this item as a group item.asList()
Gets this item as a list item.asNumber()
Gets this item as a number item.asString()
Gets this item as aConfigString
item.abstract void
fromJson(@NotNull com.google.gson.JsonElement element)
Deserialize data from theJsonElement
into this item.static BaseConfigItem<?>
getInstance(@NotNull BaseConfigItem.Type type)
Creates a newBaseConfigItem
instance based on theBaseConfigItem.Type
given.static BaseConfigItem<?>
getInstance(@NotNull BaseConfigItem.Type type, @NotNull String key)
Creates a newBaseConfigItem
instance based on theBaseConfigItem.Type
given.getKey()
The key this value is stored under in an object.getType()
Gets the type of this item.static BaseConfigItem.Type
getType(@NotNull com.google.gson.JsonElement value)
Gets the type associated with a givenJsonElement
.getValue()
Value stored by this item.boolean
Checks if this item is a boolean item.boolean
isGroup()
Checks if this item is a group item.boolean
isList()
Checks if this item is a list item.boolean
isList(BaseConfigItem.Type subType)
Checks if this item is a list item with given subtype.boolean
isNumber()
Checks if this item is a number item.boolean
isString()
Checks if this item is a string item.void
Set the value of this item.abstract void
toJson(@NotNull com.google.gson.JsonArray parent)
Serializes data into the givenJsonArray
This item'sgetKey()
value is ignored in this case.abstract void
toJson(@NotNull com.google.gson.JsonObject parent)
Serializes data into the givenJsonObject
.protected static boolean
validType(@NotNull com.google.gson.JsonElement element, @NotNull BaseConfigItem.Type type)
Checks if the given json element matches the given type.
-
Field Details
-
key
key for this item. only used for items added toConfigGroup
-
value
Value being stored by this item. -
type
Type of this item.
-
-
Constructor Details
-
BaseConfigItem
BaseConfigItem constructor.- Parameters:
key
- key to create item with.type
- type of this item.
-
-
Method Details
-
toJson
public abstract void toJson(@NotNull @NotNull com.google.gson.JsonObject parent)Serializes data into the givenJsonObject
. This item is put in theJsonObject
at key value given bygetKey()
. It is put in as type given bygetType()
- Parameters:
parent
-JsonObject
to add this item to.
-
toJson
public abstract void toJson(@NotNull @NotNull com.google.gson.JsonArray parent)Serializes data into the givenJsonArray
This item'sgetKey()
value is ignored in this case. It is put in as type given bygetType()
- Parameters:
parent
-JsonArray
to add to this item to.
-
fromJson
public abstract void fromJson(@NotNull @NotNull com.google.gson.JsonElement element)Deserialize data from theJsonElement
into this item. Treats theJsonElement
as the type given bygetType()
- Parameters:
element
-JsonElement
to load data from.
-
getType
Gets the type of this item.- Returns:
- type of this item.
- See Also:
BaseConfigItem.Type
-
getType
@Internal public static BaseConfigItem.Type getType(@NotNull @NotNull com.google.gson.JsonElement value)Gets the type associated with a givenJsonElement
.- Parameters:
value
- element to check type for- Returns:
- the type associated with this element. If not known/supported, null is returned.
-
getKey
The key this value is stored under in an object. If from an array this value may be empty.- Returns:
- key for this item.
-
getValue
Value stored by this item. Item type corresponds to this items type- Returns:
- value stored by this item type.
- See Also:
BaseConfigItem.Type
-
setValue
Set the value of this item.- Parameters:
value
- value to use instead of current value.
-
validType
protected static boolean validType(@NotNull @NotNull com.google.gson.JsonElement element, @NotNull @NotNull BaseConfigItem.Type type)Checks if the given json element matches the given type.- Parameters:
element
- json element to usetype
- type to check with- Returns:
- true if element matches type, false otherwise
-
isGroup
public boolean isGroup()Checks if this item is a group item.- Returns:
- true if group item, false otherwise
-
asGroup
Gets this item as a group item.- Returns:
- this as a group item
- Throws:
IllegalStateException
- if not a group item
-
isList
public boolean isList()Checks if this item is a list item.- Returns:
- true if list item, false otherwise
-
isList
Checks if this item is a list item with given subtype.- Parameters:
subType
- type to check with- Returns:
- true if list item with given subtype, false otherwise
-
asList
Gets this item as a list item.- Returns:
- this as list item
- Throws:
IllegalStateException
- if not a list item
-
isBoolean
public boolean isBoolean()Checks if this item is a boolean item.- Returns:
- true if boolean item, false otherwise
-
asBoolean
Gets this item as a boolean item.- Returns:
- this as boolean item
- Throws:
IllegalStateException
- if not a boolean item
-
isNumber
public boolean isNumber()Checks if this item is a number item.- Returns:
- true if number item, false otherwise
-
asNumber
Gets this item as a number item.- Returns:
- this as number item
- Throws:
IllegalStateException
- if not a number item
-
isString
public boolean isString()Checks if this item is a string item.- Returns:
- true if string item, false otherwise
-
asString
Gets this item as aConfigString
item.- Returns:
- this as string item
- Throws:
IllegalStateException
- if not a string item
-
getInstance
Creates a newBaseConfigItem
instance based on theBaseConfigItem.Type
given.- Parameters:
type
- Type to instantiate.- Returns:
- new instance of an
BaseConfigItem
.
-
getInstance
@Internal public static BaseConfigItem<?> getInstance(@NotNull @NotNull BaseConfigItem.Type type, @NotNull @NotNull String key)Creates a newBaseConfigItem
instance based on theBaseConfigItem.Type
given.- Parameters:
type
- Type to instantiate.key
- key to give item.- Returns:
- new instance of an
BaseConfigItem
.
-