Class ConfigurationStore
- All Implemented Interfaces:
Iterable<ComponentType>
,Channel
,ComponentType
,Eligible
,Manager
- Direct Known Subclasses:
NightConfigStore
,PreferencesStore
Implementing classes must
override one of the methods structured(String)
or
values(String)
as the default implementations of either
calls the other.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.jgrapes.core.Channel
Channel.Default
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreates a new component with its channel set to itself.ConfigurationStore
(Channel componentChannel) Creates a new component base with its channel set to the given channel.ConfigurationStore
(Channel componentChannel, HandlerDefinition.ChannelReplacements channelReplacements) Creates a new component base likeConfigurationStore(Channel)
but with channel mappings forHandler
annotations. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Optional
<T> If the value is notnull
, return it as the requested type.Short foras(value, Boolean.class)
.Short foras(value, Instant.class)
.Short foras(value, Number.class)
.Short foras(value, String.class)
.Configuration information should be kept simple.mergeValue
(Map<?, ?> target, String selector, Object value) Similar tostructure(Map)
but merges only a single value into an existing map.Same asstructure(Map, boolean)
withfalse
as second argument.The reverse operation toflatten(Map)
.structured
(String path) Return the properties for a given path if they exists as structured data, seestructure(Map)
.Return the values for a given path if they exist.Methods inherited from class org.jgrapes.core.Component
channel, component, defaultCriterion, isEligibleFor, setName
Methods inherited from class org.jgrapes.core.internal.ComponentVertex
activeEventPipeline, addHandler, attach, channelReplacements, children, componentPath, componentVertex, detach, fire, initComponentsHandlers, iterator, name, newEventPipeline, newEventPipeline, parent, registerAsGenerator, root, toString, unregisterAsGenerator
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
NUMBER
-
-
Constructor Details
-
ConfigurationStore
public ConfigurationStore()Creates a new component with its channel set to itself. -
ConfigurationStore
Creates a new component base with its channel set to the given channel.As a special case
Channel.SELF
can be passed to the constructor to make the component use itself as channel. The special value is necessary as you obviously cannot pass an object to be constructed to its constructor.- Parameters:
componentChannel
- the channel that the component’s handlers listen on by default and thatManager.fire(Event, Channel...)
sends the event to
-
ConfigurationStore
public ConfigurationStore(Channel componentChannel, HandlerDefinition.ChannelReplacements channelReplacements) Creates a new component base likeConfigurationStore(Channel)
but with channel mappings forHandler
annotations.- Parameters:
componentChannel
- the channel that the component’s handlers listen on by default and thatManager.fire(Event, Channel...)
sends the event tochannelReplacements
- the channel replacements to apply to thechannels
elements of theHandler
annotations
-
-
Method Details
-
flatten
Configuration information should be kept simple.Sometimes, however, it is unavoidable to structure the information associated with a (logical) key. This can be done by reflecting the structure in the names of actual keys, derived from the logical key. Names such as “key.0”, “key.1”, “key.2” can be used to express that the value associated with “key” is a list of values. “key.a”, “key.b”, “key.c” can be used to associate “key” with a map from “a”, “b”, “c” to some values.
This methods looks at all values in the map passed as argument. If the value is a collection or map, the entry is converted to several entries following the pattern outlined above.
- Parameters:
structured
- the map with possibly structured properties- Returns:
- the map with flattened properties
-
structure
Same asstructure(Map, boolean)
withfalse
as second argument.- Parameters:
flatProperties
- the flat properties- Returns:
- a map with structured values
-
structure
The reverse operation toflatten(Map)
.Entries with key names matching the pattern outlined in
flatten(Map)
are combined to a single entry with a structured value (map or list).Usually, only key patterns with consecutive numbers starting with zero are converted to lists (e.g.
key.0
,key.1
,key.2
). If entries are missing, the values at that level are converted to aMap<Integer,Object>
with the given entries instead. IfconvertSparse
istrue
, incomplete index sets such askey.2
,key.3
,key.5
are converted to lists with the available number of elements despite the missing entries.If the derived class overrides
structured(String)
, the leaf values in the returned structure are the values provided by the overriding implementation (while {@link #values(String))} always providesString
s). Some configuration formats define types other then string and therefore value can be e.g.Integer
s orInstant
s. In order to support the usage of arbitrary configuration store implementations, values obtained from the data structure returned bystructure(Map, boolean)
should always be passed throughas(Object, Class)
. This method preserves non-string objects if they match the requested type or converts the value from its string representation to the requested type, if possible.- Parameters:
flatProperties
- the flat propertiesconvertSparse
- controls conversion to lists- Returns:
- a map with structured values
-
mergeValue
Similar tostructure(Map)
but merges only a single value into an existing map.- Parameters:
target
- the targetselector
- the path selectorvalue
- the value- Returns:
- the map
-
values
Return the values for a given path if they exist.This method should only be used in cases where configuration values are needed before the
InitialConfiguration
event is fired, e.g. while creating the component tree.- Parameters:
path
- the path- Returns:
- the values, if defined for the given path
-
structured
Return the properties for a given path if they exists as structured data, seestructure(Map)
.- Parameters:
path
- the path- Returns:
- the values, if defined for the given path
-
as
If the value is notnull
, return it as the requested type.The method is successful if the value already is of the requested type (or a subtype) or if the value is of type
String
and can be converted to the requested type.Supported types are:
String
Number
, converts fromString
usingBigDecimal(String)
Instant
, converts fromTemporalAccessor
or fromString
using {@link Instant#parse(CharSequence))Boolean
, converts from {@link String} usingBoolean.valueOf(String)
- Returns:
- the value
-
asString
Short foras(value, String.class)
.- Parameters:
value
- the value- Returns:
- the optional
-
asNumber
Short foras(value, Number.class)
.- Parameters:
value
- the value- Returns:
- the optional
-
asInstant
Short foras(value, Instant.class)
.- Parameters:
value
- the value- Returns:
- the optional
-
asBoolean
Short foras(value, Boolean.class)
.- Parameters:
value
- the value- Returns:
- the optional
-