diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/DefaultFieldSet.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/DefaultFieldSet.java index 1a5088621..baa8ccce6 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/DefaultFieldSet.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/DefaultFieldSet.java @@ -481,6 +481,17 @@ public class DefaultFieldSet implements FieldSet { return parseDate(readAndTrim(index), dateFormat); } + /* (non-Javadoc) + * @see org.springframework.batch.item.file.transform.FieldSet#readDate(int, java.util.Date) + */ + public Date readDate(int index, Date defaultValue) { + try { + return readDate(index); + } catch (IllegalArgumentException e) { + return defaultValue; + } + } + /* * (non-Javadoc) * @@ -497,6 +508,17 @@ public class DefaultFieldSet implements FieldSet { } } + /* (non-Javadoc) + * @see org.springframework.batch.item.file.transform.FieldSet#readDate(int, java.util.Date) + */ + public Date readDate(String name, Date defaultValue) { + try { + return readDate(name); + } catch (IllegalArgumentException e) { + return defaultValue; + } + } + /* * (non-Javadoc) * @@ -509,6 +531,20 @@ public class DefaultFieldSet implements FieldSet { return parseDate(readAndTrim(index), sdf); } + /* + * (non-Javadoc) + * + * @see org.springframework.batch.item.file.mapping.IFieldSet#readDate(int, + * java.lang.String) + */ + public Date readDate(int index, String pattern, Date defaultValue) { + try { + return readDate(index, pattern); + } catch (IllegalArgumentException e) { + return defaultValue; + } + } + /* * (non-Javadoc) * @@ -525,6 +561,20 @@ public class DefaultFieldSet implements FieldSet { } } + /* + * (non-Javadoc) + * + * @see org.springframework.batch.item.file.mapping.IFieldSet#readDate(int, + * java.lang.String) + */ + public Date readDate(String name, String pattern, Date defaultValue) { + try { + return readDate(name, pattern); + } catch (IllegalArgumentException e) { + return defaultValue; + } + } + /* * (non-Javadoc) * diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FieldSet.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FieldSet.java index 061958179..1501e00ac 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FieldSet.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FieldSet.java @@ -316,6 +316,7 @@ public interface FieldSet { * value at index 'index' is blank. * * @param name the field name. + * @param defaultValue the default value to use if the field is blank * @throws IllegalArgumentException if a column with given name is not * defined. */ @@ -340,6 +341,27 @@ public interface FieldSet { */ Date readDate(String name); + /** + * Read the java.util.Date value in default format at + * designated column index. + * + * @param index the field index. + * @param defaultValue the default value to use if the field is blank + * @throws IndexOutOfBoundsException if the index is out of bounds. + */ + Date readDate(int index, Date defaultValue); + + /** + * Read the java.sql.Date value in given format from column + * with given name. + * + * @param name the field name. + * @param defaultValue the default value to use if the field is blank + * @throws IllegalArgumentException if a column with given name is not + * defined. + */ + Date readDate(String name, Date defaultValue); + /** * Read the java.util.Date value in default format at * designated column index. @@ -364,6 +386,32 @@ public interface FieldSet { */ Date readDate(String name, String pattern); + /** + * Read the java.util.Date value in default format at + * designated column index. + * + * @param index the field index. + * @param pattern the pattern describing the date and time format + * @param defaultValue the default value to use if the field is blank + * @throws IndexOutOfBoundsException if the index is out of bounds. + * @throws IllegalArgumentException if the date cannot be parsed. + * + */ + Date readDate(int index, String pattern, Date defaultValue); + + /** + * Read the java.sql.Date value in given format from column + * with given name. + * + * @param name the field name. + * @param pattern the pattern describing the date and time format + * @param defaultValue the default value to use if the field is blank + * @throws IllegalArgumentException if a column with given name is not + * defined or if the specified field cannot be parsed + * + */ + Date readDate(String name, String pattern, Date defaultValue); + /** * Return the number of fields in this 'FieldSet'. */ diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/DefaultFieldSetTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/DefaultFieldSetTests.java index 0e6d10854..2ed6ca9ec 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/DefaultFieldSetTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/DefaultFieldSetTests.java @@ -26,6 +26,7 @@ import java.math.BigDecimal; import java.text.NumberFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Date; import java.util.Locale; import org.junit.Before; @@ -369,6 +370,13 @@ public class DefaultFieldSetTests { assertNotNull(fieldSet.readDate("Date")); } + @Test + public void testReadDateWithDefault() throws Exception { + Date date = null; + assertEquals(date, fieldSet.readDate(13, date)); + assertEquals(date, fieldSet.readDate("BlankInput", date)); + } + @Test public void testReadDateWithFormat() throws Exception { fieldSet = new DefaultFieldSet(new String[] {"13/01/1999"}); @@ -415,6 +423,13 @@ public class DefaultFieldSetTests { } } + @Test + public void testReadDateWithPatternAndDefault() throws Exception { + Date date = null; + assertEquals(date, fieldSet.readDate(13, "dd-MM-yyyy", date)); + assertEquals(date, fieldSet.readDate("BlankInput", "dd-MM-yyyy", date)); + } + @Test public void testStrictReadDateWithPattern() throws Exception {