Added
getDuration(path)
, getIntList(path)
, and the rest of the numbers (double, float, long).Working with java.time.Duration
getDuration(path)
get a durationgetDurationList(path)
get a duration list
Konf supports "10 seconds" style config for duration as well as
having built-in functions and support for ISO-8601. See documentation
for duration config
for more details.
having built-in functions and support for ISO-8601. See documentation
for duration config
for more details.
Konf can reads list of numbers.
getIntList
reads list of intsgetLongList
reads list of longsgetDoubleList
reads list of doublesgetFloatList
reads list of floats
See documentation list of number configuration
for more details.
for more details.
Thanks
If you like our configuration project, please try our
Reactive Java project
or our Actor based microservices lib.
Reactive Java project
or our Actor based microservices lib.
Downloads
Working with Durations
Konf, the type safe Java configuration library, has a way to configure
java.time.Duration
. You use the getDuration(path)
and getDurationList(path)
method to read a duration from a location.Different way to configure duration
var config = {
tenSeconds: seconds(10),
tenDays: days(10),
tenMinutes: minutes(10),
tenHours: hours(10),
tenMillis: millis(10),
tenMilliseconds: milliseconds(10),
fifteenMinutes: "PT15M",
tenSeconds2: "10 seconds",
tenMinutes2: "10m",
tenHours2: "10 h",
tenDays2: "10 day",
tenMillis2: "10ms"
};
TypeSafe Config style
Duration can be specified with time (type safe config style).
You can postFix the type value in a string with any of these prefixes.
Typesafe config style postfixes
ns, nano, nanos, nanosecond, nanoseconds
us, micro, micros, microsecond, microseconds
ms, milli, millis, millisecond, milliseconds
s, second, seconds
m, minute, minutes
h, hour, hours
d, day, days
ISO-8601 Duration
Konf also supports ISO-8601 duration format, i.e.,
PT15M
.Built-in functions for Duration
There are also built-in functions for durations using
seconds(5)
, minutes(5)
, etc.Example usage
First we load the config.
Load the config
private Config config;
@Before
public void setUp() throws Exception {
config = ConfigLoader.load("test-config.js");
}
Then we use
getDuration(path)
to read the Duration
values. assertEquals(Duration.ofMillis(10),
config.getDuration("tenMillis"));
assertEquals(Duration.ofMillis(10),
config.getDuration("tenMilliseconds"));
assertEquals(Duration.ofSeconds(10), config.getDuration("tenSeconds"));
assertEquals(Duration.ofMinutes(10), config.getDuration("tenMinutes"));
assertEquals(Duration.ofHours(10), config.getDuration("tenHours"));
assertEquals(Duration.ofDays(10), config.getDuration("tenDays"));
assertEquals(Duration.ofMinutes(15),
config.getDuration("fifteenMinutes"));
assertEquals(Duration.ofSeconds(10), config.getDuration("tenSeconds2"));
assertEquals(Duration.ofMinutes(10), config.getDuration("tenMinutes2"));
assertEquals(Duration.ofHours(10), config.getDuration("tenHours2"));
assertEquals(Duration.ofDays(10), config.getDuration("tenDays2"));
assertEquals(Duration.ofMillis(10), config.getDuration("tenMillis2"));
assertEquals(Duration.ofMillis(10), config.getDuration("tenMilliseconds2"));
Working with lists of numbers
Konf reads list of numbers.
getIntList
reads list of intsgetLongList
reads list of longsgetDoubleList
reads list of doublesgetFloatList
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"));
}
No comments:
Post a Comment