Skip to content

Commit

Permalink
WW-5016 Reduces calls to TextProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Feb 21, 2022
1 parent 4746a49 commit 59932a5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
19 changes: 10 additions & 9 deletions core/src/main/java/org/apache/struts2/components/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.components.date.DateFormatter;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;
Expand Down Expand Up @@ -355,15 +354,17 @@ public boolean end(Writer writer, String body) {
}

private String formatDate(TextProvider textProvider, ZonedDateTime date) {
// if the format is not specified, fall back using the defined property DATETAG_PROPERTY
String globalFormat = textProvider.getText(Date.DATETAG_PROPERTY);
if (DATETAG_PROPERTY.equals(globalFormat)) {
// if tp.getText can not find the property then the
// returned string is the same as input = DATETAG_PROPERTY
globalFormat = null;
String useFormat = format;
if (useFormat == null) {
// if the format is not specified, fall back using the defined property DATETAG_PROPERTY
useFormat = textProvider.getText(DATETAG_PROPERTY);
if (DATETAG_PROPERTY.equals(useFormat)) {
// if tp.getText can not find the property then the
// returned string is the same as input = DATETAG_PROPERTY
useFormat = null;
}
}

return dateFormatter.format(date, format, globalFormat);
return dateFormatter.format(date, useFormat);
}

private ZoneId getTimeZone() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@
/**
* Allows defines a wrapper around different formatting APIs, like old SimpleDateFormat
* and new DateTimeFormatter introduced in Java 8 Date/Time API
*
* <p>
* New instance will be injected using {@link org.apache.struts2.StrutsConstants#STRUTS_DATE_FORMATTER}
*/
public interface DateFormatter {

String format(TemporalAccessor temporal, String format, String defaultFormat);
/**
* Formats provided temporal with the given format
*
* @param temporal Java 8 {@link TemporalAccessor}
* @param format implementation specific format
* @return a string representation of the formatted `temporal`
*/
String format(TemporalAccessor temporal, String format);

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@
public class DateTimeFormatterAdapter implements DateFormatter {

@Override
public String format(TemporalAccessor temporal, String format, String defaultFormat) {
public String format(TemporalAccessor temporal, String format) {
DateTimeFormatter dtf;
Locale locale = ActionContext.getContext().getLocale();
if (format == null) {
if (defaultFormat != null) {
dtf = DateTimeFormatter.ofPattern(defaultFormat, locale);
} else {
dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
.withLocale(locale);
}
dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
.withLocale(locale);
} else {
dtf = DateTimeFormatter.ofPattern(format, locale);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@
public class SimpleDateFormatAdapter implements DateFormatter {

@Override
public String format(TemporalAccessor temporal, String format, String defaultFormat) {
public String format(TemporalAccessor temporal, String format) {
DateFormat df;
Locale locale = ActionContext.getContext().getLocale();
if (format == null) {
if (defaultFormat != null) {
df = new SimpleDateFormat(defaultFormat, locale);
} else {
df = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM, locale);
}
df = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM, locale);
} else {
df = new SimpleDateFormat(format, locale);
}
Expand Down

0 comments on commit 59932a5

Please sign in to comment.