Skip to content

Commit 75abf15

Browse files
hpoettkerfmbenhassine
authored andcommitted
Reduce formatter initializations in DefaultFieldSet
Resolves #1694
1 parent b6c7e4a commit 75abf15

File tree

2 files changed

+71
-28
lines changed

2 files changed

+71
-28
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/DefaultFieldSet.java

+49-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,17 +45,13 @@ public class DefaultFieldSet implements FieldSet {
4545

4646
private final static String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
4747

48-
private DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
48+
private DateFormat dateFormat;
4949

50-
{
51-
dateFormat.setLenient(false);
52-
}
50+
private NumberFormat numberFormat;
5351

54-
private NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
52+
private String grouping;
5553

56-
private String grouping = ",";
57-
58-
private String decimal = ".";
54+
private String decimal;
5955

6056
/**
6157
* The fields wrapped by this '<code>FieldSet</code>' instance.
@@ -77,6 +73,10 @@ public final void setNumberFormat(NumberFormat numberFormat) {
7773
}
7874
}
7975

76+
private static NumberFormat getDefaultNumberFormat() {
77+
return NumberFormat.getInstance(Locale.US);
78+
}
79+
8080
/**
8181
* The {@link DateFormat} to use for parsing dates. If unset the default pattern is
8282
* ISO standard <code>yyyy-MM-dd</code>.
@@ -86,25 +86,49 @@ public void setDateFormat(DateFormat dateFormat) {
8686
this.dateFormat = dateFormat;
8787
}
8888

89+
private static DateFormat getDefaultDateFormat() {
90+
DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
91+
dateFormat.setLenient(false);
92+
return dateFormat;
93+
}
94+
8995
/**
9096
* Create a FieldSet with anonymous tokens. They can only be retrieved by column
9197
* number.
9298
* @param tokens the token values
99+
* @param dateFormat the {@link DateFormat} to use
100+
* @param numberFormat the {@link NumberFormat} to use
93101
* @see FieldSet#readString(int)
102+
* @since 5.2
94103
*/
95-
public DefaultFieldSet(String[] tokens) {
104+
public DefaultFieldSet(String[] tokens, @Nullable DateFormat dateFormat, @Nullable NumberFormat numberFormat) {
96105
this.tokens = tokens == null ? null : tokens.clone();
97-
setNumberFormat(NumberFormat.getInstance(Locale.US));
106+
setDateFormat(dateFormat == null ? getDefaultDateFormat() : dateFormat);
107+
setNumberFormat(numberFormat == null ? getDefaultNumberFormat() : numberFormat);
108+
}
109+
110+
/**
111+
* Create a FieldSet with anonymous tokens. They can only be retrieved by column
112+
* number.
113+
* @param tokens the token values
114+
* @see FieldSet#readString(int)
115+
*/
116+
public DefaultFieldSet(String[] tokens) {
117+
this(tokens, null, null);
98118
}
99119

100120
/**
101121
* Create a FieldSet with named tokens. The token values can then be retrieved either
102122
* by name or by column number.
103123
* @param tokens the token values
104124
* @param names the names of the tokens
125+
* @param dateFormat the {@link DateFormat} to use
126+
* @param numberFormat the {@link NumberFormat} to use
105127
* @see FieldSet#readString(String)
128+
* @since 5.2
106129
*/
107-
public DefaultFieldSet(String[] tokens, String[] names) {
130+
public DefaultFieldSet(String[] tokens, String[] names, @Nullable DateFormat dateFormat,
131+
@Nullable NumberFormat numberFormat) {
108132
Assert.notNull(tokens, "Tokens must not be null");
109133
Assert.notNull(names, "Names must not be null");
110134
if (tokens.length != names.length) {
@@ -113,7 +137,19 @@ public DefaultFieldSet(String[] tokens, String[] names) {
113137
}
114138
this.tokens = tokens.clone();
115139
this.names = Arrays.asList(names);
116-
setNumberFormat(NumberFormat.getInstance(Locale.US));
140+
setDateFormat(dateFormat == null ? getDefaultDateFormat() : dateFormat);
141+
setNumberFormat(numberFormat == null ? getDefaultNumberFormat() : numberFormat);
142+
}
143+
144+
/**
145+
* Create a FieldSet with named tokens. The token values can then be retrieved either
146+
* by name or by column number.
147+
* @param tokens the token values
148+
* @param names the names of the tokens
149+
* @see FieldSet#readString(String)
150+
*/
151+
public DefaultFieldSet(String[] tokens, String[] names) {
152+
this(tokens, names, null, null);
117153
}
118154

119155
@Override

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/DefaultFieldSetFactory.java

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
1818
import java.text.DateFormat;
1919
import java.text.NumberFormat;
2020

21+
import org.springframework.lang.Nullable;
22+
2123
/**
2224
* Default implementation of {@link FieldSetFactory} with no special knowledge of the
2325
* {@link FieldSet} required. Returns a {@link DefaultFieldSet} from both factory methods.
@@ -32,6 +34,23 @@ public class DefaultFieldSetFactory implements FieldSetFactory {
3234

3335
private NumberFormat numberFormat;
3436

37+
/**
38+
* Default constructor.
39+
*/
40+
public DefaultFieldSetFactory() {
41+
}
42+
43+
/**
44+
* Convenience constructor
45+
* @param dateFormat the {@link DateFormat} to use for parsing dates
46+
* @param numberFormat the {@link NumberFormat} to use for parsing numbers
47+
* @since 5.2
48+
*/
49+
public DefaultFieldSetFactory(@Nullable DateFormat dateFormat, @Nullable NumberFormat numberFormat) {
50+
this.dateFormat = dateFormat;
51+
this.numberFormat = numberFormat;
52+
}
53+
3554
/**
3655
* The {@link NumberFormat} to use for parsing numbers. If unset then
3756
* {@link java.util.Locale#US} will be used.
@@ -55,27 +74,15 @@ public void setDateFormat(DateFormat dateFormat) {
5574
*/
5675
@Override
5776
public FieldSet create(String[] values, String[] names) {
58-
DefaultFieldSet fieldSet = new DefaultFieldSet(values, names);
59-
return enhance(fieldSet);
77+
return new DefaultFieldSet(values, names, dateFormat, numberFormat);
6078
}
6179

6280
/**
6381
* {@inheritDoc}
6482
*/
6583
@Override
6684
public FieldSet create(String[] values) {
67-
DefaultFieldSet fieldSet = new DefaultFieldSet(values);
68-
return enhance(fieldSet);
69-
}
70-
71-
private FieldSet enhance(DefaultFieldSet fieldSet) {
72-
if (dateFormat != null) {
73-
fieldSet.setDateFormat(dateFormat);
74-
}
75-
if (numberFormat != null) {
76-
fieldSet.setNumberFormat(numberFormat);
77-
}
78-
return fieldSet;
85+
return new DefaultFieldSet(values, dateFormat, numberFormat);
7986
}
8087

8188
}

0 commit comments

Comments
 (0)