Skip to content

Commit

Permalink
Reduce formatter initializations in DefaultFieldSet
Browse files Browse the repository at this point in the history
Resolves #1694
  • Loading branch information
hpoettker authored and fmbenhassine committed May 7, 2024
1 parent b6c7e4a commit 75abf15
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,17 +45,13 @@ public class DefaultFieldSet implements FieldSet {

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

private DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
private DateFormat dateFormat;

{
dateFormat.setLenient(false);
}
private NumberFormat numberFormat;

private NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
private String grouping;

private String grouping = ",";

private String decimal = ".";
private String decimal;

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

private static NumberFormat getDefaultNumberFormat() {
return NumberFormat.getInstance(Locale.US);
}

/**
* The {@link DateFormat} to use for parsing dates. If unset the default pattern is
* ISO standard <code>yyyy-MM-dd</code>.
Expand All @@ -86,25 +86,49 @@ public void setDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat;
}

private static DateFormat getDefaultDateFormat() {
DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
dateFormat.setLenient(false);
return dateFormat;
}

/**
* Create a FieldSet with anonymous tokens. They can only be retrieved by column
* number.
* @param tokens the token values
* @param dateFormat the {@link DateFormat} to use
* @param numberFormat the {@link NumberFormat} to use
* @see FieldSet#readString(int)
* @since 5.2
*/
public DefaultFieldSet(String[] tokens) {
public DefaultFieldSet(String[] tokens, @Nullable DateFormat dateFormat, @Nullable NumberFormat numberFormat) {
this.tokens = tokens == null ? null : tokens.clone();
setNumberFormat(NumberFormat.getInstance(Locale.US));
setDateFormat(dateFormat == null ? getDefaultDateFormat() : dateFormat);
setNumberFormat(numberFormat == null ? getDefaultNumberFormat() : numberFormat);
}

/**
* 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, null, null);
}

/**
* 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
* @param dateFormat the {@link DateFormat} to use
* @param numberFormat the {@link NumberFormat} to use
* @see FieldSet#readString(String)
* @since 5.2
*/
public DefaultFieldSet(String[] tokens, String[] names) {
public DefaultFieldSet(String[] tokens, String[] names, @Nullable DateFormat dateFormat,
@Nullable NumberFormat numberFormat) {
Assert.notNull(tokens, "Tokens must not be null");
Assert.notNull(names, "Names must not be null");
if (tokens.length != names.length) {
Expand All @@ -113,7 +137,19 @@ public DefaultFieldSet(String[] tokens, String[] names) {
}
this.tokens = tokens.clone();
this.names = Arrays.asList(names);
setNumberFormat(NumberFormat.getInstance(Locale.US));
setDateFormat(dateFormat == null ? getDefaultDateFormat() : dateFormat);
setNumberFormat(numberFormat == null ? getDefaultNumberFormat() : numberFormat);
}

/**
* 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) {
this(tokens, names, null, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,8 @@
import java.text.DateFormat;
import java.text.NumberFormat;

import org.springframework.lang.Nullable;

/**
* Default implementation of {@link FieldSetFactory} with no special knowledge of the
* {@link FieldSet} required. Returns a {@link DefaultFieldSet} from both factory methods.
Expand All @@ -32,6 +34,23 @@ public class DefaultFieldSetFactory implements FieldSetFactory {

private NumberFormat numberFormat;

/**
* Default constructor.
*/
public DefaultFieldSetFactory() {
}

/**
* Convenience constructor
* @param dateFormat the {@link DateFormat} to use for parsing dates
* @param numberFormat the {@link NumberFormat} to use for parsing numbers
* @since 5.2
*/
public DefaultFieldSetFactory(@Nullable DateFormat dateFormat, @Nullable NumberFormat numberFormat) {
this.dateFormat = dateFormat;
this.numberFormat = numberFormat;
}

/**
* The {@link NumberFormat} to use for parsing numbers. If unset then
* {@link java.util.Locale#US} will be used.
Expand All @@ -55,27 +74,15 @@ public void setDateFormat(DateFormat dateFormat) {
*/
@Override
public FieldSet create(String[] values, String[] names) {
DefaultFieldSet fieldSet = new DefaultFieldSet(values, names);
return enhance(fieldSet);
return new DefaultFieldSet(values, names, dateFormat, numberFormat);
}

/**
* {@inheritDoc}
*/
@Override
public FieldSet create(String[] values) {
DefaultFieldSet fieldSet = new DefaultFieldSet(values);
return enhance(fieldSet);
}

private FieldSet enhance(DefaultFieldSet fieldSet) {
if (dateFormat != null) {
fieldSet.setDateFormat(dateFormat);
}
if (numberFormat != null) {
fieldSet.setNumberFormat(numberFormat);
}
return fieldSet;
return new DefaultFieldSet(values, dateFormat, numberFormat);
}

}

0 comments on commit 75abf15

Please sign in to comment.