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 2f70c9c8f..2bef134a8 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 @@ -49,7 +49,9 @@ public class DefaultFieldSet implements FieldSet { } private NumberFormat numberFormat = NumberFormat.getInstance(Locale.US); + private String grouping = ","; + private String decimal = "."; /** @@ -60,15 +62,15 @@ public class DefaultFieldSet implements FieldSet { private List names; /** - * The {@link NumberFormat} to use for parsing numbers. If unset the - * US locale will be used ('.' as decimal place). + * The {@link NumberFormat} to use for parsing numbers. If unset the US + * locale will be used ('.' as decimal place). * @param numberFormat the {@link NumberFormat} to use for number parsing */ public final void setNumberFormat(NumberFormat numberFormat) { this.numberFormat = numberFormat; if (numberFormat instanceof DecimalFormat) { - grouping = ""+((DecimalFormat)numberFormat).getDecimalFormatSymbols().getGroupingSeparator(); - decimal = ""+((DecimalFormat)numberFormat).getDecimalFormatSymbols().getDecimalSeparator(); + grouping = "" + ((DecimalFormat) numberFormat).getDecimalFormatSymbols().getGroupingSeparator(); + decimal = "" + ((DecimalFormat) numberFormat).getDecimalFormatSymbols().getDecimalSeparator(); } } @@ -81,11 +83,24 @@ public class DefaultFieldSet implements FieldSet { this.dateFormat = dateFormat; } + /** + * Create a FieldSet with anonymous tokens. They can only be retrieved by + * column number. + * @param tokens the token values + * @see FieldSet#readString(int) + */ public DefaultFieldSet(String[] tokens) { this.tokens = tokens == null ? null : (String[]) tokens.clone(); setNumberFormat(NumberFormat.getInstance(Locale.US)); } + /** + * Create a FieldSet with named tokens. The token values can then be + * retrieved either by name or by column number. + * @param tokens the token values + * @param names the names of the tokens + * @see FieldSet#readString(String) + */ public DefaultFieldSet(String[] tokens, String[] names) { Assert.notNull(tokens); Assert.notNull(names); @@ -438,7 +453,7 @@ public class DefaultFieldSet implements FieldSet { */ public BigDecimal readBigDecimal(int index, BigDecimal defaultValue) { String candidate = readAndTrim(index); - + if (!StringUtils.hasText(candidate)) { return defaultValue; } @@ -484,13 +499,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) + /* + * (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) { + } + catch (IllegalArgumentException e) { return defaultValue; } } @@ -511,13 +530,17 @@ public class DefaultFieldSet implements FieldSet { } } - /* (non-Javadoc) - * @see org.springframework.batch.item.file.transform.FieldSet#readDate(int, java.util.Date) + /* + * (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) { + } + catch (IllegalArgumentException e) { return defaultValue; } } @@ -543,7 +566,8 @@ public class DefaultFieldSet implements FieldSet { public Date readDate(int index, String pattern, Date defaultValue) { try { return readDate(index, pattern); - } catch (IllegalArgumentException e) { + } + catch (IllegalArgumentException e) { return defaultValue; } } @@ -573,7 +597,8 @@ public class DefaultFieldSet implements FieldSet { public Date readDate(String name, String pattern, Date defaultValue) { try { return readDate(name, pattern); - } catch (IllegalArgumentException e) { + } + catch (IllegalArgumentException e) { return defaultValue; } } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FieldSetFactory.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FieldSetFactory.java index c547cff67..27f6c318a 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FieldSetFactory.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FieldSetFactory.java @@ -11,8 +11,21 @@ package org.springframework.batch.item.file.transform; */ public interface FieldSetFactory { - FieldSet create(String[] names, String[] values); + /** + * Create a FieldSet with named tokens. The token values can then be + * retrieved either by name or by column number. + * @param values the token values + * @param names the names of the tokens + * @see DefaultFieldSet#readString(String) + */ + FieldSet create(String[] values, String[] names); + /** + * Create a FieldSet with anonymous tokens. They can only be retrieved by + * column number. + * @param values the token values + * @see FieldSet#readString(int) + */ FieldSet create(String[] values); } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/util/FileUtils.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/util/FileUtils.java index 5be6d2923..8b5df8970 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/util/FileUtils.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/util/FileUtils.java @@ -95,7 +95,7 @@ public final class FileUtils { } try { - return file.createNewFile(); + return file.createNewFile() && file.exists(); } catch (IOException e) { // On some filesystems you can get an exception here even though the diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/util/FileUtilsTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/util/FileUtilsTests.java index b22c30d78..0eaed6f11 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/util/FileUtilsTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/util/FileUtilsTests.java @@ -141,7 +141,7 @@ public class FileUtilsTests { }; try{ FileUtils.setUpOutputFile(file, false, false); - fail(); + fail("Expected IOException because file doesn't exist"); }catch(ItemStreamException ex){ String message = ex.getMessage(); assertTrue("Wrong message: "+message, message.startsWith("Output file was not created"));