BATCH-1262: number format for BigDecimal
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.batch.item.file.transform;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DateFormat;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -48,6 +49,8 @@ public class DefaultFieldSet implements FieldSet {
|
||||
}
|
||||
|
||||
private NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
|
||||
private String grouping = ",";
|
||||
private String decimal = ".";
|
||||
|
||||
/**
|
||||
* The fields wrapped by this '<code>FieldSet</code>' instance.
|
||||
@@ -63,6 +66,10 @@ public class DefaultFieldSet implements FieldSet {
|
||||
*/
|
||||
public void setNumberFormat(NumberFormat numberFormat) {
|
||||
this.numberFormat = numberFormat;
|
||||
if (numberFormat instanceof DecimalFormat) {
|
||||
grouping = ""+((DecimalFormat)numberFormat).getDecimalFormatSymbols().getGroupingSeparator();
|
||||
decimal = ""+((DecimalFormat)numberFormat).getDecimalFormatSymbols().getDecimalSeparator();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,6 +83,7 @@ public class DefaultFieldSet implements FieldSet {
|
||||
|
||||
public DefaultFieldSet(String[] tokens) {
|
||||
this.tokens = tokens == null ? null : (String[]) tokens.clone();
|
||||
setNumberFormat(NumberFormat.getInstance(Locale.US));
|
||||
}
|
||||
|
||||
public DefaultFieldSet(String[] tokens, String[] names) {
|
||||
@@ -87,6 +95,7 @@ public class DefaultFieldSet implements FieldSet {
|
||||
}
|
||||
this.tokens = (String[]) tokens.clone();
|
||||
this.names = Arrays.asList(names);
|
||||
setNumberFormat(NumberFormat.getInstance(Locale.US));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -429,15 +438,24 @@ public class DefaultFieldSet implements FieldSet {
|
||||
*/
|
||||
public BigDecimal readBigDecimal(int index, BigDecimal defaultValue) {
|
||||
String candidate = readAndTrim(index);
|
||||
|
||||
if (!StringUtils.hasText(candidate)) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
try {
|
||||
return (StringUtils.hasText(candidate)) ? new BigDecimal(candidate) : defaultValue;
|
||||
String result = removeSeparators(candidate);
|
||||
return new BigDecimal(result);
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
throw new NumberFormatException("Unparseable number: " + candidate);
|
||||
}
|
||||
}
|
||||
|
||||
private String removeSeparators(String candidate) {
|
||||
return candidate.replace(grouping, "").replace(decimal, ".");
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
|
||||
@@ -42,7 +42,7 @@ public class DefaultFieldSetTests {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
tokens = new String[] { "TestString", "true", "C", "10", "-472", "354224", "543", "124.3", "424.3", "324",
|
||||
tokens = new String[] { "TestString", "true", "C", "10", "-472", "354224", "543", "124.3", "424.3", "1,3245",
|
||||
null, "2007-10-12", "12-10-2007", "" };
|
||||
names = new String[] { "String", "Boolean", "Char", "Byte", "Short", "Integer", "Long", "Float", "Double",
|
||||
"BigDecimal", "Null", "Date", "DatePattern", "BlankInput" };
|
||||
@@ -146,9 +146,36 @@ public class DefaultFieldSetTests {
|
||||
@Test
|
||||
public void testReadBigDecimal() throws Exception {
|
||||
|
||||
BigDecimal bd = new BigDecimal(324);
|
||||
assertEquals(fieldSet.readBigDecimal(9), bd);
|
||||
assertEquals(fieldSet.readBigDecimal("BigDecimal"), bd);
|
||||
BigDecimal bd = new BigDecimal("424.3");
|
||||
assertEquals(bd, fieldSet.readBigDecimal(8));
|
||||
assertEquals(bd, fieldSet.readBigDecimal("Double"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadBigBigDecimal() throws Exception {
|
||||
|
||||
fieldSet = new DefaultFieldSet(new String[] {"12345678901234567890"});
|
||||
BigDecimal bd = new BigDecimal("12345678901234567890");
|
||||
assertEquals(bd, fieldSet.readBigDecimal(0));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadBigDecimalWithFormat() throws Exception {
|
||||
|
||||
fieldSet.setNumberFormat(NumberFormat.getInstance(Locale.US));
|
||||
BigDecimal bd = new BigDecimal("424.3");
|
||||
assertEquals(bd, fieldSet.readBigDecimal(8));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadBigDecimalWithEuroFormat() throws Exception {
|
||||
|
||||
fieldSet.setNumberFormat(NumberFormat.getInstance(Locale.GERMANY));
|
||||
BigDecimal bd = new BigDecimal("1.3245");
|
||||
assertEquals(bd, fieldSet.readBigDecimal(9));
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user