Created
June 24, 2016 20:23
-
-
Save matteosb/1429bb0fb10712802b24ca664192c5cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.google.inject.AbstractModule; | |
| import com.typesafe.config.Config; | |
| import static com.google.inject.name.Names.named; | |
| /** | |
| * Abstract guice module with convenience methods for binding values to the same key that they have in typesafe config. | |
| */ | |
| public abstract class TypesafeConfigGuiceModule extends AbstractModule { | |
| protected abstract Config getConfig(); | |
| protected String bindConfigString(String key) { | |
| String value = getConfig().getString(key); | |
| bindConstant().annotatedWith(named(key)).to(value); | |
| return value; | |
| } | |
| protected String bindConfigStringWithDefault(String key, String defaultValue) { | |
| String value = defaultValue; | |
| if (getConfig().hasPath(key)) { | |
| value = getConfig().getString(key); | |
| } | |
| bindConstant().annotatedWith(named(key)).to(value); | |
| return value; | |
| } | |
| protected int bindConfigInt(String key) { | |
| int value = getConfig().getInt(key); | |
| bindConstant().annotatedWith(named(key)).to(value); | |
| return value; | |
| } | |
| protected int bindConfigIntWithDefault(String key, int defaultValue) { | |
| int value = defaultValue; | |
| if (getConfig().hasPath(key)) { | |
| value = getConfig().getInt(key); | |
| } | |
| bindConstant().annotatedWith(named(key)).to(value); | |
| return value; | |
| } | |
| protected boolean bindConfigBoolean(String key) { | |
| boolean value = getConfig().getBoolean(key); | |
| bindConstant().annotatedWith(named(key)).to(value); | |
| return value; | |
| } | |
| protected boolean bindConfigBooleanWithDefault(String key, Boolean defaultValue) { | |
| boolean value = defaultValue; | |
| if (getConfig().hasPath(key)) { | |
| value = getConfig().getBoolean(key); | |
| } | |
| bindConstant().annotatedWith(named(key)).to(value); | |
| return value; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment