T - the type of value the produced ConfigLeaf will holdpublic class ConfigLeafBuilder<T,R> extends ConfigNodeBuilder
ConfigLeafs.ConfigLeaf| Modifier and Type | Field and Description |
|---|---|
protected Function<T,R> |
deserializer |
protected Function<R,T> |
serializer |
protected SerializableType<T> |
type |
attributes, comment, name, parent| Modifier and Type | Method and Description |
|---|---|
ConfigLeaf<T> |
build()
Builds the
ConfigLeaf. |
static <T,R> ConfigLeafBuilder<T,R> |
create(ConfigTreeBuilder parentNode,
String name,
ConfigType<R,T,?> type,
R defaultValue)
Creates a builder for a leaf node with the given parent, name, and type.
|
static <T> ConfigLeafBuilder<T,T> |
create(ConfigTreeBuilder parentNode,
String name,
SerializableType<T> type,
T defaultValue)
Creates a builder for a leaf node with the given parent, name, and serialized type.
|
ConfigTreeBuilder |
finishValue()
Builds and registers the
ConfigLeaf with the parent node. |
ConfigTreeBuilder |
finishValue(Consumer<ConfigLeaf<T>> action)
Builds and registers the
ConfigLeaf with the parent node before
running the given action on the newly built leaf. |
SerializableType<T> |
getType()
Returns the type of values stored in the leaf node.
|
ConfigLeafBuilder<T,R> |
withAttribute(ConfigAttribute<?> attribute)
Adds a single
ConfigAttribute to the built ConfigNode. |
<A> ConfigLeafBuilder<T,R> |
withAttribute(FiberId id,
SerializableType<A> type,
A defaultValue)
Adds a
ConfigAttribute to the built ConfigNode. |
ConfigLeafBuilder<T,R> |
withAttributes(Collection<ConfigAttribute<?>> attributes)
Adds a collection of
ConfigAttribute to the built ConfigNode. |
ConfigLeafBuilder<T,R> |
withComment(String comment)
Sets the
ConfigLeaf's comment. |
ConfigLeafBuilder<T,R> |
withDefaultValue(R defaultValue)
Sets the default value.
|
ConfigLeafBuilder<T,R> |
withListener(BiConsumer<R,R> consumer)
Adds a listener to the
ConfigLeaf. |
ConfigLeafBuilder<T,R> |
withName(String name)
Sets the
ConfigLeaf's name. |
@Nonnull protected final SerializableType<T> type
public static <T,R> ConfigLeafBuilder<T,R> create(ConfigTreeBuilder parentNode, @Nonnull String name, @Nonnull ConfigType<R,T,?> type, @Nonnull R defaultValue)
T - The type of serialized values the leaf node stores.R - The runtime type of values the builder receives.parentNode - The parent node builder.name - The name for the leaf node.type - The ConfigType for values stored in this leaf node.
The actual type stored in the leaf node is represented by type.getSerializedType().public static <T> ConfigLeafBuilder<T,T> create(ConfigTreeBuilder parentNode, @Nonnull String name, @Nonnull SerializableType<T> type, @Nonnull T defaultValue)
T - The type of serialized values the leaf node stores.parentNode - The parent node builder.name - The name for the leaf node.type - The SerializableType for values stored in this leaf node.@Nonnull public SerializableType<T> getType()
public ConfigLeafBuilder<T,R> withName(@Nonnull String name)
ConfigLeaf's name.withName in class ConfigNodeBuildername - the namethis builderConfigTree.lookupLeaf(java.lang.String, io.github.fablabsmc.fablabs.api.fiber.v1.schema.type.SerializableType<T>)public ConfigLeafBuilder<T,R> withComment(String comment)
ConfigLeaf's comment.
If null, or if this method is never called, the ConfigLeaf won't have a comment.
An empty comment (non null, but only consisting of whitespace) will be serialised.
withComment in class ConfigNodeBuildercomment - the commentthis builderpublic <A> ConfigLeafBuilder<T,R> withAttribute(FiberId id, SerializableType<A> type, A defaultValue)
ConfigNodeBuilderConfigAttribute to the built ConfigNode.withAttribute in class ConfigNodeBuilderA - the type of values stored in the attributeid - the id of the attributetype - the class object representing the type of values stored in the attributedefaultValue - the attribute's default valuethis, for chainingConfigNode.getAttributes()public ConfigLeafBuilder<T,R> withAttributes(Collection<ConfigAttribute<?>> attributes)
ConfigNodeBuilderConfigAttribute to the built ConfigNode.withAttributes in class ConfigNodeBuilderattributes - A collection of attributes.public ConfigLeafBuilder<T,R> withAttribute(ConfigAttribute<?> attribute)
ConfigNodeBuilderConfigAttribute to the built ConfigNode.withAttribute in class ConfigNodeBuilderattribute - The attribute.public ConfigLeafBuilder<T,R> withListener(BiConsumer<R,R> consumer)
ConfigLeaf.
Listeners are called when the value of a ConfigLeaf is changed. They are of type BiConsumer: the first argument being the old value, and the second argument being the new value.
Listeners set with this method are chained: if there was already one specified, a new listener is created that calls the old one first, and then the new one.
consumer - the listenerthis builderpublic ConfigLeafBuilder<T,R> withDefaultValue(R defaultValue)
If null, or if this method is never called, the ConfigLeaf will have no default value.
Note that every ConfigLeaf created from this builder will share a reference
to the given defaultValue. Immutability is encouraged.
defaultValue - the default valuethis builderpublic ConfigLeaf<T> build()
ConfigLeaf.
If a parent was specified in the constructor, the ConfigLeaf will also be registered to its parent node.
This method should not be called multiple times if the default value is intended to be mutated. Multiple calls will result in duplicated references to the default value.
build in class ConfigNodeBuilderConfigLeaffinishValue(Consumer)public ConfigTreeBuilder finishValue()
ConfigLeaf with the parent node.
This method is equivalent to this.finishValue(leaf -> {}).finishValue(Consumer),
build()public ConfigTreeBuilder finishValue(Consumer<ConfigLeaf<T>> action)
ConfigLeaf with the parent node before
running the given action on the newly built leaf.action - An operation to run on the built ConfigLeaf.build()