Skip to content

Commit 58944b4

Browse files
committed
use optional instead of providing default for int
1 parent bd4286c commit 58944b4

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/ConfigPropertiesUtil.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ public static Optional<Boolean> getBoolean(OpenTelemetry openTelemetry, String..
6767
* Returns the int value of the given property name from system properties and environment
6868
* variables.
6969
*/
70-
public static int getInt(String propertyName, int defaultValue) {
71-
String strValue = getString(propertyName);
72-
if (strValue == null) {
73-
return defaultValue;
74-
}
75-
try {
76-
return Integer.parseInt(strValue);
77-
} catch (NumberFormatException ignored) {
78-
return defaultValue;
79-
}
70+
public static Optional<Integer> getInt(String propertyName) {
71+
return Optional.ofNullable(getString(propertyName))
72+
.flatMap(
73+
s -> {
74+
try {
75+
return Optional.of(Integer.parseInt(s));
76+
} catch (NumberFormatException ignored) {
77+
return Optional.empty();
78+
}
79+
});
8080
}
8181

8282
/**

instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/internal/ConfigPropertiesUtilTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,24 @@ void getString_declarativeConfig(Object property, String expected) {
9393
@SetSystemProperty(key = "test.property.int", value = "42")
9494
@Test
9595
void getInt_systemProperty() {
96-
assertThat(ConfigPropertiesUtil.getInt("test.property.int", -1)).isEqualTo(42);
96+
assertThat(ConfigPropertiesUtil.getInt("test.property.int")).hasValue(42);
9797
}
9898

9999
@SetEnvironmentVariable(key = "TEST_PROPERTY_INT", value = "12")
100100
@Test
101101
void getInt_environmentVariable() {
102-
assertThat(ConfigPropertiesUtil.getInt("test.property.int", -1)).isEqualTo(12);
102+
assertThat(ConfigPropertiesUtil.getInt("test.property.int")).hasValue(12);
103103
}
104104

105105
@Test
106106
void getInt_none() {
107-
assertThat(ConfigPropertiesUtil.getInt("test.property.int", -1)).isEqualTo(-1);
107+
assertThat(ConfigPropertiesUtil.getInt("test.property.int")).isEmpty();
108108
}
109109

110110
@SetSystemProperty(key = "test.property.int", value = "not a number")
111111
@Test
112112
void getInt_invalidNumber() {
113-
assertThat(ConfigPropertiesUtil.getInt("test.property.int", -1)).isEqualTo(-1);
113+
assertThat(ConfigPropertiesUtil.getInt("test.property.int")).isEmpty();
114114
}
115115

116116
@SetEnvironmentVariable(key = "OTEL_INSTRUMENTATION_TEST_PROPERTY_BOOLEAN", value = "false")

javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/config/EarlyInitAgentConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public int getInt(String propertyName, int defaultValue) {
4646
String configFileValueStr = configFileContents.get(propertyName);
4747
int configFileValue =
4848
configFileValueStr == null ? defaultValue : Integer.parseInt(configFileValueStr);
49-
return ConfigPropertiesUtil.getInt(propertyName, configFileValue);
49+
return ConfigPropertiesUtil.getInt(propertyName).orElse(configFileValue);
5050
} catch (NumberFormatException ignored) {
5151
return defaultValue;
5252
}

0 commit comments

Comments
 (0)