-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Configuration.Builder for manual construction
- Adds Configuration.Builder for manually creating a Configuration - Refactor extract the internal CoreConfiguration.postLoad() logic
- Loading branch information
1 parent
65cac70
commit e91594a
Showing
4 changed files
with
181 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
89 changes: 89 additions & 0 deletions
89
avaje-config/src/main/java/io/avaje/config/CoreConfigurationBuilder.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package io.avaje.config; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.ServiceLoader; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
final class CoreConfigurationBuilder implements Configuration.Builder { | ||
|
||
private final Map<String, String> sourceMap = new LinkedHashMap<>(); | ||
private ModificationEventRunner eventRunner; | ||
private ConfigurationLog configurationLog; | ||
|
||
@Override | ||
public Configuration.Builder eventRunner(ModificationEventRunner eventRunner) { | ||
this.eventRunner = eventRunner; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Configuration.Builder log(ConfigurationLog configurationLog) { | ||
this.configurationLog = configurationLog; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Configuration.Builder put(String key, String value) { | ||
requireNonNull(key); | ||
requireNonNull(value); | ||
sourceMap.put(key, value); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Configuration.Builder putAll(Map<String, ?> source) { | ||
requireNonNull(source); | ||
source.forEach((key, value) -> { | ||
if (key != null && value != null) { | ||
put(key, value.toString()); | ||
} | ||
}); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Configuration.Builder putAll(Properties source) { | ||
requireNonNull(source); | ||
source.forEach((key, value) -> { | ||
if (key != null && value != null) { | ||
put(key.toString(), value.toString()); | ||
} | ||
}); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Configuration build() { | ||
return new CoreConfiguration(initRunner(), initLog(), initEntries()) | ||
.postLoad(); | ||
} | ||
|
||
private CoreEntry.CoreMap initEntries() { | ||
final var entries = CoreEntry.newMap(); | ||
sourceMap.forEach((key, value) -> entries.put(key, value, "initial")); | ||
return CoreExpressionEval.evalFor(entries); | ||
} | ||
|
||
private ConfigurationLog initLog() { | ||
if (configurationLog != null) { | ||
return configurationLog; | ||
} else { | ||
return ServiceLoader.load(ConfigurationLog.class) | ||
.findFirst() | ||
.orElseGet(DefaultConfigurationLog::new); | ||
} | ||
} | ||
|
||
private ModificationEventRunner initRunner() { | ||
if (eventRunner != null) { | ||
return eventRunner; | ||
} else { | ||
return ServiceLoader.load(ModificationEventRunner.class) | ||
.findFirst() | ||
.orElseGet(CoreConfiguration.ForegroundEventRunner::new); | ||
} | ||
} | ||
} |
This file contains 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