Skip to content

Commit 6ef5162

Browse files
committed
fix(api): fix decimal numbers not being correctly parsed (#142)
Resolves: #142
1 parent 6c779af commit 6ef5162

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

connect-file-pulse-api/src/main/java/io/streamthoughts/kafka/connect/filepulse/data/internal/TypeConverter.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Collection;
2929
import java.util.Date;
3030
import java.util.Objects;
31+
import java.util.Optional;
3132

3233
/**
3334
* Class which can be used to convert an object to a specific type.
@@ -115,8 +116,12 @@ public static Long getLong(final Object value) throws IllegalArgumentException {
115116
public static Float getFloat(final Object value) throws IllegalArgumentException {
116117
Objects.requireNonNull(value, "value can't be null");
117118

118-
if (value instanceof String && isNumber((String) value)) {
119-
return new BigDecimal(value.toString()).floatValue();
119+
if (value instanceof String) {
120+
return getBigDecimal(value)
121+
.map(BigDecimal::floatValue)
122+
.orElseThrow(() ->
123+
new DataException(String.format("Cannot parse 64-bits double content from \"%s\"", value))
124+
);
120125
}
121126
if (value instanceof Number) {
122127
Number number = (Number) value;
@@ -125,11 +130,23 @@ public static Float getFloat(final Object value) throws IllegalArgumentException
125130
throw new DataException(String.format("Cannot parse 32-bits float content from \"%s\"", value));
126131
}
127132

133+
private static Optional<BigDecimal> getBigDecimal(final Object value) {
134+
try {
135+
return Optional.of(new BigDecimal(value.toString().replace(",", ".")));
136+
} catch (NumberFormatException e) {
137+
return Optional.empty();
138+
}
139+
}
140+
128141
public static Double getDouble(final Object value) throws IllegalArgumentException {
129142
Objects.requireNonNull(value, "value can't be null");
130143

131-
if (value instanceof String && isNumber((String) value)) {
132-
return new BigDecimal(value.toString()).doubleValue();
144+
if (value instanceof String) {
145+
return getBigDecimal(value)
146+
.map(BigDecimal::doubleValue)
147+
.orElseThrow(() ->
148+
new DataException(String.format("Cannot parse 64-bits double content from \"%s\"", value))
149+
);
133150
}
134151
if (value instanceof Number) {
135152
Number number = (Number) value;
@@ -202,12 +219,10 @@ public static BigDecimal getDecimal(final Object value) {
202219
return null;
203220
}
204221

205-
try {
206-
return new BigDecimal(result.replace(",", "."));
207-
} catch (NumberFormatException e) {
208-
throw new DataException(
209-
String.format("Cannot parse decimal content from \"%s\"", value));
210-
}
222+
return getBigDecimal(value)
223+
.orElseThrow(() ->
224+
new DataException(String.format("Cannot parse decimal content from \"%s\"", value))
225+
);
211226
}
212227

213228
private static boolean isNumber(final String s) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.streamthoughts.kafka.connect.filepulse.data.internal;
2+
3+
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import java.math.BigDecimal;
8+
9+
public class TypeConverterTest {
10+
11+
@Test
12+
public void should_convert_decimal_number() {
13+
final BigDecimal result = TypeConverter.getDecimal("1.10");
14+
Assert.assertEquals(1.10, result.doubleValue(), 0.1);
15+
}
16+
17+
@Test
18+
public void should_convert_decimal_number_given_comma() {
19+
final BigDecimal result = TypeConverter.getDecimal("1,101");
20+
Assert.assertEquals(1.101, result.doubleValue(), 0);
21+
}
22+
23+
@Test
24+
public void should_convert_double_number() {
25+
final Double result = TypeConverter.getDouble("1.101");
26+
Assert.assertEquals(1.101, result.doubleValue(), 0);
27+
}
28+
29+
@Test
30+
public void should_convert_double_number_given_comma() {
31+
final Double result = TypeConverter.getDouble("1,101");
32+
Assert.assertEquals(1.101, result.doubleValue(), 0);
33+
}
34+
35+
@Test
36+
public void should_convert_float_number() {
37+
final Float result = TypeConverter.getFloat("1.101");
38+
Assert.assertEquals(1.101f, result.floatValue(), 0);
39+
}
40+
41+
42+
}

0 commit comments

Comments
 (0)