-
Notifications
You must be signed in to change notification settings - Fork 18
Defining properties
Each configurable value is represented as a property in ConfigMe.
As we saw in the Technical Overview, properties are defined as constants in classes that implement
the SettingsHolder
interface. They're usually defined by statically imported methods of PropertyInitializer
.
Each property defines the path at which it's located in the configuration file, along with the default value to fall
back to in case we can't build it from the configuration file.
import static ch.jalu.configme.properties.PropertyInitializer.newProperty;
public final class TitleConfig implements SettingsHolder {
public static final Property<String> TITLE_TEXT =
newProperty("title.text", "-Default-");
public static final Property<Integer> TITLE_SIZE =
newProperty("title.size", 10);
private TitleConfig() {
// prevent instantiation
}
}
This produces an initial YML configuration that looks as such:
title:
text: '-Default-'
size: 10
Your configuration's properties can be put into multiple settings holder classes.
Comments can be specified for each property, as well as for any paths in between.
Comments are added to properties with the @Comment
annotation:
public final class TitleConfig implements SettingsHolder {
@Comment("The text the title will have")
public static final Property<String> TITLE_TEXT =
newProperty("title.text", "-Default-");
@Comment({
"The font size of the title.",
"The default is 10."
})
public static final Property<Integer> TITLE_SIZE =
newProperty("title.size", 10);
private TitleConfig() {
// prevent instantiation
}
}
Whenever the configuration is saved to the file, it will generate something like this:
title:
# The text the title will have
text: '-Default-'
# The font size of the title.
# The default is 10.
size: 10
Define comments for intermediate paths by overriding the method registerComments
from the SettingsHolder
interface.
public class TitleConfig implements SettingsHolder {
public static final Property<String> TITLE_TEXT =
newProperty("title.text", "-Default-");
public static final Property<Integer> TITLE_SIZE =
newProperty("title.size", 10);
private TitleConfig() {
// prevent instantiation
}
@Override
public void registerComments(CommentsConfiguration conf) {
conf.setComment("title",
"This section defines the title that will be displayed");
}
}
Whenever the configuration is saved to the file, it will generate something like this:
# This section defines the title that will be displayed
title:
text: '-Default-'
size: 10
You can define a comment header of the entire configuration file by setting comments for the path of empty string:
@Override
public void registerComments(CommentsConfiguration conf) {
conf.setComment("",
"Configuration file for my application", "For help, please visit https://example.org");
}
Many property types come out of the box. Typically you create them with PropertyInitializer.
As a non-exhaustive list, the following property types are available:
- String property
- Number properties (integer, double)
- Boolean property
- Enum property
- Bean property (map to a specific JavaBean class)
Then, with a component type of any of the above:
- List property
- Set property
- Map property (where the key type is always String and the value type is configurable)
- Array property
- Optional Property (wraps the given type into an
Optional
)
Specialized types:
- Lowercase string set property –
Set<String>
whose entries are changed to lowercase - InlineArrayProperty – property of array type stored in the resource as a string separated by a delimiter (comma, newline, ...)
Navigation
« Technical overview | Defining properties | The settings manager » |
Guide
- Introduction
- Getting started
- Migration service
- Bean properties
- Custom property types
- Technical documentation
Updating
Development (internal)