This is an automated email from the ASF dual-hosted git repository.
lukaszlenart pushed a commit to branch WW-5016-uses-proper-format
in repository https://gitbox.apache.org/repos/asf/struts.git
The following commit(s) were added to refs/heads/WW-5016-uses-proper-format by
this push:
new 59932a5 WW-5016 Reduces calls to TextProvider
59932a5 is described below
commit 59932a51799890582859938a355546b75b5f6834
Author: Lukasz Lenart <[email protected]>
AuthorDate: Mon Feb 21 19:31:52 2022 +0100
WW-5016 Reduces calls to TextProvider
---
.../main/java/org/apache/struts2/components/Date.java | 19 ++++++++++---------
.../apache/struts2/components/date/DateFormatter.java | 11 +++++++++--
.../components/date/DateTimeFormatterAdapter.java | 10 +++-------
.../components/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 0560057..ad174fd 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.inject.Inject;
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 class Date extends ContextBean {
}
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 7ad276a..282daaa 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 @@ import java.time.temporal.TemporalAccessor;
/**
* 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);
}
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 64bb4b1..05767ab 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 @@ import java.util.Locale;
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 e9f29f0..38f3c52 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 @@ import java.util.Locale;
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);
}