Table of Contents

Overview

An easy to use and lightweight Java library for retrieving configuration properties backed by an enum. Supports multi-lined value and variable substitution (e.g. $PATH or ${PATH} as in bash syntax) in the values. Use of enum for defining the property keys will make referencing source less error prone and easy to refactor, as far as this library is concerned

Feature Highlights

Short Description

While using property files are often preferred for defining application configurations due to their simplicity and readability, they also have certain disadvantages. One of the major problem in using property files is that property keys are in the form of String, so it is difficult to manage. If we were to change some key, we need to manually search its occurrences and update all of them. This makes our code fragile and cumbersome. So, this library is designed to provide enum backed configuration property. Due to use of enum, all the property keys are defined in a single place, and since enum constants are referred to instead of plain Strings, refactoring the code becomes easy and less error prone. The APIs also provide methods for retrieval of the values in form of data type that we want. Have a look at Javadocs for details

Prerequisites

Getting Started

<dependency>
    <groupId>com.github.easy-develop</groupId>
    <artifactId>properties</artifactId>
    <version>1.0.1</version>
</dependency>

If you are using any other tool for build, have a look at Maven Repository page for corresponding dependency declaration

public enum MyPropertyKey {
    HOME_DIR,
    CONF_DIR,
    LOG_DIR,
    CURRENT_USER_IDS
}

Note: The enum can be changed to indicate things like whether property key is mandatory or optional, or default value if value is not available. But for our example, we are considering simplest of the cases

import com.easy.properties.Properties;
import com.easy.properties.PropertiesLoader;
import java.util.List;

public class ReadPropsDemo {
    public static void main(String[] args) {
        PropertiesLoader propsLoader = new PropertiesLoader("_path_to_property_file_", MyPropertyKey.class);
        Properties props = propsLoader.load();

        String homeDir = props.get(MyPropertyKey.HOME_DIR);
        List<Integer> currentUserIds = props.getList(MyPropertyKey.CURRENT_USER_IDS, int.class);

        System.out.println("Home directory is: " + homeDir);
        System.out.println("Current users: " + currentUserIds);
    }
}

And that’s it. Obtained instance of com.easy.properties.Properties can be used to retrieve value corresponding to certain enum constant in required format (data type) as shown above. The content of configuration property file could be something like below:

HOME_DIR = /home/demo
CONF_DIR = ${HOME_DIR}/conf
LOG_DIR = ${HOME_DIR}/logs
CURRENT_USER_IDS = 1921, 8887, 7746

Have a look at examples if you want to see the usage in different cases

Support

Please open an issue if you need any assistance or have any suggestion regarding this library

License

This project is licensed under MIT License