You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/03/19 14:57:57 UTC
(commons-configuration) branch master updated: Refactor x6 code pattern
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-configuration.git
The following commit(s) were added to refs/heads/master by this push:
new 9c21b67a Refactor x6 code pattern
9c21b67a is described below
commit 9c21b67a6cdaa5ab8ea5530a7a666673c7ec93ed
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Mar 19 10:57:50 2024 -0400
Refactor x6 code pattern
---
.../commons/configuration2/DataConfiguration.java | 52 +++++++---------------
1 file changed, 16 insertions(+), 36 deletions(-)
diff --git a/src/main/java/org/apache/commons/configuration2/DataConfiguration.java b/src/main/java/org/apache/commons/configuration2/DataConfiguration.java
index bb6af620..9960971f 100644
--- a/src/main/java/org/apache/commons/configuration2/DataConfiguration.java
+++ b/src/main/java/org/apache/commons/configuration2/DataConfiguration.java
@@ -30,6 +30,7 @@ import java.util.List;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Objects;
+import java.util.function.Supplier;
import org.apache.commons.configuration2.convert.ConversionHandler;
import org.apache.commons.configuration2.convert.DefaultConversionHandler;
@@ -196,6 +197,15 @@ public class DataConfiguration extends AbstractConfiguration {
return configuration.containsKey(key);
}
+ private <R> R applyTempDateFormat(final String format, final Supplier<R> supplier) {
+ TEMP_DATE_FORMAT.set(format);
+ try {
+ return supplier.get();
+ } finally {
+ TEMP_DATE_FORMAT.remove();
+ }
+ }
+
@Override
protected void clearPropertyDirect(final String key) {
configuration.clearProperty(key);
@@ -919,12 +929,7 @@ public class DataConfiguration extends AbstractConfiguration {
* @throws ConversionException is thrown if the key maps to an object that is not a Date.
*/
public Date getDate(final String key, final Date defaultValue, final String format) {
- TEMP_DATE_FORMAT.set(format);
- try {
- return get(Date.class, key, defaultValue);
- } finally {
- TEMP_DATE_FORMAT.remove();
- }
+ return applyTempDateFormat(format, () -> get(Date.class, key, defaultValue));
}
public List<Date> getDateList(final String key) {
@@ -973,12 +978,7 @@ public class DataConfiguration extends AbstractConfiguration {
* @throws ConversionException is thrown if the key maps to an object that is not a list of Dates.
*/
public List<Date> getDateList(final String key, final List<Date> defaultValue, final String format) {
- TEMP_DATE_FORMAT.set(format);
- try {
- return getList(Date.class, key, defaultValue);
- } finally {
- TEMP_DATE_FORMAT.remove();
- }
+ return applyTempDateFormat(format, () -> getList(Date.class, key, defaultValue));
}
/**
@@ -1037,12 +1037,7 @@ public class DataConfiguration extends AbstractConfiguration {
* @throws ConversionException is thrown if the key maps to an object that is not a list of Dates.
*/
public Date[] getDateArray(final String key, final Date[] defaultValue, final String format) {
- TEMP_DATE_FORMAT.set(format);
- try {
- return get(Date[].class, key, defaultValue);
- } finally {
- TEMP_DATE_FORMAT.remove();
- }
+ return applyTempDateFormat(format, () -> get(Date[].class, key, defaultValue));
}
/**
@@ -1107,12 +1102,7 @@ public class DataConfiguration extends AbstractConfiguration {
* @throws ConversionException is thrown if the key maps to an object that is not a Calendar.
*/
public Calendar getCalendar(final String key, final Calendar defaultValue, final String format) {
- TEMP_DATE_FORMAT.set(format);
- try {
- return get(Calendar.class, key, defaultValue);
- } finally {
- TEMP_DATE_FORMAT.remove();
- }
+ return applyTempDateFormat(format, () -> get(Calendar.class, key, defaultValue));
}
/**
@@ -1172,12 +1162,7 @@ public class DataConfiguration extends AbstractConfiguration {
* @throws ConversionException is thrown if the key maps to an object that is not a list of Calendars.
*/
public List<Calendar> getCalendarList(final String key, final List<Calendar> defaultValue, final String format) {
- TEMP_DATE_FORMAT.set(format);
- try {
- return getList(Calendar.class, key, defaultValue);
- } finally {
- TEMP_DATE_FORMAT.remove();
- }
+ return applyTempDateFormat(format, () -> getList(Calendar.class, key, defaultValue));
}
/**
@@ -1239,12 +1224,7 @@ public class DataConfiguration extends AbstractConfiguration {
* @throws ConversionException is thrown if the key maps to an object that is not a list of Calendars.
*/
public Calendar[] getCalendarArray(final String key, final Calendar[] defaultValue, final String format) {
- TEMP_DATE_FORMAT.set(format);
- try {
- return get(Calendar[].class, key, defaultValue);
- } finally {
- TEMP_DATE_FORMAT.remove();
- }
+ return applyTempDateFormat(format, () -> get(Calendar[].class, key, defaultValue));
}
/**