Working with lists of ints, longs, doubles, - advantageous/konf GitHub Wiki

Konf can reads list of numbers.

  • getIntList reads list of ints
  • getLongList reads list of longs
  • getDoubleList reads list of doubles
  • getFloatList reads list of floats

Given this sample configuration file.

test-config.js
var config = {

  floats: [1.0, 2.0, 3.0],
  doubles: [1.0, 2.0, 3.0],
  longs: [1.0, 2.0, 3.0],
  ints: [1, 2, 3],

  intsNull: [1, null, 3],
  intsWrongType: [1, "2", 3]
}

First we load the config file (must be on the class path).

Load config file



    private Config config;

    @Before
    public void setUp() throws Exception {
        config = ConfigLoader.load("test-config.js");
    }
...

Now we can use getDoubleList et al to read values from the config as follows.

Read config items


    @Test
    public void testNumberList() throws Exception {
        assertEquals(asList(1.0, 2.0, 3.0), 
                  config.getDoubleList("doubles"));

        assertEquals(asList(1.0f, 2.0f, 3.0f), 
                  config.getFloatList("floats"));

        assertEquals(asList(1, 2, 3), 
                  config.getIntList("ints"));

        assertEquals(asList(1L, 2L, 3L), 
                  config.getLongList("longs"));
    }

The list of numbers must not contain nulls.

Nulls do not work



    @Test(expected = IllegalArgumentException.class)
    public void listHasNull() {
        assertEquals(asList(1, 2, 3), config.getIntList("intsNull"));
    }

The list of numbers must contain valid numbers.

Wrong types do not work


    @Test(expected = IllegalArgumentException.class)
    public void wrongTypeInList() {
        assertEquals(asList(1, 2, 3), config.getIntList("intsWrongType"));

    }