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 dedafb5a94 GROOVY-10973: Groovy should provide 'asReversed' and 
'reverseEach' methods for NavigableSet
dedafb5a94 is described below

commit dedafb5a9432693d5f288e769a750fbc28547af7
Author: Paul King <[email protected]>
AuthorDate: Fri Mar 10 11:45:28 2023 +1000

    GROOVY-10973: Groovy should provide 'asReversed' and 'reverseEach' methods 
for NavigableSet
---
 .../groovy/runtime/DefaultGroovyMethods.java       | 36 ++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git 
a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java 
b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index e83880a5a0..4d441e8597 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -139,6 +139,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.ListIterator;
 import java.util.Map;
+import java.util.NavigableSet;
 import java.util.NoSuchElementException;
 import java.util.Objects;
 import java.util.Queue;
@@ -2569,6 +2570,25 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
         return self;
     }
 
+    /**
+     * Iterate over each element of the set in reverse order.
+     * <pre class="groovyTestCase">
+     * TreeSet navSet = [2, 4, 1, 3]  // natural order is sorted
+     * List result = []
+     * navSet.reverseEach { result &lt;&lt; it }
+     * assert result == [4, 3, 2, 1]
+     * </pre>
+     *
+     * @param self    a NavigableSet
+     * @param closure a closure to which each item is passed.
+     * @return the original NavigableSet
+     * @since 4.0.11
+     */
+    public static <T> NavigableSet<T> reverseEach(NavigableSet<T> self, 
@ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
+        each(self.descendingIterator(), closure);
+        return self;
+    }
+
     /**
      * Used to determine if the given predicate closure is valid (i.e. returns
      * <code>true</code> for all items in this data structure).
@@ -12151,6 +12171,22 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
         return new ReversedList<>(self);
     }
 
+    /**
+     * Creates a reverse order view of the set. The order of original list 
will not change.
+     * <pre class="groovyTestCase">
+     * TreeSet navSet = [2, 4, 1, 3]  // natural order is sorted
+     * assert navSet.asReversed() == [4, 3, 2, 1] as Set
+     * </pre>
+     *
+     * @param self a NavigableSet
+     * @param <T> the type of element
+     * @return the reversed view NavigableSet
+     * @since 4.0.11
+     */
+    public static <T> NavigableSet<T> asReversed(NavigableSet<T> self) {
+        return self.descendingSet();
+    }
+
     /**
      * Creates a new List with the identical contents to this list
      * but in reverse order.

Reply via email to