-
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.
Merge pull request #90 from avaje/feature/putAll-wildcard
Add Configuration.Builder for manual construction
- Loading branch information
Showing
8 changed files
with
257 additions
and
28 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
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
121 changes: 121 additions & 0 deletions
121
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,121 @@ | ||
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; | ||
private ResourceLoader resourceLoader; | ||
|
||
private boolean includeResourceLoading; | ||
private InitialLoader initialLoader; | ||
|
||
@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 resourceLoader(ResourceLoader resourceLoader) { | ||
this.resourceLoader = resourceLoader; | ||
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.Builder includeResourceLoading() { | ||
this.includeResourceLoading = true; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Configuration build() { | ||
final var runner = initRunner(); | ||
final var log = initLog(); | ||
if (includeResourceLoading) { | ||
log.preInitialisation(); | ||
initialLoader = new InitialLoader(log, initResourceLoader()); | ||
} | ||
return new CoreConfiguration(runner, log, initEntries()).postLoad(initialLoader); | ||
} | ||
|
||
private CoreEntry.CoreMap initEntries() { | ||
final var entries = initEntryMap(); | ||
sourceMap.forEach((key, value) -> entries.put(key, value, "initial")); | ||
return CoreExpressionEval.evalFor(entries); | ||
} | ||
|
||
private CoreEntry.CoreMap initEntryMap() { | ||
return initialLoader == null ? CoreEntry.newMap() : initialLoader.load(); | ||
} | ||
|
||
private ResourceLoader initResourceLoader() { | ||
if (resourceLoader == null) { | ||
resourceLoader = ServiceLoader.load(ResourceLoader.class) | ||
.findFirst() | ||
.orElseGet(DefaultResourceLoader::new); | ||
} | ||
return resourceLoader; | ||
} | ||
|
||
private ConfigurationLog initLog() { | ||
if (configurationLog == null) { | ||
configurationLog = ServiceLoader.load(ConfigurationLog.class) | ||
.findFirst() | ||
.orElseGet(DefaultConfigurationLog::new); | ||
} | ||
return configurationLog; | ||
} | ||
|
||
private ModificationEventRunner initRunner() { | ||
if (eventRunner == null) { | ||
eventRunner = ServiceLoader.load(ModificationEventRunner.class) | ||
.findFirst() | ||
.orElseGet(CoreConfiguration.ForegroundEventRunner::new); | ||
} | ||
return eventRunner; | ||
} | ||
} |
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
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
Oops, something went wrong.