From 59932a51799890582859938a355546b75b5f6834 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Mon, 21 Feb 2022 19:31:52 +0100 Subject: [PATCH] WW-5016 Reduces calls to TextProvider --- .../org/apache/struts2/components/Date.java | 19 ++++++++++--------- .../components/date/DateFormatter.java | 11 +++++++++-- .../date/DateTimeFormatterAdapter.java | 10 +++------- .../date/SimpleDateFormatAdapter.java | 8 ++------ 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/components/Date.java b/core/src/main/java/org/apache/struts2/components/Date.java index 0560057b83..ad174fd111 100644 --- a/core/src/main/java/org/apache/struts2/components/Date.java +++ b/core/src/main/java/org/apache/struts2/components/Date.java @@ -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; @@ -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() { diff --git a/core/src/main/java/org/apache/struts2/components/date/DateFormatter.java b/core/src/main/java/org/apache/struts2/components/date/DateFormatter.java index 7ad276a257..282daaa061 100644 --- a/core/src/main/java/org/apache/struts2/components/date/DateFormatter.java +++ b/core/src/main/java/org/apache/struts2/components/date/DateFormatter.java @@ -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 - * + *

* 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); } diff --git a/core/src/main/java/org/apache/struts2/components/date/DateTimeFormatterAdapter.java b/core/src/main/java/org/apache/struts2/components/date/DateTimeFormatterAdapter.java index 64bb4b1659..05767ab1b3 100644 --- a/core/src/main/java/org/apache/struts2/components/date/DateTimeFormatterAdapter.java +++ b/core/src/main/java/org/apache/struts2/components/date/DateTimeFormatterAdapter.java @@ -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); } diff --git a/core/src/main/java/org/apache/struts2/components/date/SimpleDateFormatAdapter.java b/core/src/main/java/org/apache/struts2/components/date/SimpleDateFormatAdapter.java index e9f29f0be7..38f3c52969 100644 --- a/core/src/main/java/org/apache/struts2/components/date/SimpleDateFormatAdapter.java +++ b/core/src/main/java/org/apache/struts2/components/date/SimpleDateFormatAdapter.java @@ -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); }