From bba56f2e987dd333db5c7e8d7f87ce6ee3d4dc1e Mon Sep 17 00:00:00 2001 From: Brian Baldino Date: Wed, 23 Sep 2020 09:00:03 -0700 Subject: [PATCH] return an Optional from parseApplicationReplacement --- .../java/com/typesafe/config/ConfigFactory.java | 14 ++++++++------ .../config/DefaultConfigLoadingStrategy.java | 8 ++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/ConfigFactory.java b/config/src/main/java/com/typesafe/config/ConfigFactory.java index 18963b5cc..e0275effb 100644 --- a/config/src/main/java/com/typesafe/config/ConfigFactory.java +++ b/config/src/main/java/com/typesafe/config/ConfigFactory.java @@ -11,6 +11,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.Map; +import java.util.Optional; import java.util.Properties; import java.util.concurrent.Callable; @@ -1095,9 +1096,10 @@ public static Config parseResourcesAnySyntax(String resourceBasename) { * Parse only any application replacement (specified by one of config.{resource,file,url}), returning * an empty Config if no overrides were set. * @param parseOptions parse options - * @return the parsed configuration + * @return a {@link java.util.Optional} containing any specified replacement, or {@link Optional#empty()} + * if none was specified. */ - public static Config parseApplicationReplacement(ConfigParseOptions parseOptions) { + public static java.util.Optional parseApplicationReplacement(ConfigParseOptions parseOptions) { ClassLoader loader = parseOptions.getClassLoader(); if (loader == null) @@ -1120,7 +1122,7 @@ public static Config parseApplicationReplacement(ConfigParseOptions parseOptions specified += 1; if (specified == 0) { - return ConfigImpl.emptyConfig("TODO: what to put here? Should something else be returned?"); + return java.util.Optional.empty(); } else if (specified > 1) { throw new ConfigException.Generic("You set more than one of config.file='" + file + "', config.url='" + url + "', config.resource='" + resource @@ -1133,12 +1135,12 @@ public static Config parseApplicationReplacement(ConfigParseOptions parseOptions resource = resource.substring(1); // this deliberately does not parseResourcesAnySyntax; if // people want that they can use an include statement. - return ConfigFactory.parseResources(loader, resource, overrideOptions); + return java.util.Optional.of(ConfigFactory.parseResources(loader, resource, overrideOptions)); } else if (file != null) { - return ConfigFactory.parseFile(new File(file), overrideOptions); + return java.util.Optional.of(ConfigFactory.parseFile(new File(file), overrideOptions)); } else { try { - return ConfigFactory.parseURL(new URL(url), overrideOptions); + return java.util.Optional.of(ConfigFactory.parseURL(new URL(url), overrideOptions)); } catch (MalformedURLException e) { throw new ConfigException.Generic("Bad URL in config.url system property: '" + url + "': " + e.getMessage(), e); diff --git a/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java b/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java index dd621a2ac..317cddc5b 100644 --- a/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java +++ b/config/src/main/java/com/typesafe/config/DefaultConfigLoadingStrategy.java @@ -8,11 +8,7 @@ public class DefaultConfigLoadingStrategy implements ConfigLoadingStrategy { @Override public Config parseApplicationConfig(ConfigParseOptions parseOptions) { - Config applicationReplacement = ConfigFactory.parseApplicationReplacement(parseOptions); - if (applicationReplacement.isEmpty()) { - return ConfigFactory.parseResourcesAnySyntax("application", parseOptions); - } else { - return applicationReplacement; - } + return ConfigFactory.parseApplicationReplacement(parseOptions) + .orElseGet(() -> ConfigFactory.parseResourcesAnySyntax("application", parseOptions)); } }