CNF-01 is a library that provides immutable configuration for Java projects. Immutability is achieved by converting the configuration source into an immutable Map. The resulting configuration Map can’t be altered even with modifying the underlying source.
-
Provides immutable configurations for:
-
configuration files / path properties (
PathConfiguration
) -
System Properties (
PropertiesConfiguration
) -
environment variables (
EnvironmentConfiguration
)
-
-
Default configurations in case the provided configurations from a source are not found or are otherwise broken (
DefaultConfiguration
)
All sources of configuration can be used with the Configuration
interface. It provides the method asMap()
which returns the immutable configurations.
An example with PathConfiguration
:
Configuration configuration = new PathConfiguration("file/path");
Map<String, String> configurationMap = configuration.asMap();
You might want to specify default configurations in case something brakes. For example, the file path in the code block above might not be found. In the following example, notice how the Map with default configurations are given through the ImmutabilitySupportedMap
object to ensure type safety. DefaultConfiguration
only takes an ImmutableMap
(provided in CNF-01) as a parameter for the defaults.
Map<String, String> map = new HashMap<>();
ImmutableMap<String, String> defaults = new ImmutabilitySupportedMap<>(map).toImmutableMap();
DefaultConfiguration defaultConfiguration = new DefaultConfiguration(
new PathConfiguration("file/path"),
defaults
);
Map<String, String> configurationMap = defaultConfiguration.asMap();
The configuration Map from CNF-01 shouldn’t be used directly in regular objects. It should only be passed to objects that are responsible for providing configured versions of other objects, in other words, Factories.
Small example with an Example
object:
ExampleFactory exampleFactory = new ExampleFactory(configurationMap);
Example example = exampleFactory.example();
Here, the logic for instantiating an Example
is in the ExampleFactory
object, which receives the configuration map from CNF-01 as a parameter. This ensures that the main object Example
is as clear as it can be.
You can involve yourself with our project by opening an issue or submitting a pull request.
Contribution requirements:
-
All changes must be accompanied by a new or changed test. If you think testing is not required in your pull request, include a sufficient explanation as why you think so.
-
Security checks must pass
-
Pull requests must align with the principles and values of extreme programming.
-
Pull requests must follow the principles of Object Thinking and Elegant Objects (EO).
Read more in our Contributing Guideline.
Contributors must sign Teragrep Contributor License Agreement before a pull request is accepted to organization’s repositories.
You need to submit the CLA only once. After submitting the CLA you can contribute to all Teragrep’s repositories.