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 2023/03/12 23:53:41 UTC
[groovy] 01/02: GROOVY-10973: Groovy should provide 'asReversed' and 'reverseEach' methods for NavigableSet
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 51f0929fffbe2097bd688e8532061893d84db744
Author: Paul King <pa...@asert.com.au>
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 660a612cc8..029c1bd41b 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -147,6 +147,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;
@@ -2543,6 +2544,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 << 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).
@@ -12393,6 +12413,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.