changeset 2943:8819ec2a8246

8075509: List.map should return itself if list is unchanged Summary: Fix List.map to match semantics of old Type.map Reviewed-by: jlahoda
author mcimadamore
date Thu, 19 Mar 2015 16:23:21 +0000
parents eb7b825ad678
children 5ee7bba6ef41
files src/jdk.compiler/share/classes/com/sun/tools/javac/util/List.java
diffstat 1 files changed, 7 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/List.java	Thu Mar 19 11:40:47 2015 +0000
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/List.java	Thu Mar 19 16:23:21 2015 +0000
@@ -417,12 +417,15 @@
         return last;
     }
 
+    @SuppressWarnings("unchecked")
     public <Z> List<Z> map(Function<A, Z> mapper) {
-        ListBuffer<Z> buf = new ListBuffer<>();
-        for (A a : this) {
-            buf.add(mapper.apply(a));
+        if (nonEmpty()) {
+            List<Z> tail1 = tail.map(mapper);
+            Z head1 = mapper.apply(head);
+            if (tail1 != tail || head1 != head)
+                return tail1.prepend(head1);
         }
-        return buf.toList();
+        return (List<Z>)this;
     }
 
     @SuppressWarnings("unchecked")