Class Options
- All Implemented Interfaces:
Serializable, Cloneable, Map<String,Object>, Clearable, EmptyAccessor, org.refcodes.ontology.Ontology, Diagnosable, Schemable<DiagnosticOptions>, Containable, ImmutableTable<String, Object>, ImmutableTable.MutableTable<String, Object>, Keys<String, Object>, Keys.MutableKeys<String, Object>, Keys.MutableValues<String, Object>
- Direct Known Subclasses:
DiagnosticOptions
Options class serves as a generic information container for
providing arbitrary, optional parameters and interfaces with the runtime
environment: The Options container provides flexible, extensible
configuration lookup with support for explicit values, JVM system properties,
and environment variables.
It is typically used when additional parameters are required that are not known in advance and must be supplied without extending or breaking the existing API.
Extensions of this class should implement type safe convenience methods for retrieving their specific option values. These methods might also take system properties or environment variables into account, depending on the according implementation and requirements.
Options primarily behave like a Map and allows
explicit option values to be set programmatically. In addition, when an
option is requested via a getter and no explicit value is present, the lookup
transparently falls back to external configuration sources.
Note: As of environment variable lookup and system property mapping, make sure to provide a sound namespace for the option keys: The first part(s) of your option keys should identify your namespace!
Resolution order
Option values are resolved in the following order:
- Explicitly set option value (stored in this
Optionsinstance) - JVM system property
- Environment variable
If none of these sources provides a value, null is returned.
System property key mapping
When falling back to JVM system properties, the option key is transformed according to the following rules:
- camelCase word boundaries are converted to dot separators
- underscore characters (
_) are replaced by dots (.) - the resulting key is converted to lower case
Example:
option key: maxBufferSize system key: max.buffer.size
Environment variable key mapping
When falling back to environment variables, the option key is transformed according to the following rules:
- camelCase word boundaries are converted to underscore separators
- dot characters (
.) are replaced by underscores (_) - the resulting key is converted to upper case
Example:
option key: maxBufferSize environment: MAX_BUFFER_SIZE
Design intent
This class is intended as a lightweight configuration bridge between programmatic configuration and externally supplied parameters, especially for implementation-specific or extensible subsystems where not all options can be known in advance.
The fallback to system properties and environment variables is deliberately transparent but deterministic. The lookup rules are fixed and documented so that configuration remains predictable and debuggable.
Note that this class does not perform type conversion beyond the string-based lookup provided by system properties and environment variables. Callers are responsible for interpreting option values according to their expected type.
This class is not intended as a general-purpose configuration framework, but as an infrastructure-level utility for SDKs and toolkits that require flexible, low-friction option passing.
Extensions
Subclasses of Options typically expose a static builder()
factory method which returns a specialized Options.Builder implementation.
This builder is used to configure and finally create an instance of the
extended Options type.
During Options.Builder.build(), the builder invokes the protected constructor
Options(Builder) of the extended Options class in
order for the configured option values to be transfered into the newly
created instance. Both the builder and the extended Options type
usually provide type safe convenience methods for setting and retrieving
supported options.
Subclasses may additionally define an enumeration implementing the
DiagnosticOptions interface. Such an enumeration can be used to
declare well known options and to provide additional diagnostic metadata,
such as descriptions or option's types, which is exposed via the
toSchema() method. Using an enumeration also helps to avoid key and
type mismatches when working with known options which provide a type. The
constructor of subclassed Options classes then usually invokes the
(super) Options(Builder, Class) constructor to provide the
Builder alongside the configuration enumeration for construction.
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class Settings
Settings.Node, Settings.Properties, Settings.RootNested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> Nested classes/interfaces inherited from interface EmptyAccessor
EmptyAccessor.EmptyBuilder<B>, EmptyAccessor.EmptyMutator, EmptyAccessor.EmptyPropertyNested classes/interfaces inherited from interface ImmutableTable
ImmutableTable.MutableTable<K,V> Nested classes/interfaces inherited from interface Keys
Keys.MutableKeys<K,V>, Keys.MutableValues<K, V> -
Field Summary
Fields inherited from class org.refcodes.ontology.Taxonomy
NAMESPACE_SEPARATOR -
Constructor Summary
ConstructorsModifierConstructorDescriptionOptions()Creates an emptyOptionsinstance.protectedOptions(Options.Builder aBuilder) Instantiates a new instance from the given builder.protectedOptions(Options.Builder aBuilder, Class<? extends Enum<?>> aOptions) Instantiates a new instance from the given builder and register options provided by the configuration enumeration. -
Method Summary
Modifier and TypeMethodDescriptionstatic Options.Builderbuilder()Creates a newOptions.Builderfor constructingOptionsinstances.protected voidenrichSchema(Schema.Builder aBuilder, Settings.Node aNode, DiagnosticOptions aOptions) Hook for subclasses to enrich aSchemanode.Retrieves the value associated with the given option key.Returns theOptions.RegistryItemassociated with the given key.In case options with type information were registered, type conformance is enforced.protected voidRegister options provided by the configuration enumeration.toSchema()Methods inherited from class Settings
createResult, fromNamespace, merge, ofNamespace, removeNamespace, toSchema, toSchema, transformKeys, transformValuesMethods inherited from class org.refcodes.ontology.Taxonomy
contains, containsNamespace, get, get, get, get, getOr, namespaces, put, put, remove, toKey, toNormalizedMethods inherited from class HashMap
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, getOrDefault, isEmpty, keySet, merge, newHashMap, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, valuesMethods inherited from class AbstractMap
equals, hashCode, toStringMethods inherited from interface Containable
sizeMethods inherited from interface EmptyAccessor
isEmptyMethods inherited from interface ImmutableTable.MutableTable
delete, putMethods inherited from interface Keys
containsKey, containsValue, getOr, keySet, valuesMethods inherited from interface Map
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, getOrDefault, hashCode, isEmpty, keySet, merge, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, valuesMethods inherited from interface org.refcodes.ontology.Ontology
get, get, getOr, getOr, getOr
-
Constructor Details
-
Options
public Options()Creates an emptyOptionsinstance.This constructor is intended for quick experimentation or ad-hoc usage. More sophisticated subclasses typically provide type-safe accessors for their supported options and expose a static
builder()factory method.Such builders usually extend the
Options.Builderclass and configure option values before creating the finalOptionsinstance via the protectedOptions(Builder)constructor.Subclasses may additionally register known options by supplying enumerations to
register(Class)or by using theOptions(Builder, Class)constructor in order to enrich diagnostic output and enable type-safe configuration. -
Options
Instantiates a new instance from the given builder.- Parameters:
aBuilder- The builder from which to populate the this instance.
-
Options
Instantiates a new instance from the given builder and register options provided by the configuration enumeration.In case the provided enumeration implements the
Optioninterface, the the according metadata is used to enrich diagnostic output (as oftoSchema()and enable type-safe configuration of theOptionsinstance (as of the overriddenput(String, Object)method).- Parameters:
aBuilder- The builder from which to populate the this instance.aOptions- The enumeration describing the options.
-
-
Method Details
-
get
Retrieves the value associated with the given option key.If no explicit value is present in this
Optionsinstance, the lookup transparently falls back to external configuration sources in the following order:- JVM system property (derived from the option by calling
SystemProperty.toSystemPropertyName(String)) - Environment variable (derived from the option key by calling
EnvironmentVariable.toEnvironmentVariableName(String))
If no value is found,
nullis returned. - JVM system property (derived from the option by calling
-
put
-
toSchema
-
builder
Creates a newOptions.Builderfor constructingOptionsinstances.- Returns:
- A new builder instance
-
enrichSchema
protected void enrichSchema(Schema.Builder aBuilder, Settings.Node aNode, DiagnosticOptions aOptions) Description copied from class:SettingsHook for subclasses to enrich aSchemanode.This method is invoked for each node during schema construction and allows subclasses to attach additional metadata such as diagnostic or runtime information.
The default implementation does nothing.
- Overrides:
enrichSchemain classSettings- Parameters:
aBuilder- The schema builder representing the current node.aNode- The internal node representation.aOptions- Optional diagnostic options.
-
register
-
getOption
Returns theOptions.RegistryItemassociated with the given key.- Parameters:
aKey- The option's key- Returns:
- The
Options.RegistryItemor null if not found.
-