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 2020/07/12 13:25:27 UTC

[groovy] branch GROOVY-9631 updated: Trivial tweak: create default `IndexMap` with supplier

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

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


The following commit(s) were added to refs/heads/GROOVY-9631 by this push:
     new bfbf13c  Trivial tweak: create default `IndexMap` with supplier
bfbf13c is described below

commit bfbf13c6d63584f205f8c052a91ec47c13bf0a4d
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Jul 12 21:22:04 2020 +0800

    Trivial tweak: create default `IndexMap` with supplier
---
 src/main/java/groovy/lang/MetaClassImpl.java | 37 ++++++++++++++--------------
 1 file changed, 19 insertions(+), 18 deletions(-)

diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index 641d31c..b59c1ba 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -110,6 +110,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
+import java.util.function.Supplier;
 
 import static org.apache.groovy.util.Arrays.concat;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.inSamePackage;
@@ -283,12 +284,12 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
     public MetaProperty getMetaProperty(String name) {
         MetaProperty metaProperty;
 
-        IndexMap<String, MetaProperty> propertyMap = classPropertyIndex.getOrPut(theCachedClass, new IndexMap<>());
+        IndexMap<String, MetaProperty> propertyMap = classPropertyIndex.getOrPut(theCachedClass, IndexMap::new);
         metaProperty = propertyMap.get(name);
         if (metaProperty == null) {
             metaProperty = staticPropertyIndex.get(name);
             if (metaProperty == null) {
-                propertyMap = classPropertyIndexForSuper.getOrPut(theCachedClass, new IndexMap<>());
+                propertyMap = classPropertyIndexForSuper.getOrPut(theCachedClass, IndexMap::new);
                 metaProperty = propertyMap.get(name);
                 if (metaProperty == null) {
                     MetaBeanProperty property = findPropertyInClassHierarchy(name, theCachedClass);
@@ -2346,9 +2347,9 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
                 superInterfaces.sort(CACHED_CLASS_NAME_COMPARATOR);
             }
 
-            IndexMap<String, MetaProperty> iPropertyIndex = classPropertyIndex.getOrPut(theCachedClass, new IndexMap<>());
+            IndexMap<String, MetaProperty> iPropertyIndex = classPropertyIndex.getOrPut(theCachedClass, IndexMap::new);
             for (CachedClass iclass : superInterfaces) {
-                IndexMap<String, MetaProperty> sPropertyIndex = classPropertyIndex.getOrPut(iclass, new IndexMap<>());
+                IndexMap<String, MetaProperty> sPropertyIndex = classPropertyIndex.getOrPut(iclass, IndexMap::new);
                 copyNonPrivateFields(sPropertyIndex, iPropertyIndex, null);
                 addFields(iclass, iPropertyIndex);
             }
@@ -2388,7 +2389,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
     }
 
     private void makeStaticPropertyIndex() {
-        IndexMap<String, MetaProperty> propertyMap = classPropertyIndex.getOrPut(theCachedClass, new IndexMap<>());
+        IndexMap<String, MetaProperty> propertyMap = classPropertyIndex.getOrPut(theCachedClass, IndexMap::new);
         for (Iterator<Map.Entry<String, MetaProperty>> iter = propertyMap.entrySet().iterator(); iter.hasNext(); ) {
             Map.Entry<String, MetaProperty> entry = iter.next();
 
@@ -2469,11 +2470,11 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
 
     private void inheritStaticInterfaceFields(List<CachedClass> superClasses, Set<CachedClass> interfaces) {
         for (CachedClass iclass : interfaces) {
-            IndexMap<String, MetaProperty> iPropertyIndex = classPropertyIndex.getOrPut(iclass, new IndexMap<>());
+            IndexMap<String, MetaProperty> iPropertyIndex = classPropertyIndex.getOrPut(iclass, IndexMap::new);
             addFields(iclass, iPropertyIndex);
             for (CachedClass superClass : superClasses) {
                 if (!iclass.getTheClass().isAssignableFrom(superClass.getTheClass())) continue;
-                IndexMap<String, MetaProperty> sPropertyIndex = classPropertyIndex.getOrPut(superClass, new IndexMap<>());
+                IndexMap<String, MetaProperty> sPropertyIndex = classPropertyIndex.getOrPut(superClass, IndexMap::new);
                 copyNonPrivateFields(iPropertyIndex, sPropertyIndex, null);
             }
         }
@@ -2482,7 +2483,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
     private void inheritFields(LinkedList<CachedClass> superClasses) {
         IndexMap<String, MetaProperty> last = null;
         for (CachedClass klass : superClasses) {
-            IndexMap<String, MetaProperty> propertyIndex = classPropertyIndex.getOrPut(klass, new IndexMap<>());
+            IndexMap<String, MetaProperty> propertyIndex = classPropertyIndex.getOrPut(klass, IndexMap::new);
             if (last != null) {
                 copyNonPrivateFields(last, propertyIndex, klass);
             }
@@ -2513,7 +2514,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         // now look for any stray getters that may be used to define a property
         for (CachedClass klass : superClasses) {
             MetaMethodIndex.Header header = metaMethodIndex.getHeader(klass.getTheClass());
-            IndexMap<String, MetaProperty> propertyIndex = classPropertyIndex.getOrPut(klass, new IndexMap<>());
+            IndexMap<String, MetaProperty> propertyIndex = classPropertyIndex.getOrPut(klass, IndexMap::new);
             for (MetaMethodIndex.Entry e = header.head; e != null; e = e.nextClassEntry) {
                 String methodName = e.name;
                 // name too short?
@@ -2653,7 +2654,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         if (staticProperty != null) {
             staticPropertyIndex.put(mp.getName(), mp);
         } else {
-            IndexMap<String, MetaProperty> propertyMap = classPropertyIndex.getOrPut(theCachedClass, new IndexMap<>());
+            IndexMap<String, MetaProperty> propertyMap = classPropertyIndex.getOrPut(theCachedClass, IndexMap::new);
             //keep field
             CachedField field;
             MetaProperty old = propertyMap.get(mp.getName());
@@ -3901,6 +3902,14 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         metaMethodIndex.clearCaches();
     }
 
+    private static class IndexMap<K, V> extends LinkedHashMap<K, V> {
+        private static final long serialVersionUID = 3695878442364190521L;
+
+        public V getOrPut(K key, Supplier<? extends V> valueSupplier) {
+            return this.computeIfAbsent(key, k -> valueSupplier.get());
+        }
+    }
+
     @Deprecated
     private static final SingleKeyHashMap.Copier NAME_INDEX_COPIER = value -> {
         if (value instanceof FastArray) {
@@ -3913,14 +3922,6 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
     @Deprecated
     private static final SingleKeyHashMap.Copier METHOD_INDEX_COPIER = value -> SingleKeyHashMap.copy(new SingleKeyHashMap(false), (SingleKeyHashMap) value, NAME_INDEX_COPIER);
 
-    private static class IndexMap<K, V> extends LinkedHashMap<K, V> {
-        private static final long serialVersionUID = 3695878442364190521L;
-
-        public V getOrPut(K key, V value) {
-            return this.computeIfAbsent(key, k -> value);
-        }
-    }
-
     /**
      * @deprecated use {@link IndexMap} instead
      */