You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2022/09/18 08:26:42 UTC

[groovy] branch master updated: GROOVY-10759: groovy-datetime should support collections of fields when accessing a temporal accessor

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new d57971f5ed GROOVY-10759: groovy-datetime should support collections of fields when accessing a temporal accessor
d57971f5ed is described below

commit d57971f5ed276ef2deae5cbf16b0e23e498c5991
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sat Sep 17 23:41:46 2022 +1000

    GROOVY-10759: groovy-datetime should support collections of fields when accessing a temporal accessor
---
 subprojects/groovy-datetime/build.gradle           |  3 +-
 .../datetime/extensions/DateTimeExtensions.java    | 32 ++++++++++++++++++++++
 .../groovy/DateTimeJavadocAssertionTest.groovy     | 26 ++++++++++++++++++
 3 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/subprojects/groovy-datetime/build.gradle b/subprojects/groovy-datetime/build.gradle
index 77d82404c9..37723aeb3b 100644
--- a/subprojects/groovy-datetime/build.gradle
+++ b/subprojects/groovy-datetime/build.gradle
@@ -24,6 +24,7 @@ dependencies {
     api rootProject // DateTimeExtensions has methods with Closure params...
     testImplementation projects.groovyTest
     testImplementation projects.groovyDateutil
+    testRuntimeOnly projects.groovyAnt // for JavadocAssertionTests
 }
 
 groovyLibrary {
@@ -31,4 +32,4 @@ groovyLibrary {
         extensionClasses = 'org.apache.groovy.datetime.extensions.DateTimeExtensions'
         staticExtensionClasses = 'org.apache.groovy.datetime.extensions.DateTimeStaticExtensions'
     }
-}
\ No newline at end of file
+}
diff --git a/subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeExtensions.java b/subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeExtensions.java
index 5f6e41adc3..f871c8e858 100644
--- a/subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeExtensions.java
+++ b/subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeExtensions.java
@@ -51,10 +51,12 @@ import java.time.temporal.TemporalAmount;
 import java.time.temporal.TemporalField;
 import java.time.temporal.TemporalUnit;
 import java.time.temporal.UnsupportedTemporalTypeException;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.TimeZone;
@@ -296,6 +298,36 @@ public final class DateTimeExtensions {
         return self.getLong(field);
     }
 
+    /**
+     * Supports the getAt operator for an iterable; equivalent to collecting the
+     * {@link java.time.temporal.TemporalAccessor#getLong(java.time.temporal.TemporalField)} method
+     * for each field in the supplied iterable.
+     * <pre class="groovyTestCase">
+     * import static java.time.temporal.ChronoField.*
+     * import java.time.LocalTime
+     *
+     * def breakfast = LocalTime.of(8, 30, 0)
+     * def (h, m, s) = breakfast[HOUR_OF_DAY, MINUTE_OF_HOUR, SECOND_OF_MINUTE]
+     * assert "$h:$m:$s" == "8:30:0"
+     * </pre>
+     * <p>
+     *
+     * @param self  a TemporalAccessor
+     * @param fields an iterable of non-null TemporalField values
+     * @return the list of values for the fields
+     * @throws DateTimeException                if a value for the field cannot be obtained
+     * @throws UnsupportedTemporalTypeException if the field is not supported
+     * @throws ArithmeticException              if numeric overflow occurs
+     * @since 4.0.6
+     */
+    public static List<Long> getAt(final TemporalAccessor self, Iterable<TemporalField> fields) {
+        List<Long> result = new ArrayList<>();
+        for (TemporalField f : fields) {
+            result.add(self.getLong(f));
+        }
+        return result;
+    }
+
     /* ******** java.time.temporal.TemporalAmount extension methods ******** */
 
     /**
diff --git a/subprojects/groovy-datetime/src/test/groovy/DateTimeJavadocAssertionTest.groovy b/subprojects/groovy-datetime/src/test/groovy/DateTimeJavadocAssertionTest.groovy
new file mode 100644
index 0000000000..5ba3937c9b
--- /dev/null
+++ b/subprojects/groovy-datetime/src/test/groovy/DateTimeJavadocAssertionTest.groovy
@@ -0,0 +1,26 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements. See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership. The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License. You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied. See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+import groovy.test.JavadocAssertionTestSuite
+import org.junit.runner.RunWith
+import org.junit.runners.Suite
+
+@RunWith(Suite)
+@Suite.SuiteClasses(JavadocAssertionTestSuite)
+class DateTimeJavadocAssertionTest {
+}