groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] 05/06: Trivial refactoring: Can be replaced with single 'Map.computeIfAbsent' method call
Date Sun, 03 Nov 2019 13:39:11 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 76e82806c46366bfbd0cdecc631a91c4d6597387
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Sun Nov 3 21:34:26 2019 +0800

    Trivial refactoring: Can be replaced with single 'Map.computeIfAbsent' method call
---
 .../java/groovy/util/FactoryBuilderSupport.java    |  6 +-----
 .../groovy/ast/decompiled/AsmDecompiler.java       |  5 +----
 .../groovy/runtime/DefaultGroovyMethods.java       |  7 +------
 .../runtime/metaclass/MetaClassRegistryImpl.java   | 24 ++++------------------
 .../stc/GroovyTypeCheckingExtensionSupport.java    |  6 +-----
 .../transform/stc/StaticTypeCheckingVisitor.java   | 12 ++---------
 6 files changed, 10 insertions(+), 50 deletions(-)

diff --git a/src/main/java/groovy/util/FactoryBuilderSupport.java b/src/main/java/groovy/util/FactoryBuilderSupport.java
index aeffa68..be85429 100644
--- a/src/main/java/groovy/util/FactoryBuilderSupport.java
+++ b/src/main/java/groovy/util/FactoryBuilderSupport.java
@@ -169,11 +169,7 @@ public abstract class FactoryBuilderSupport extends Binding {
     }
 
     private Set<String> getRegistrationGroup(String name) {
-        Set<String> group = registrationGroup.get(name);
-        if (group == null ) {
-            group = new TreeSet<String>();
-            registrationGroup.put(name, group);
-        }
+        Set<String> group = registrationGroup.computeIfAbsent(name, k -> new TreeSet<String>());
         return group;
     }
 
diff --git a/src/main/java/org/codehaus/groovy/ast/decompiled/AsmDecompiler.java b/src/main/java/org/codehaus/groovy/ast/decompiled/AsmDecompiler.java
index 4e0f922..bee97c0 100644
--- a/src/main/java/org/codehaus/groovy/ast/decompiled/AsmDecompiler.java
+++ b/src/main/java/org/codehaus/groovy/ast/decompiled/AsmDecompiler.java
@@ -140,10 +140,7 @@ public abstract class AsmDecompiler {
                     @Override
                     public AnnotationVisitor visitParameterAnnotation(int parameter, String
desc, boolean visible) {
                         if (stub.parameterAnnotations == null) stub.parameterAnnotations
= new HashMap<Integer, List<AnnotationStub>>(1);
-                        List<AnnotationStub> list = stub.parameterAnnotations.get(parameter);
-                        if (list == null) {
-                            stub.parameterAnnotations.put(parameter, list = new ArrayList<AnnotationStub>());
-                        }
+                        List<AnnotationStub> list = stub.parameterAnnotations.computeIfAbsent(parameter,
k -> new ArrayList<AnnotationStub>());
                         AnnotationStub annotationStub = new AnnotationStub(desc);
                         list.add(annotationStub);
                         return readAnnotationMembers(annotationStub);
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 64b3603..3811036 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -5917,12 +5917,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport
{
      * @since 1.5.0
      */
     protected static <K, T> void groupAnswer(final Map<K, List<T>> answer,
T element, K value) {
-        List<T> groupedElements = answer.get(value);
-
-        if (null == groupedElements) {
-            groupedElements = new ArrayList<T>();
-            answer.put(value, groupedElements);
-        }
+        List<T> groupedElements = answer.computeIfAbsent(value, k -> new ArrayList<T>());
 
         groupedElements.add(element);
     }
diff --git a/src/main/java/org/codehaus/groovy/runtime/metaclass/MetaClassRegistryImpl.java
b/src/main/java/org/codehaus/groovy/runtime/metaclass/MetaClassRegistryImpl.java
index ec9fe1b..5edca84 100644
--- a/src/main/java/org/codehaus/groovy/runtime/metaclass/MetaClassRegistryImpl.java
+++ b/src/main/java/org/codehaus/groovy/runtime/metaclass/MetaClassRegistryImpl.java
@@ -210,11 +210,7 @@ public class MetaClassRegistryImpl implements MetaClassRegistry{
                             newParams
                     );
                     final CachedClass declClass = method.getDeclaringClass();
-                    List<MetaMethod> arr = map.get(declClass);
-                    if (arr == null) {
-                        arr = new ArrayList<MetaMethod>(4);
-                        map.put(declClass, arr);
-                    }
+                    List<MetaMethod> arr = map.computeIfAbsent(declClass, k -> new
ArrayList<MetaMethod>(4));
                     arr.add(method);
                     instanceMethods.add(method);
                 }
@@ -231,11 +227,7 @@ public class MetaClassRegistryImpl implements MetaClassRegistry{
                 if (Modifier.isStatic(mod) && Modifier.isPublic(mod) && method.getAnnotation(Deprecated.class)
== null) {
                     CachedClass[] paramTypes = method.getParameterTypes();
                     if (paramTypes.length > 0) {
-                        List<MetaMethod> arr = map.get(paramTypes[0]);
-                        if (arr == null) {
-                            arr = new ArrayList<MetaMethod>(4);
-                            map.put(paramTypes[0], arr);
-                        }
+                        List<MetaMethod> arr = map.computeIfAbsent(paramTypes[0], k
-> new ArrayList<MetaMethod>(4));
                         if (useInstanceMethods) {
                             final NewInstanceMetaMethod metaMethod = new NewInstanceMetaMethod(method);
                             arr.add(metaMethod);
@@ -255,11 +247,7 @@ public class MetaClassRegistryImpl implements MetaClassRegistry{
         try {
             MetaMethod method = (MetaMethod) aClass.getDeclaredConstructor().newInstance();
             final CachedClass declClass = method.getDeclaringClass();
-            List<MetaMethod> arr = map.get(declClass);
-            if (arr == null) {
-                arr = new ArrayList<MetaMethod>(4);
-                map.put(declClass, arr);
-            }
+            List<MetaMethod> arr = map.computeIfAbsent(declClass, k -> new ArrayList<MetaMethod>(4));
             arr.add(method);
             instanceMethods.add(method);
         } catch (InstantiationException | IllegalAccessException | NoSuchMethodException
| InvocationTargetException e) { /* ignore */
@@ -527,11 +515,7 @@ public class MetaClassRegistryImpl implements MetaClassRegistry{
             List<MetaMethod> metaMethods = module.getMetaMethods();
             for (MetaMethod metaMethod : metaMethods) {
                 CachedClass cachedClass = metaMethod.getDeclaringClass();
-                List<MetaMethod> methods = map.get(cachedClass);
-                if (methods == null) {
-                    methods = new ArrayList<MetaMethod>(4);
-                    map.put(cachedClass, methods);
-                }
+                List<MetaMethod> methods = map.computeIfAbsent(cachedClass, k ->
new ArrayList<MetaMethod>(4));
                 methods.add(metaMethod);
                 if (metaMethod.isStatic()) {
                     staticMethods.add(metaMethod);
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/GroovyTypeCheckingExtensionSupport.java
b/src/main/java/org/codehaus/groovy/transform/stc/GroovyTypeCheckingExtensionSupport.java
index d456331..0852cf3 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/GroovyTypeCheckingExtensionSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/GroovyTypeCheckingExtensionSupport.java
@@ -441,11 +441,7 @@ public class GroovyTypeCheckingExtensionSupport extends AbstractTypeCheckingExte
                 if (methodName == null) {
                     return InvokerHelper.invokeMethod(extension, name, args);
                 }
-                List<Closure> closures = extension.eventHandlers.get(methodName);
-                if (closures == null) {
-                    closures = new LinkedList<Closure>();
-                    extension.eventHandlers.put(methodName, closures);
-                }
+                List<Closure> closures = extension.eventHandlers.computeIfAbsent(methodName,
k -> new LinkedList<Closure>());
                 closures.add((Closure) argsArray[0]);
                 return null;
             } else {
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 226064a..37abc13 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1145,11 +1145,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
     protected void pushInstanceOfTypeInfo(final Expression objectOfInstanceOf, final Expression
typeExpression) {
         final Map<Object, List<ClassNode>> tempo = typeCheckingContext.temporaryIfBranchTypeInformation.peek();
         Object key = extractTemporaryTypeInfoKey(objectOfInstanceOf);
-        List<ClassNode> potentialTypes = tempo.get(key);
-        if (potentialTypes == null) {
-            potentialTypes = new LinkedList<ClassNode>();
-            tempo.put(key, potentialTypes);
-        }
+        List<ClassNode> potentialTypes = tempo.computeIfAbsent(key, k -> new LinkedList<ClassNode>());
         potentialTypes.add(typeExpression.getType());
     }
 
@@ -4225,11 +4221,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
                 ((Parameter) accessedVariable).putNodeMetaData(INFERRED_TYPE, cn);
             }
             if (var.isClosureSharedVariable() && cn != null) {
-                List<ClassNode> assignedTypes = typeCheckingContext.closureSharedVariablesAssignmentTypes.get(var);
-                if (assignedTypes == null) {
-                    assignedTypes = new LinkedList<ClassNode>();
-                    typeCheckingContext.closureSharedVariablesAssignmentTypes.put(var, assignedTypes);
-                }
+                List<ClassNode> assignedTypes = typeCheckingContext.closureSharedVariablesAssignmentTypes.computeIfAbsent(var,
k -> new LinkedList<ClassNode>());
                 assignedTypes.add(cn);
             }
             if (!typeCheckingContext.temporaryIfBranchTypeInformation.empty()) {


Mime
View raw message