You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/01/02 06:49:53 UTC

[groovy] 01/01: GROOVY-9877: Add DGM `asReversed` to `List`

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

sunlan pushed a commit to branch GROOVY-9877
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 824dc1ad925f81bf60c9f059903f69c60900c979
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Jan 2 14:49:13 2021 +0800

    GROOVY-9877: Add DGM `asReversed` to `List`
---
 .../java/org/apache/groovy/util/ReversedList.java  | 45 ++++++++++++++++++++++
 .../groovy/runtime/DefaultGroovyMethods.java       | 18 +++++++++
 2 files changed, 63 insertions(+)

diff --git a/src/main/java/org/apache/groovy/util/ReversedList.java b/src/main/java/org/apache/groovy/util/ReversedList.java
new file mode 100644
index 0000000..1bc5b53
--- /dev/null
+++ b/src/main/java/org/apache/groovy/util/ReversedList.java
@@ -0,0 +1,45 @@
+/*
+ *  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.
+ */
+package org.apache.groovy.util;
+
+import java.util.AbstractList;
+import java.util.List;
+
+/**
+ * Represents view of reversed list
+ *
+ * @since 4.0.0
+ */
+public class ReversedList<E> extends AbstractList<E> {
+    private final List<E> delegate;
+
+    public ReversedList(List<E> list) {
+        this.delegate = list;
+    }
+
+    @Override
+    public final E get(int index) {
+        return delegate.get(delegate.size() - 1 - index);
+    }
+
+    @Override
+    public int size() {
+        return delegate.size();
+    }
+}
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 2b16562..cd6ddc8 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -55,6 +55,7 @@ import groovy.util.OrderBy;
 import groovy.util.PermutationGenerator;
 import groovy.util.ProxyGenerator;
 import org.apache.groovy.io.StringBuilderWriter;
+import org.apache.groovy.util.ReversedList;
 import org.codehaus.groovy.classgen.Verifier;
 import org.codehaus.groovy.reflection.ClassInfo;
 import org.codehaus.groovy.reflection.MixinInMetaClass;
@@ -12075,6 +12076,23 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
     }
 
     /**
+     * Creates a view list with reversed order, and the order of original list will not change.
+     * <pre class="groovyTestCase">
+     * def list = ["a", 6, true]
+     * assert list.asReversed() == [true, 6, "a"]
+     * assert list == ["a", 6, true]
+     * </pre>
+     *
+     * @param self a list
+     * @param <T> the type of element
+     * @return the reversed list
+     * @since 4.0.0
+     */
+    public static <T> List<T> asReversed(List<T> self) {
+        return new ReversedList<>(self);
+    }
+
+    /**
      * Creates a new List with the identical contents to this list
      * but in reverse order.
      * <pre class="groovyTestCase">