public class ConfigTreeBuilder extends ConfigNodeBuilder implements ConfigTree
Usage example:
ConfigBranch config = ConfigTree.builder()
.withValue("A", ConfigTypes.INTEGER, 10)
.fork("child")
.beginValue("drops", ConfigTypes.makeSet(ConfigTypes.STRING)), new HashSet<>(Arrays.asList("diamond", "cactus"))
.withComment("List of things to drop")
.finishValue()
.finishNode()
.build();
ConfigTree.builder(),
PropertyMirrorattributes, parent| Constructor and Description |
|---|
ConfigTreeBuilder(ConfigTree parent,
String name)
Creates a new builder with initial settings.
|
| Modifier and Type | Method and Description |
|---|---|
ConfigTreeBuilder |
applyFromPojo(Object pojo)
Configure this builder using a POJO (Plain Old Java Object).
|
ConfigTreeBuilder |
applyFromPojo(Object pojo,
AnnotatedSettings settings)
Configure this builder using a POJO (Plain Old Java Object).
|
<T,R> ConfigLeafBuilder<T,R> |
beginValue(String name,
ConfigType<R,T,?> type,
R defaultValue) |
<T> ConfigLeafBuilder<T,T> |
beginValue(String name,
SerializableType<T> type,
T defaultValue)
Creates a scalar
ConfigLeafBuilder with the given default value. |
ConfigBranch |
build()
Construct a new
ConfigNode based on this builder's specifications. |
ConfigTreeBuilder |
finishBranch()
Builds and registers the
ConfigNode with the parent node. |
ConfigTreeBuilder |
finishBranch(Consumer<ConfigBranch> action)
Builds and registers the
ConfigNode with the parent node before
running the given action on the newly built node. |
ConfigTreeBuilder |
fork(String name)
Forks this builder, creating a subtree whose parent is this node.
|
NodeCollection |
getItems()
Returns a collection of this builder's children.
|
ConfigNode |
lookup(String name)
Tries to find a child in this builder by name.
|
boolean |
lookupAndBind(String name,
PropertyMirror<?> mirror)
Tries to find a child leaf in this node by name.
|
ConfigBranch |
lookupBranch(String name)
Tries to find a child branch in this node by name.
|
<T> ConfigLeaf<T> |
lookupLeaf(String name,
SerializableType<T> type)
Tries to find a child leaf in this node by name.
|
ConfigTreeBuilder |
withAttribute(ConfigAttribute<?> attribute)
Adds a single
ConfigAttribute to the built ConfigNode. |
<A> ConfigTreeBuilder |
withAttribute(FiberId id,
SerializableType<A> type,
A defaultValue)
Adds a
ConfigAttribute to the built ConfigBranch. |
ConfigTreeBuilder |
withAttributes(Collection<ConfigAttribute<?>> attributes)
Adds a collection of
ConfigAttribute to the built ConfigNode. |
ConfigTreeBuilder |
withChild(ConfigNode item)
Attempts to introduce a new child to this builder.
|
ConfigTreeBuilder |
withChild(ConfigNode item,
boolean overwrite)
Attempts to introduce a new child to this builder.
|
ConfigTreeBuilder |
withComment(String comment)
Sets the
ConfigBranch's comment. |
<R> ConfigTreeBuilder |
withMirroredValue(String name,
PropertyMirror<R> mirror,
R defaultValue)
Adds a
ConfigLeaf bound to a PropertyMirror, using the mirror's type information. |
ConfigTreeBuilder |
withName(String name)
Sets the
ConfigBranch's name. |
ConfigTreeBuilder |
withParent(ConfigTreeBuilder parent)
Sets the parent builder for this builder.
|
ConfigTreeBuilder |
withSeparateSerialization()
Marks the built subtree as being serialized separately.
|
ConfigTreeBuilder |
withSeparateSerialization(boolean serializeSeparately)
Sets whether a subtree should be serialized separately.
|
<R,S> ConfigTreeBuilder |
withValue(String name,
ConfigType<R,S,?> type,
R defaultValue)
Adds a
ConfigLeaf with a type and default value derived from another type. |
<T> ConfigTreeBuilder |
withValue(String name,
SerializableType<T> type,
T defaultValue)
Adds a
ConfigLeaf with the given type and default value to the tree. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitbuilder, builderpublic ConfigTreeBuilder(@Nullable ConfigTree parent, @Nullable String name)
parent - the initial parentname - the initial nameConfigTree.builder(),
ConfigTree.builder(ConfigTree, String)@Nonnull public NodeCollection getItems()
The returned collection is guaranteed to have no two nodes with the same name. Elements may be freely added and removed from it.
getItems in interface ConfigTreeNodeCollection.add(ConfigNode, boolean),
NodeCollection.removeByName(String)@Nullable public ConfigNode lookup(String name)
lookup in interface ConfigTreename - The name of the child to look fornull@Nullable public ConfigBranch lookupBranch(String name)
lookupBranch in interface ConfigTreename - The name of the child to look for.@Nullable public <T> ConfigLeaf<T> lookupLeaf(String name, SerializableType<T> type)
lookupLeaf in interface ConfigTreeT - the type of values held by the leafname - the name of the leaf to look fortype - a SerializableType object representing the type of values held by the leafnullpublic boolean lookupAndBind(String name, PropertyMirror<?> mirror)
lookupAndBind in interface ConfigTreename - the name of the leaf to mirrormirror - the mirror to bind to the leaftrue if the operation succeededpublic ConfigTreeBuilder withParent(ConfigTreeBuilder parent)
parent - The parent builder.IllegalStateException - If no name has been set in this builder.public ConfigTreeBuilder withName(String name)
ConfigBranch'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 ConfigTreeBuilder withComment(@Nullable String comment)
ConfigBranch'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> ConfigTreeBuilder withAttribute(FiberId id, SerializableType<A> type, A defaultValue)
ConfigAttribute to the built ConfigBranch.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 ConfigTreeBuilder withAttributes(Collection<ConfigAttribute<?>> attributes)
ConfigAttribute to the built ConfigNode.withAttributes in class ConfigNodeBuilderattributes - A collection of attributes.public ConfigTreeBuilder withAttribute(ConfigAttribute<?> attribute)
ConfigAttribute to the built ConfigNode.withAttribute in class ConfigNodeBuilderattribute - The attribute.public ConfigTreeBuilder withSeparateSerialization()
A subtree marked for separate serialization will not appear in the serialized representation of its ancestors. This property can be useful when partitioning a big configuration tree into several files.
This method has no effect if the built node is a tree root.
this, for chainingwithSeparateSerialization(),
ConfigBranch.isSerializedSeparately()public ConfigTreeBuilder withSeparateSerialization(boolean serializeSeparately)
If serializeSeparately is true, the subtree created
from this builder will not appear in the serialized representation of the
ancestor. This property can be especially useful when partitioning a
big configuration tree into several files.
This method has no effect if the built node is a tree root.
serializeSeparately - true if the built tree should be serialized separatelythis, for chainingpublic ConfigTreeBuilder applyFromPojo(Object pojo) throws RuntimeFiberException
The tree structure will be based on the pojo's fields,
recursively generating settings. The generated settings can be configured
in the pojo's class declaration, using annotations such as Setting.
The generated ConfigLeafs will be bound to their respective fields,
setting the latter when the former's value is updated.
If a FiberException is thrown when using the underlying AnnotatedSettings,
it is wrapped in a RuntimeFiberException.
pojo - an object serving as a base to reflectively generate a config treethis, for chainingRuntimeFiberExceptionSetting,
Settings,
AnnotatedSettings.applyToNode(ConfigTree, Object),
applyFromPojo(Object, AnnotatedSettings)public ConfigTreeBuilder applyFromPojo(Object pojo, AnnotatedSettings settings) throws RuntimeFiberException
The node's structure will be based on the pojo's fields,
recursively generating settings. The generated settings can be configured
in the pojo's class declaration, using annotations such as Setting.
The generated ConfigLeafs will be bound to their respective fields,
setting the latter when the former's value is updated.
If a FiberException is thrown when using the underlying AnnotatedSettings,
it is wrapped in a RuntimeFiberException.
pojo - an object serving as a base to reflectively generate a config treesettings - an AnnotatedSettings instance used to configure this builderthis, for chainingRuntimeFiberException@Setting,
@Settings,
AnnotatedSettings.applyToNode(ConfigTree, Object)public <T> ConfigLeafBuilder<T,T> beginValue(@Nonnull String name, @Nonnull SerializableType<T> type, @Nonnull T defaultValue)
ConfigLeafBuilder with the given default value.T - the type type representstype - the class of the type of value the ConfigLeaf produced by the builder holdswithValue(String, SerializableType, Object),
ConfigLeafBuilder,
ConfigTypespublic <T,R> ConfigLeafBuilder<T,R> beginValue(@Nonnull String name, @Nonnull ConfigType<R,T,?> type, @Nonnull R defaultValue)
public <T> ConfigTreeBuilder withValue(@Nonnull String name, @Nonnull SerializableType<T> type, @Nonnull T defaultValue)
ConfigLeaf with the given type and default value to the tree.
This method allows only basic configuration of the created leaf.
For more flexibility, beginValue(java.lang.String, io.github.fablabsmc.fablabs.api.fiber.v1.schema.type.SerializableType<T>, T) can be used.
T - the type of value the ConfigLeaf holds.name - the name of the child leaftype - the type of values held by the leafdefaultValue - the default value of the ConfigLeaf to create.this, for chainingbeginValue(String, SerializableType, Object),
ConfigTypespublic <R,S> ConfigTreeBuilder withValue(@Nonnull String name, @Nonnull ConfigType<R,S,?> type, @Nullable R defaultValue)
ConfigLeaf with a type and default value derived from another type.
The built leaf will only accept values of type's
serializable config type.
The full derived type information is only used to convert the provided defaultValue
to a valid serialized form. Property mirrors can be used
to interact seamlessly with the leaf using runtime types.
This method allows only basic configuration of the created leaf.
For more flexibility, beginValue(java.lang.String, io.github.fablabsmc.fablabs.api.fiber.v1.schema.type.SerializableType<T>, T) can be used.
R - the runtime type of the defaultValue representation.S - the type of value the ConfigLeaf holds.name - the name of the child leaftype - the type of values held by the leafdefaultValue - the runtime representation of the default value of the ConfigLeaf to create.this, for chainingbeginValue(String, SerializableType, Object),
ConfigTypespublic <R> ConfigTreeBuilder withMirroredValue(@Nonnull String name, @Nonnull PropertyMirror<R> mirror, @Nonnull R defaultValue)
ConfigLeaf bound to a PropertyMirror, using the mirror's type information.
This method behaves as if:
this.beginValue(name, mirror.getMirroredType(), defaultValue).finishValue(mirror::mirror)
The built leaf will only accept values of the mirror's
serializable config type.
The full derived type information is only used to convert the provided defaultValue
to a valid serialized form. The mirror can be used to interact seamlessly
with the leaf using runtime types.
This method allows only basic configuration of the created leaf.
For more flexibility, beginValue(java.lang.String, io.github.fablabsmc.fablabs.api.fiber.v1.schema.type.SerializableType<T>, T) can be used.
R - the runtime type of the defaultValue representation.name - the name of the child leafmirror - a mirror to be bound to the leafdefaultValue - the runtime representation of the default value of the ConfigLeaf to create.this, for chainingbeginValue(String, SerializableType, Object),
ConfigTypespublic ConfigTreeBuilder withChild(@Nonnull ConfigNode item) throws DuplicateChildException
item - The child to addthis, for chainingDuplicateChildException - if there was already a child by the same namePropertypublic ConfigTreeBuilder withChild(@Nonnull ConfigNode item, boolean overwrite) throws DuplicateChildException
item - The child to addoverwrite - whether existing items should be overwrittenthis, for chainingDuplicateChildException - if there was already a child by the same namepublic ConfigTreeBuilder fork(String name)
name - the name of the new Nodepublic ConfigBranch build() throws RuntimeFiberException
ConfigNode based on this builder's specifications.
Calling this method more than once with the same parameters (specifically same parent and/or children) may result in exceptions being thrown, as the resulting tree structure will be invalid.
build in class ConfigNodeBuilderConfigNodeRuntimeFiberException - if building the node results in an invalid treepublic ConfigTreeBuilder finishBranch()
ConfigNode with the parent node.
This method is equivalent to this.finishBranch(branch -> {}).public ConfigTreeBuilder finishBranch(Consumer<ConfigBranch> action)
ConfigNode with the parent node before
running the given action on the newly built node.action - An operation to run on the built ConfigBranch.