Class BaseConfigItem<T>

java.lang.Object
dev.cashire.cashconfig.items.BaseConfigItem<T>
Type Parameters:
T - type stored in this item. Supported types listed in BaseConfigItem.Type
Direct Known Subclasses:
ConfigBoolean, ConfigGroup, ConfigList, ConfigNumber, ConfigString

public abstract class BaseConfigItem<T> extends Object
Base type of all Configuration Items.
  • Field Details

    • key

      protected final String key
      key for this item. only used for items added to ConfigGroup
    • value

      protected T value
      Value being stored by this item.
    • type

      protected final BaseConfigItem.Type type
      Type of this item.
  • Constructor Details

    • BaseConfigItem

      protected BaseConfigItem(@NotNull @NotNull String key, @NotNull @NotNull BaseConfigItem.Type type)
      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 given JsonObject. This item is put in the JsonObject at key value given by getKey(). It is put in as type given by getType()
      Parameters:
      parent - JsonObject to add this item to.
    • toJson

      public abstract void toJson(@NotNull @NotNull com.google.gson.JsonArray parent)
      Serializes data into the given JsonArray This item's getKey() value is ignored in this case. It is put in as type given by getType()
      Parameters:
      parent - JsonArray to add to this item to.
    • fromJson

      public abstract void fromJson(@NotNull @NotNull com.google.gson.JsonElement element)
      Deserialize data from the JsonElement into this item. Treats the JsonElement as the type given by getType()
      Parameters:
      element - JsonElement to load data from.
    • getType

      public BaseConfigItem.Type 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 given JsonElement.
      Parameters:
      value - element to check type for
      Returns:
      the type associated with this element. If not known/supported, null is returned.
    • getKey

      public String 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

      public T 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

      public void setValue(@NotNull T value)
      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 use
      type - 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

      public ConfigGroup 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

      public boolean isList(BaseConfigItem.Type subType)
      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

      public ConfigList 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

      public ConfigBoolean 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

      public ConfigNumber 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

      public ConfigString asString()
      Gets this item as a ConfigString item.
      Returns:
      this as string item
      Throws:
      IllegalStateException - if not a string item
    • getInstance

      @Internal public static BaseConfigItem<?> getInstance(@NotNull @NotNull BaseConfigItem.Type type)
      Creates a new BaseConfigItem instance based on the BaseConfigItem.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 new BaseConfigItem instance based on the BaseConfigItem.Type given.
      Parameters:
      type - Type to instantiate.
      key - key to give item.
      Returns:
      new instance of an BaseConfigItem.