Skip to content

Latest commit

 

History

History
42 lines (29 loc) · 1.12 KB

example-2.md

File metadata and controls

42 lines (29 loc) · 1.12 KB

Example - Getting custom value

You can define a custom function for getting a custom value from configuration. The function has to an input parameter of type Configuration and it has to return the specified custom value. The function converts a configuration node to the custom value.

Configuration file

math {
    big_integer 1234567890098765432112345678900987654321;
}

Source code

import com.github.twoleds.configuration.Configuration;

import java.math.BigInteger;

public class Test {

    // it converts configuration node to an instance of BigInteger
    public static BigInteger toBigInteger(Configuration conf) {
        return new BigInteger(conf.getValue());
    }

    public static void main(String[] args) throws Exception {

        // parse configuration from a string/file/URL
        String confStr = "math { big_integer 1234567890098765432112345678900987654321; }";
        Configuration conf = Configuration.parse(confStr);

        // get a BigInteger value from the configuration
        BigInteger value = conf.query("math/big_integer", Test::toBigInteger, BigInteger.ZERO);

    }

}