This is an automated email from the ASF dual-hosted git repository. paulk pushed a commit to branch GROOVY_4_0_X in repository https://gitbox.apache.org/repos/asf/groovy.git
commit c0d38bec72717b1cbd963e12ad0accd392c77f36 Author: Paul King <[email protected]> AuthorDate: Wed Aug 24 19:20:46 2022 +1000 GROOVY-10728: Enhance groovy-dateutil extensions to support collections of indices when accessing Calendar/Date --- .../dateutil/extensions/DateUtilExtensions.java | 44 +++++++++++++++++++++- .../extensions/DateUtilExtensionsTest.java | 12 ++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilExtensions.java b/subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilExtensions.java index b2fb898906..a0c57bdeab 100644 --- a/subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilExtensions.java +++ b/subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilExtensions.java @@ -20,13 +20,17 @@ package org.apache.groovy.dateutil.extensions; import groovy.lang.Closure; import groovy.lang.GroovyRuntimeException; +import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation; import java.sql.Timestamp; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; +import java.util.Collection; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.TimeZone; @@ -35,6 +39,7 @@ import java.util.TimeZone; * Date and Calendar classes inside the Groovy environment. */ public class DateUtilExtensions { + private DateUtilExtensions() {} /** * Support the subscript operator for a Date. @@ -48,7 +53,22 @@ public class DateUtilExtensions { public static int getAt(Date self, int field) { Calendar cal = Calendar.getInstance(); cal.setTime(self); - return cal.get(field); + return getAt(cal, field); + } + + /** + * Support the subscript operator for a Date with a collection of indices. + * + * @param self a Date + * @param fields a collection of Calendar fields, e.g. [YEAR, MONTH] + * @return the value for the given field, e.g. [2022, FEBRUARY] + * @see java.util.Calendar + * @since 4.0.5 + */ + public static List<Integer> getAt(Date self, Collection fields) { + Calendar cal = Calendar.getInstance(); + cal.setTime(self); + return getAt(cal, fields); } /** @@ -77,6 +97,28 @@ public class DateUtilExtensions { return self.get(field); } + /** + * Support the subscript operator for a Calendar with a collection of indices. + * + * @param self a Calendar + * @param fields a collection of Calendar fields, e.g. [YEAR, MONTH] + * @return the value for the given field, e.g. [2022, FEBRUARY] + * @see java.util.Calendar + * @since 4.0.5 + */ + public static List<Integer> getAt(Calendar self, Collection fields) { + List<Integer> answer = new ArrayList<>(fields.size()); + for (Object field : fields) { + if (field instanceof Collection) { + answer.addAll(getAt(self, (Collection) field)); + } else { + int idx = DefaultTypeTransformation.intUnbox(field); + answer.add(getAt(self, idx)); + } + } + return answer; + } + /** * Support the subscript operator for mutating a Calendar. * Example usage: diff --git a/subprojects/groovy-dateutil/src/test/java/org/apache/groovy/dateutil/extensions/DateUtilExtensionsTest.java b/subprojects/groovy-dateutil/src/test/java/org/apache/groovy/dateutil/extensions/DateUtilExtensionsTest.java index 8cecc5b106..e2f21cea7f 100644 --- a/subprojects/groovy-dateutil/src/test/java/org/apache/groovy/dateutil/extensions/DateUtilExtensionsTest.java +++ b/subprojects/groovy-dateutil/src/test/java/org/apache/groovy/dateutil/extensions/DateUtilExtensionsTest.java @@ -24,10 +24,13 @@ import org.junit.Test; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Calendar; import java.util.Date; +import java.util.List; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class DateUtilExtensionsTest { @Test @@ -61,4 +64,13 @@ public class DateUtilExtensionsTest { calendar.setTime(sdf.parse("20180101")); assertEquals("20171231", sdf.format(DateUtilExtensions.previous(calendar).getTime())); } + + @Test + public void calendarCollectGetAt() { + Calendar calendar = Calendar.getInstance(); + List<Integer> result = DateUtilExtensions.getAt(calendar, Arrays.asList(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH)); + assertTrue("Year", result.get(0) >= 2022); + assertTrue("Month", result.get(1) <= 11); + assertTrue("Day", result.get(2) <= 31); + } }
