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 2021/01/15 04:58:55 UTC

[groovy] 01/02: last() method for LinkedList can be more efficient (closes #1456)

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

commit c7652957b29d3f204ece9b80b32cd15f52860f5f
Author: davydotcom <da...@gmail.com>
AuthorDate: Thu Jan 14 10:29:12 2021 -0500

    last() method for LinkedList can be more efficient (closes #1456)
---
 .../groovy/runtime/DefaultGroovyMethods.java       | 24 ++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index cd6ddc8..ccc97a0 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -9834,6 +9834,29 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
         return true;
     }
 
+
+     /**
+     * Returns the last item from the LinkedList.
+     * <pre class="groovyTestCase">
+     * def list = [3, 4, 2]
+     * assert list.last() == 2
+     * // check original is unaltered
+     * assert list == [3, 4, 2]
+     * </pre>
+     *
+     * @param self a List
+     * @return the last item from the List
+     * @throws NoSuchElementException if the list is empty and you try to access the last() item.
+     * @since 1.5.5
+     */
+    public static <T> T last(LinkedList<T> self) {
+
+        if (self.isEmpty()) {
+            throw new NoSuchElementException("Cannot access last() element from an empty List");
+        }
+        return self.getLast();
+    }
+
     /**
      * Returns the last item from the List.
      * <pre class="groovyTestCase">
@@ -9849,6 +9872,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 1.5.5
      */
     public static <T> T last(List<T> self) {
+
         if (self.isEmpty()) {
             throw new NoSuchElementException("Cannot access last() element from an empty List");
         }