Skip to content

Commit 84bda82

Browse files
committed
Add config helper
1 parent 1bec275 commit 84bda82

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ allprojects {
2121

2222
// Using Jitpack so I need the repo name in the group to match.
2323
group = 'com.stubbornjava.StubbornJava'
24-
version = '0.1.27-SNAPSHOT'
24+
version = '0.1.28-SNAPSHOT'
2525

2626
repositories {
2727
mavenLocal()

stubbornjava-common/src/main/java/com/stubbornjava/common/Configs.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.File;
44
import java.util.Map;
5+
import java.util.function.Function;
6+
import java.util.function.Supplier;
57

68
import org.jooq.lambda.Seq;
79
import org.slf4j.Logger;
@@ -43,6 +45,20 @@ public static Map<String, Object> asMap(Config config) {
4345
.toMap(e -> e.getKey(), e -> e.getValue().unwrapped());
4446
}
4547

48+
public <T> T getOrDefault(Config config, String path, Function<Config, T> extractor, T defaultValue) {
49+
if (config.hasPath(path)) {
50+
return extractor.apply(config);
51+
}
52+
return defaultValue;
53+
}
54+
55+
public <T> T getOrDefault(Config config, String path, Function<Config, T> extractor, Supplier<T> defaultSupplier) {
56+
if (config.hasPath(path)) {
57+
return extractor.apply(config);
58+
}
59+
return defaultSupplier.get();
60+
}
61+
4662
public static class Builder {
4763
private Config conf;
4864

0 commit comments

Comments
 (0)