Skip to content

Commit

Permalink
Extended exception handling of getters for int, long and double.
Browse files Browse the repository at this point in the history
Signed-off-by: Juergen Fickel <juergen.fickel@bosch-si.com>
  • Loading branch information
Juergen Fickel committed Feb 25, 2019
1 parent 116bd17 commit f2ca9b7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
Expand Up @@ -35,6 +35,7 @@
import com.typesafe.config.ConfigOrigin;
import com.typesafe.config.ConfigResolveOptions;
import com.typesafe.config.ConfigValue;
import com.typesafe.config.ConfigValueType;

/**
* This class is the default implementation of {@link org.eclipse.ditto.services.utils.config.ScopedConfig}.
Expand Down Expand Up @@ -227,33 +228,69 @@ public Number getNumber(final String path) {
@Override
public int getInt(final String path) {
try {
return config.getInt(path);
} catch (final ConfigException.Missing | ConfigException.WrongType e) {
return tryToGetIntValue(path);
} catch(final ConfigException.Missing | ConfigException.WrongType | NumberFormatException e) {
final String msgPattern = "Failed to get int value for path <{0}>!";
throw new DittoConfigError(MessageFormat.format(msgPattern, appendToConfigPath(path)), e);
}
}

private int tryToGetIntValue(final String path) {
try {
return config.getInt(path);
} catch (final ConfigException.WrongType e) {
final ConfigValue configValue = config.getValue(path);
if (ConfigValueType.STRING == configValue.valueType()) {
return Integer.parseInt(String.valueOf(configValue.unwrapped()));
}
throw e;
}
}

@Override
public long getLong(final String path) {
try {
return config.getLong(path);
} catch (final ConfigException.Missing | ConfigException.WrongType e) {
return tryToGetLongValue(path);
} catch (final ConfigException.Missing | ConfigException.WrongType | NumberFormatException e) {
final String msgPattern = "Failed to get long value for path <{0}>!";
throw new DittoConfigError(MessageFormat.format(msgPattern, appendToConfigPath(path)), e);
}
}

private long tryToGetLongValue(final String path) {
try {
return config.getLong(path);
} catch (final ConfigException.WrongType e) {
final ConfigValue configValue = config.getValue(path);
if (ConfigValueType.STRING == configValue.valueType()) {
return Long.parseLong(String.valueOf(configValue.unwrapped()));
}
throw e;
}
}

@Override
public double getDouble(final String path) {
try {
return config.getDouble(path);
} catch (final ConfigException.Missing | ConfigException.WrongType e) {
return tryToGetDoubleValue(path);
} catch (final ConfigException.Missing | ConfigException.WrongType | NumberFormatException e) {
final String msgPattern = "Failed to get double value for path <{0}>!";
throw new DittoConfigError(MessageFormat.format(msgPattern, appendToConfigPath(path)), e);
}
}

private double tryToGetDoubleValue(final String path) {
try {
return config.getDouble(path);
} catch (final ConfigException.WrongType e) {
final ConfigValue configValue = config.getValue(path);
if (ConfigValueType.STRING == configValue.valueType()) {
return Double.parseDouble(String.valueOf(configValue.unwrapped()));
}
throw e;
}
}

@Override
public String getString(final String path) {
try {
Expand Down
Expand Up @@ -114,4 +114,17 @@ public void tryToGetMissingValue() {
.withCauseInstanceOf(ConfigException.Missing.class);
}

@Test
public void getIntValueAlthoughConfigContainsString() {
final String parentPath = "ditto";
final String valuePath = "intValueAsString";
final int intValue = 23;
final Config config = ConfigFactory.parseMap(
Collections.singletonMap(parentPath + "." + valuePath, String.valueOf(intValue)));

final DefaultScopedConfig underTest = DefaultScopedConfig.newInstance(config, parentPath);

assertThat(underTest.getInt(valuePath)).as(valuePath).isEqualTo(intValue);
}

}

0 comments on commit f2ca9b7

Please sign in to comment.