PureJ Cfg
Features
- Checked and type-safe access to configuration key/value pairs
- Automatic substitution (resolve) of expressions of the form ${my.key} inside config values
- Convenience functionality: Subsets, merge, range-checks
In contrary to the Apache Commons Configuration library which aims for the same goals, this library is extremly small (just 9k), has much better performance (see below) and does not require additional dependencies.
Requisites
- Java 1.8 or higher
Maven users just need to add the following dependency:
<dependency>
<groupId>com.purej</groupId>
<artifactId>purej-cfg</artifactId>
<version>1.2</version>
</dependency>
Usage
Create a Cfg instance from various sources:
Cfg cfg = new Cfg(); // New empty config
Cfg cfg = new Cfg("myCfg.properties"); // Load from java properties resource or file
Cfg cfg = new Cfg(System.getenv()); // Load from system environment
Access type-safe mandatory config values (throws a CfgException if a key or value is missing or if conversion failed):
boolean myBool = cfg.getBoolean("my.mandatory.boolean.key");
int myInt = cfg.getInt("my.mandatory.int.key");
String myString = cfg.getString("my.mandatory.string.key");
TimeUnit myEnum = cfg.getEnum("my.mandatory.enum.key", TimeUnit.class);
Access type-safe optional config values by providing a default for missing keys/values:
boolean myBool = cfg.getBoolean("my.optional.boolean.key", Boolean.TRUE);
int myInt = cfg.getInt("my.optional.int.key", 42);
String myString = cfg.getString("my.optional.string.key", null);
TimeUnit myEnum = cfg.getEnum("my.optional.enum.key", TimeUnit.class, TimeUnit.DAY);
Change some config values and store to a properties file:
cfg.put("my.key1", 42);
cfg.put("my.key2", "my.value2");
cfg.store(new File("myCfg.properties"));
Performance
Performance comparison to the Apache Commons Configuration library for a common use-case:
- Read a properties file (which contains 20 key/value entries) from disk
- Access properties with different types (string/bool/int/long/decimal)
- Access the same properties over a config-subset
The times are measured as the average of 1 million tries. The test-program can be found under src/test/java.
| PureJ Cfg | Apache Commons Configuration | |
|---|---|---|
| Read property file | 90 micros | 480 micros |
| Access single property | 0.3 micros | 1.5 micros |
| Access single property on a subset | 0.5 micros | 2.8 micros |