Package cc.carm.lib.configuration.value
Class ConfigValue<T>
- java.lang.Object
-
- cc.carm.lib.configuration.value.ValueManifest<T>
-
- cc.carm.lib.configuration.value.ConfigValue<T>
-
- Direct Known Subclasses:
CachedConfigValue
public abstract class ConfigValue<T> extends ValueManifest<T>
Represents a configurable value with type safety and null-handling capabilities.This abstract class provides core functionalities for managing configuration values, including value retrieval with fallback defaults, null safety enforcement, and value persistence controls. It serves as the foundation for type-specific configuration implementations.
Functions:
- Type-safe value access through
get()
andoptional()
- Default value fallback via
getOrDefault()
- Null-safety enforcement with
resolve()
andgetNotNull()
- Default value initialization through
setDefault()
- Value comparison with
isDefault()
Persistence Behavior:
Value modifications viaset(Object)
orsetDefault()
methods do NOT automatically persist to configuration sources. Explicit calls toConfigurationHolder.save()
are required for permanent storage.
-
-
Field Summary
-
Fields inherited from class cc.carm.lib.configuration.value.ValueManifest
defaultSupplier, holder, initializer, path, type
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ConfigValue(@NotNull ValueManifest<T> manifest)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract T
get()
Gets the configured value (i.e., the value read from the source).T
getNotNull()
Gets the non-null value of this configuration.T
getOrDefault()
Gets the configured value, or returns the default value if not present.boolean
isDefault()
Checks if the loaded configuration value matches the default value.@NotNull java.util.Optional<@Nullable T>
optional()
Gets the value of this configuration as anOptional
.T
resolve()
Gets the non-null value of this configuration.void
save()
Try to save the configuration.abstract void
set(T value)
Sets the value of this configuration.void
setDefault()
Initializes the default value for this configuration.void
setDefault(boolean override)
Sets the configuration value to its default.-
Methods inherited from class cc.carm.lib.configuration.value.ValueManifest
config, defaults, defaults, defaults, getData, hasDefaults, holder, holder, initialize, initialize, metadata, path, path, setData, type
-
-
-
-
Constructor Detail
-
ConfigValue
protected ConfigValue(@NotNull @NotNull ValueManifest<T> manifest)
-
-
Method Detail
-
get
@Nullable public abstract T get()
Gets the configured value (i.e., the value read from the source).
If no default value was written during initialization, you can use thegetOrDefault()
method to obtain the default value when this value is empty.- Returns:
- Configured value
-
getOrDefault
public T getOrDefault()
Gets the configured value, or returns the default value if not present.- Returns:
- Configured value or default value
-
resolve
@NotNull public T resolve()
Gets the non-null value of this configuration.- Returns:
- Non-null value
- Throws:
java.lang.NullPointerException
- Thrown when the corresponding data is null
-
getNotNull
@NotNull public T getNotNull()
Gets the non-null value of this configuration.- Returns:
- Non-null value
- Throws:
java.lang.NullPointerException
- Thrown when the corresponding data is null- See Also:
for a more descriptive function
-
optional
@NotNull public @NotNull java.util.Optional<@Nullable T> optional()
Gets the value of this configuration as anOptional
.- Returns:
Optional
value
-
set
public abstract void set(@Nullable T value)
Sets the value of this configuration.
After setting, the configuration file will NOT be saved automatically. To save, callConfigurationHolder.save()
.- Parameters:
value
- The value to set
-
setDefault
public void setDefault()
Initializes the default value for this configuration.
After setting, the configuration file will NOT be saved automatically. To save, callConfigurationHolder.save()
.
-
setDefault
public void setDefault(boolean override)
Sets the configuration value to its default.
After setting, the configuration file will NOT be saved automatically. To save, callConfigurationHolder.save()
.- Parameters:
override
- Whether to overwrite existing configured value
-
isDefault
public boolean isDefault()
Checks if the loaded configuration value matches the default value.- Returns:
- Whether the current value is the default value
-
save
public void save() throws java.lang.Exception
Try to save the configuration.
To save multiple modifications, it is recommended to callConfigurationHolder.save()
after all modifications are completed instead of this.- Throws:
java.lang.Exception
- Thrown when an error occurs during saving
-
-