PureJ Cfg

A small library that allows easy and type-safe access to configuration properties

License

License

GroupId

GroupId

com.purej
ArtifactId

ArtifactId

purej-cfg
Last Version

Last Version

1.2
Release Date

Release Date

Type

Type

jar
Description

Description

PureJ Cfg
A small library that allows easy and type-safe access to configuration properties
Project Organization

Project Organization

Stefan Mueller, adopus consulting gmbh
Source Code Management

Source Code Management

http://github.com/purej/purej-cfg

Download purej-cfg

How to add to project

<!-- https://jarcasting.com/artifacts/com.purej/purej-cfg/ -->
<dependency>
    <groupId>com.purej</groupId>
    <artifactId>purej-cfg</artifactId>
    <version>1.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.purej/purej-cfg/
implementation 'com.purej:purej-cfg:1.2'
// https://jarcasting.com/artifacts/com.purej/purej-cfg/
implementation ("com.purej:purej-cfg:1.2")
'com.purej:purej-cfg:jar:1.2'
<dependency org="com.purej" name="purej-cfg" rev="1.2">
  <artifact name="purej-cfg" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.purej', module='purej-cfg', version='1.2')
)
libraryDependencies += "com.purej" % "purej-cfg" % "1.2"
[com.purej/purej-cfg "1.2"]

Dependencies

test (2)

Group / Artifact Type Version
junit : junit jar 4.8.2
commons-configuration : commons-configuration jar 1.10

Project Modules

There are no modules declared in this project.

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

Versions

Version
1.2
1.1
1.0