Simplify code with new Map features 34/57334/4
authorStephen Kitt <skitt@redhat.com>
Thu, 18 May 2017 09:29:32 +0000 (11:29 +0200)
committerRobert Varga <nite@hq.sk>
Fri, 19 May 2017 21:07:33 +0000 (21:07 +0000)
This is mostly computeIfAbsent to replace the “get, if null,
initialise and put” pattern.

Change-Id: Ie866025c3a6d5099f6f5494ab9d4437d7e9d2320
Signed-off-by: Stephen Kitt <skitt@redhat.com>
16 files changed:
opendaylight/config/config-api/src/main/java/org/opendaylight/controller/config/api/ValidationException.java
opendaylight/config/config-manager-facade-xml/src/main/java/org/opendaylight/controller/config/facade/xml/ConfigSubsystemFacade.java
opendaylight/config/config-manager-facade-xml/src/main/java/org/opendaylight/controller/config/facade/xml/RpcFacade.java
opendaylight/config/config-manager-facade-xml/src/main/java/org/opendaylight/controller/config/facade/xml/mapping/config/Config.java
opendaylight/config/config-manager-facade-xml/src/main/java/org/opendaylight/controller/config/facade/xml/mapping/config/ServiceRegistryWrapper.java
opendaylight/config/config-manager-facade-xml/src/main/java/org/opendaylight/controller/config/facade/xml/mapping/config/Services.java
opendaylight/config/config-manager-facade-xml/src/main/java/org/opendaylight/controller/config/facade/xml/rpc/ModuleRpcs.java
opendaylight/config/config-manager-facade-xml/src/main/java/org/opendaylight/controller/config/facade/xml/runtime/Runtime.java
opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/ServiceReferenceRegistryImpl.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/InMemoryJournal.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/InMemorySnapshotStore.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/config/FileModuleShardConfigProvider.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/ClusterUtils.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/sharding/DistributedShardFrontend.java
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/ShardedDOMDataTree.java
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/ShardingTableEntry.java

index f1c90920be8c5bcaa3dbceb44226b057547560df..1d528e5b001c5c5335fff5abbba5c41e2b7ca87c 100644 (file)
@@ -43,11 +43,7 @@ public class ValidationException extends Exception {
                     String instanceName = innerEntry.getKey();
                     ExceptionMessageWithStackTrace ex = innerEntry.getValue();
                     Map<String, ExceptionMessageWithStackTrace> instanceToExMap = failedValidations
                     String instanceName = innerEntry.getKey();
                     ExceptionMessageWithStackTrace ex = innerEntry.getValue();
                     Map<String, ExceptionMessageWithStackTrace> instanceToExMap = failedValidations
-                            .get(moduleName);
-                    if (instanceToExMap == null) {
-                        instanceToExMap = new HashMap<>();
-                        failedValidations.put(moduleName, instanceToExMap);
-                    }
+                            .computeIfAbsent(moduleName, k -> new HashMap<>());
                     if (instanceToExMap.containsKey(instanceName)) {
                         throw new IllegalArgumentException(
                                 "Cannot merge with same module name "
                     if (instanceToExMap.containsKey(instanceName)) {
                         throw new IllegalArgumentException(
                                 "Cannot merge with same module name "
index 781bda575f9bfba6640ed766896ce86c3031adea..2b4e63364671bf987ddf3287679525e757be4908 100644 (file)
@@ -9,10 +9,10 @@
 package org.opendaylight.controller.config.facade.xml;
 
 import com.google.common.base.Optional;
 package org.opendaylight.controller.config.facade.xml;
 
 import com.google.common.base.Optional;
-import com.google.common.collect.Maps;
 import com.google.common.collect.Multimap;
 import java.io.Closeable;
 import java.util.Date;
 import com.google.common.collect.Multimap;
 import java.io.Closeable;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -263,22 +263,16 @@ public class ConfigSubsystemFacade implements Closeable {
     }
 
     private static Map<String, Map<Date, IdentityMapping>> transformIdentities(final Set<Module> modules) {
     }
 
     private static Map<String, Map<Date, IdentityMapping>> transformIdentities(final Set<Module> modules) {
-        Map<String, Map<Date, IdentityMapping>> mappedIds = Maps.newHashMap();
+        Map<String, Map<Date, IdentityMapping>> mappedIds = new HashMap<>();
         for (Module module : modules) {
             String namespace = module.getNamespace().toString();
         for (Module module : modules) {
             String namespace = module.getNamespace().toString();
-            Map<Date, IdentityMapping> revisionsByNamespace = mappedIds.get(namespace);
-            if (revisionsByNamespace == null) {
-                revisionsByNamespace = Maps.newHashMap();
-                mappedIds.put(namespace, revisionsByNamespace);
-            }
+            Map<Date, IdentityMapping> revisionsByNamespace =
+                    mappedIds.computeIfAbsent(namespace, k -> new HashMap<>());
 
             Date revision = module.getRevision();
 
 
             Date revision = module.getRevision();
 
-            IdentityMapping identityMapping = revisionsByNamespace.get(revision);
-            if (identityMapping == null) {
-                identityMapping = new IdentityMapping();
-                revisionsByNamespace.put(revision, identityMapping);
-            }
+            IdentityMapping identityMapping =
+                    revisionsByNamespace.computeIfAbsent(revision, k -> new IdentityMapping());
 
             for (IdentitySchemaNode identitySchemaNode : module.getIdentities()) {
                 identityMapping.addIdSchemaNode(identitySchemaNode);
 
             for (IdentitySchemaNode identitySchemaNode : module.getIdentities()) {
                 identityMapping.addIdSchemaNode(identitySchemaNode);
@@ -302,7 +296,7 @@ public class ConfigSubsystemFacade implements Closeable {
                                                                                                              final Map<String/* Namespace from yang file */,
                                                                                                                      Map<String /* Name of module entry from yang file */, ModuleMXBeanEntry>> mBeanEntries) {
 
                                                                                                              final Map<String/* Namespace from yang file */,
                                                                                                                      Map<String /* Name of module entry from yang file */, ModuleMXBeanEntry>> mBeanEntries) {
 
-        Map<String, Map<String, ModuleConfig>> namespaceToModuleNameToModuleConfig = Maps.newHashMap();
+        Map<String, Map<String, ModuleConfig>> namespaceToModuleNameToModuleConfig = new HashMap<>();
 
         for (Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleToMbe : mBeanEntries.entrySet()) {
             for (Map.Entry<String, ModuleMXBeanEntry> moduleNameToMbe : namespaceToModuleToMbe.getValue().entrySet()) {
 
         for (Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleToMbe : mBeanEntries.entrySet()) {
             for (Map.Entry<String, ModuleMXBeanEntry> moduleNameToMbe : namespaceToModuleToMbe.getValue().entrySet()) {
@@ -312,11 +306,9 @@ public class ConfigSubsystemFacade implements Closeable {
                 ModuleConfig moduleConfig = new ModuleConfig(moduleName,
                         new InstanceConfig(reader, moduleMXBeanEntry.getAttributes(), moduleMXBeanEntry.getNullableDummyContainerName()));
 
                 ModuleConfig moduleConfig = new ModuleConfig(moduleName,
                         new InstanceConfig(reader, moduleMXBeanEntry.getAttributes(), moduleMXBeanEntry.getNullableDummyContainerName()));
 
-                Map<String, ModuleConfig> moduleNameToModuleConfig = namespaceToModuleNameToModuleConfig.get(namespaceToModuleToMbe.getKey());
-                if (moduleNameToModuleConfig == null) {
-                    moduleNameToModuleConfig = Maps.newHashMap();
-                    namespaceToModuleNameToModuleConfig.put(namespaceToModuleToMbe.getKey(), moduleNameToModuleConfig);
-                }
+                Map<String, ModuleConfig> moduleNameToModuleConfig =
+                        namespaceToModuleNameToModuleConfig.computeIfAbsent(namespaceToModuleToMbe.getKey(),
+                                k -> new HashMap<>());
 
                 moduleNameToModuleConfig.put(moduleName, moduleConfig);
             }
 
                 moduleNameToModuleConfig.put(moduleName, moduleConfig);
             }
@@ -331,17 +323,17 @@ public class ConfigSubsystemFacade implements Closeable {
 
     private Map<String, Map<String, ModuleRuntime>> createModuleRuntimes(final ConfigRegistryClient configRegistryClient,
                                                                          final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
 
     private Map<String, Map<String, ModuleRuntime>> createModuleRuntimes(final ConfigRegistryClient configRegistryClient,
                                                                          final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
-        Map<String, Map<String, ModuleRuntime>> retVal = Maps.newHashMap();
+        Map<String, Map<String, ModuleRuntime>> retVal = new HashMap<>();
 
         for (Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : mBeanEntries.entrySet()) {
 
 
         for (Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : mBeanEntries.entrySet()) {
 
-            Map<String, ModuleRuntime> innerMap = Maps.newHashMap();
+            Map<String, ModuleRuntime> innerMap = new HashMap<>();
             Map<String, ModuleMXBeanEntry> entriesFromNamespace = namespaceToModuleEntry.getValue();
             for (Map.Entry<String, ModuleMXBeanEntry> moduleToMXEntry : entriesFromNamespace.entrySet()) {
 
                 ModuleMXBeanEntry mbe = moduleToMXEntry.getValue();
 
             Map<String, ModuleMXBeanEntry> entriesFromNamespace = namespaceToModuleEntry.getValue();
             for (Map.Entry<String, ModuleMXBeanEntry> moduleToMXEntry : entriesFromNamespace.entrySet()) {
 
                 ModuleMXBeanEntry mbe = moduleToMXEntry.getValue();
 
-                Map<RuntimeBeanEntry, InstanceConfig> cache = Maps.newHashMap();
+                Map<RuntimeBeanEntry, InstanceConfig> cache = new HashMap<>();
                 RuntimeBeanEntry root = null;
                 for (RuntimeBeanEntry rbe : mbe.getRuntimeBeans()) {
                     cache.put(rbe, new InstanceConfig(configRegistryClient, rbe.getYangPropertiesToTypesMap(), mbe.getNullableDummyContainerName()));
                 RuntimeBeanEntry root = null;
                 for (RuntimeBeanEntry rbe : mbe.getRuntimeBeans()) {
                     cache.put(rbe, new InstanceConfig(configRegistryClient, rbe.getYangPropertiesToTypesMap(), mbe.getNullableDummyContainerName()));
@@ -365,7 +357,7 @@ public class ConfigSubsystemFacade implements Closeable {
     }
 
     private InstanceRuntime createInstanceRuntime(final RuntimeBeanEntry root, final Map<RuntimeBeanEntry, InstanceConfig> cache) {
     }
 
     private InstanceRuntime createInstanceRuntime(final RuntimeBeanEntry root, final Map<RuntimeBeanEntry, InstanceConfig> cache) {
-        Map<String, InstanceRuntime> children = Maps.newHashMap();
+        Map<String, InstanceRuntime> children = new HashMap<>();
         for (RuntimeBeanEntry child : root.getChildren()) {
             children.put(child.getJavaNamePrefix(), createInstanceRuntime(child, cache));
         }
         for (RuntimeBeanEntry child : root.getChildren()) {
             children.put(child.getJavaNamePrefix(), createInstanceRuntime(child, cache));
         }
@@ -374,7 +366,7 @@ public class ConfigSubsystemFacade implements Closeable {
     }
 
     private Map<String, String> createJmxToYangMap(final List<RuntimeBeanEntry> children) {
     }
 
     private Map<String, String> createJmxToYangMap(final List<RuntimeBeanEntry> children) {
-        Map<String, String> jmxToYangNamesForChildRbe = Maps.newHashMap();
+        Map<String, String> jmxToYangNamesForChildRbe = new HashMap<>();
         for (RuntimeBeanEntry rbe : children) {
             jmxToYangNamesForChildRbe.put(rbe.getJavaNamePrefix(), rbe.getYangName());
         }
         for (RuntimeBeanEntry rbe : children) {
             jmxToYangNamesForChildRbe.put(rbe.getJavaNamePrefix(), rbe.getYangName());
         }
index dd4bd5b9de37a437d0e139c1a1ba34f7483aa1ab..37043eb9ab9ca3a396cb2184c622c3f68150d260 100644 (file)
@@ -10,7 +10,8 @@ package org.opendaylight.controller.config.facade.xml;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
-import com.google.common.collect.Maps;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
 import javax.management.ObjectName;
 import javax.management.openmbean.OpenType;
 import java.util.Map;
 import javax.management.ObjectName;
 import javax.management.openmbean.OpenType;
@@ -48,23 +49,17 @@ public class RpcFacade {
 
     public Rpcs mapRpcs() {
 
 
     public Rpcs mapRpcs() {
 
-        final Map<String, Map<String, ModuleRpcs>> map = Maps.newHashMap();
+        final Map<String, Map<String, ModuleRpcs>> map = new HashMap<>();
 
         for (final Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : yangStoreService.getModuleMXBeanEntryMap().entrySet()) {
 
 
         for (final Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : yangStoreService.getModuleMXBeanEntryMap().entrySet()) {
 
-            Map<String, ModuleRpcs> namespaceToModules = map.get(namespaceToModuleEntry.getKey());
-            if (namespaceToModules == null) {
-                namespaceToModules = Maps.newHashMap();
-                map.put(namespaceToModuleEntry.getKey(), namespaceToModules);
-            }
+            Map<String, ModuleRpcs> namespaceToModules =
+                    map.computeIfAbsent(namespaceToModuleEntry.getKey(), k -> new HashMap<>());
 
             for (final Map.Entry<String, ModuleMXBeanEntry> moduleEntry : namespaceToModuleEntry.getValue().entrySet()) {
 
 
             for (final Map.Entry<String, ModuleMXBeanEntry> moduleEntry : namespaceToModuleEntry.getValue().entrySet()) {
 
-                ModuleRpcs rpcMapping = namespaceToModules.get(moduleEntry.getKey());
-                if (rpcMapping == null) {
-                    rpcMapping = new ModuleRpcs(yangStoreService.getEnumResolver());
-                    namespaceToModules.put(moduleEntry.getKey(), rpcMapping);
-                }
+                ModuleRpcs rpcMapping = namespaceToModules.computeIfAbsent(moduleEntry.getKey(),
+                        k -> new ModuleRpcs(yangStoreService.getEnumResolver()));
 
                 final ModuleMXBeanEntry entry = moduleEntry.getValue();
 
 
                 final ModuleMXBeanEntry entry = moduleEntry.getValue();
 
@@ -108,7 +103,7 @@ public class RpcFacade {
 
     private Map<String, AttributeConfigElement> sortAttributes(
             final Map<String, AttributeConfigElement> attributes, final XmlElement xml) {
 
     private Map<String, AttributeConfigElement> sortAttributes(
             final Map<String, AttributeConfigElement> attributes, final XmlElement xml) {
-        final Map<String, AttributeConfigElement> sorted = Maps.newLinkedHashMap();
+        final Map<String, AttributeConfigElement> sorted = new LinkedHashMap<>();
 
         for (XmlElement xmlElement : xml.getChildElements()) {
             final String name = xmlElement.getName();
 
         for (XmlElement xmlElement : xml.getChildElements()) {
             final String name = xmlElement.getName();
index 21b74da6cd23dd2a79c7c6422b461e1e4b428171..d40bfe1d80c170b76a8d647f32b87414ce878a5c 100644 (file)
@@ -220,11 +220,7 @@ public class Config {
 
         ModuleConfig moduleMapping = getModuleMapping(moduleNamespace, instanceName, factoryName);
 
 
         ModuleConfig moduleMapping = getModuleMapping(moduleNamespace, instanceName, factoryName);
 
-        Multimap<String, T> innerMap = retVal.get(moduleNamespace);
-        if (innerMap == null) {
-            innerMap = HashMultimap.create();
-            retVal.put(moduleNamespace, innerMap);
-        }
+        Multimap<String, T> innerMap = retVal.computeIfAbsent(moduleNamespace, k -> HashMultimap.create());
 
         T resolvedElement = resolvingStrategy.resolveElement(moduleMapping, moduleElement, serviceTracker,
                 instanceName, moduleNamespace, defaultStrategy);
 
         T resolvedElement = resolvingStrategy.resolveElement(moduleMapping, moduleElement, serviceTracker,
                 instanceName, moduleNamespace, defaultStrategy);
index b9e5f0f5b56005eeb29f34411ac0276c314f3b5a..eab120a3ba749d46ea42307b43e3e40b94a485ee 100644 (file)
@@ -9,7 +9,7 @@
 package org.opendaylight.controller.config.facade.xml.mapping.config;
 
 import com.google.common.base.Preconditions;
 package org.opendaylight.controller.config.facade.xml.mapping.config;
 
 import com.google.common.base.Preconditions;
-import com.google.common.collect.Maps;
+import java.util.HashMap;
 import java.util.Map;
 import javax.management.InstanceNotFoundException;
 import javax.management.ObjectName;
 import java.util.Map;
 import javax.management.InstanceNotFoundException;
 import javax.management.ObjectName;
@@ -64,7 +64,7 @@ public class ServiceRegistryWrapper {
     }
 
     public Map<String, Map<String, Map<String, String>>> getMappedServices() {
     }
 
     public Map<String, Map<String, Map<String, String>>> getMappedServices() {
-        Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
+        Map<String, Map<String, Map<String, String>>> retVal = new HashMap<>();
 
         Map<String, Map<String, ObjectName>> serviceMapping = configServiceRefRegistry.getServiceMapping();
         for (Map.Entry<String, Map<String, ObjectName>> qNameToRefNameEntry : serviceMapping.entrySet()){
 
         Map<String, Map<String, ObjectName>> serviceMapping = configServiceRefRegistry.getServiceMapping();
         for (Map.Entry<String, Map<String, ObjectName>> qNameToRefNameEntry : serviceMapping.entrySet()){
@@ -75,18 +75,11 @@ public class ServiceRegistryWrapper {
 
                 QName qname = QName.create(qNameToRefNameEntry.getKey());
                 String namespace = qname.getNamespace().toString();
 
                 QName qname = QName.create(qNameToRefNameEntry.getKey());
                 String namespace = qname.getNamespace().toString();
-                Map<String, Map<String, String>> serviceToRefs = retVal.get(namespace);
-                if(serviceToRefs==null) {
-                    serviceToRefs = Maps.newHashMap();
-                    retVal.put(namespace, serviceToRefs);
-                }
+                Map<String, Map<String, String>> serviceToRefs =
+                        retVal.computeIfAbsent(namespace, k -> new HashMap<>());
 
                 String localName = qname.getLocalName();
 
                 String localName = qname.getLocalName();
-                Map<String, String> refsToSis = serviceToRefs.get(localName);
-                if(refsToSis==null) {
-                    refsToSis = Maps.newHashMap();
-                    serviceToRefs.put(localName, refsToSis);
-                }
+                Map<String, String> refsToSis = serviceToRefs.computeIfAbsent(localName, k -> new HashMap<>());
 
                 Preconditions.checkState(!refsToSis.containsKey(refName),
                         "Duplicate reference name %s for service %s:%s, now for instance %s", refName, namespace,
 
                 Preconditions.checkState(!refsToSis.containsKey(refName),
                         "Duplicate reference name %s for service %s:%s, now for instance %s", refName, namespace,
index 4ee1bcaf0b1a7f37ee8d5285924b996a67ef65dd..9853bc79a26eb9db51b2ceb3e8bc39ec8d98a0a4 100644 (file)
@@ -11,7 +11,7 @@ package org.opendaylight.controller.config.facade.xml.mapping.config;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
-import com.google.common.collect.Maps;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -36,8 +36,8 @@ public final class Services {
     public static final String TYPE_KEY = "type";
     public static final String SERVICE_KEY = "service";
 
     public static final String TYPE_KEY = "type";
     public static final String SERVICE_KEY = "service";
 
-    private final Map<String /*Namespace*/, Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = Maps
-            .newHashMap();
+    private final Map<String /*Namespace*/, Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>>>
+            namespaceToServiceNameToRefNameToInstance = new HashMap<>();
 
     /**
      *
 
     /**
      *
@@ -57,18 +57,12 @@ public final class Services {
                 String serviceName = serviceEntry.getKey();
                 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
 
                 String serviceName = serviceEntry.getKey();
                 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
 
-                    Map<String, Map<String, ServiceInstance>> namespaceToServices = tracker.namespaceToServiceNameToRefNameToInstance.get(namespace);
-                    if (namespaceToServices == null) {
-                        namespaceToServices = Maps.newHashMap();
-                        tracker.namespaceToServiceNameToRefNameToInstance.put(namespace, namespaceToServices);
-                    }
+                    Map<String, Map<String, ServiceInstance>> namespaceToServices =
+                            tracker.namespaceToServiceNameToRefNameToInstance.computeIfAbsent(namespace,
+                                    k -> new HashMap<>());
 
                     Map<String, ServiceInstance> refNameToInstance = namespaceToServices
 
                     Map<String, ServiceInstance> refNameToInstance = namespaceToServices
-                            .get(serviceName);
-                    if (refNameToInstance == null) {
-                        refNameToInstance = Maps.newHashMap();
-                        namespaceToServices.put(serviceName, refNameToInstance);
-                    }
+                            .computeIfAbsent(serviceName, k -> new HashMap<>());
 
                     String refName = refEntry.getKey();
                     //we want to compare reference not value of the provider
 
                     String refName = refEntry.getKey();
                     //we want to compare reference not value of the provider
@@ -86,7 +80,7 @@ public final class Services {
     // TODO support edit strategies on services
 
     public static Services fromXml(final XmlElement xml) throws DocumentedException {
     // TODO support edit strategies on services
 
     public static Services fromXml(final XmlElement xml) throws DocumentedException {
-        Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
+        Map<String, Map<String, Map<String, String>>> retVal = new HashMap<>();
 
         List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
         xml.checkUnrecognisedElements(services);
 
         List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
         xml.checkUnrecognisedElements(services);
@@ -98,20 +92,13 @@ public final class Services {
 
             Preconditions.checkState(prefixNamespace.getKey()!=null && !prefixNamespace.getKey().equals(""), "Type attribute was not prefixed");
 
 
             Preconditions.checkState(prefixNamespace.getKey()!=null && !prefixNamespace.getKey().equals(""), "Type attribute was not prefixed");
 
-            Map<String, Map<String, String>> namespaceToServices = retVal.get(prefixNamespace.getValue());
-            if(namespaceToServices == null) {
-                namespaceToServices = Maps.newHashMap();
-                retVal.put(prefixNamespace.getValue(), namespaceToServices);
-            }
+            Map<String, Map<String, String>> namespaceToServices =
+                    retVal.computeIfAbsent(prefixNamespace.getValue(), k -> new HashMap<>());
 
             String serviceName =  ObjectNameAttributeReadingStrategy
                 .checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
 
 
             String serviceName =  ObjectNameAttributeReadingStrategy
                 .checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
 
-            Map<String, String> innerMap = namespaceToServices.get(serviceName);
-            if (innerMap == null) {
-                innerMap = Maps.newHashMap();
-                namespaceToServices.put(serviceName, innerMap);
-            }
+            Map<String, String> innerMap = namespaceToServices.computeIfAbsent(serviceName, k -> new HashMap<>());
 
             List<XmlElement> instances = service.getChildElements(XmlMappingConstants.INSTANCE_KEY);
             service.checkUnrecognisedElements(instances, typeElement);
 
             List<XmlElement> instances = service.getChildElements(XmlMappingConstants.INSTANCE_KEY);
             service.checkUnrecognisedElements(instances, typeElement);
index 6c26bf1bf80598ff49d999792a7ee96ddda1cccb..8ce71a1dc5ab08d8be87bfca70e0f646fc8a9617 100644 (file)
@@ -9,7 +9,7 @@
 package org.opendaylight.controller.config.facade.xml.rpc;
 
 import com.google.common.base.Preconditions;
 package org.opendaylight.controller.config.facade.xml.rpc;
 
 import com.google.common.base.Preconditions;
-import com.google.common.collect.Maps;
+import java.util.HashMap;
 import java.util.Map;
 import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver;
 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
 import java.util.Map;
 import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver;
 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
@@ -17,8 +17,8 @@ import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc;
 
 public final class ModuleRpcs {
 
 
 public final class ModuleRpcs {
 
-    private final Map<String, String> yangToJavaNames = Maps.newHashMap();
-    private final Map<String, Map<String, InstanceRuntimeRpc>> rpcMapping = Maps.newHashMap();
+    private final Map<String, String> yangToJavaNames = new HashMap<>();
+    private final Map<String, Map<String, InstanceRuntimeRpc>> rpcMapping = new HashMap<>();
     private final EnumResolver enumResolver;
 
     public ModuleRpcs(final EnumResolver enumResolver) {
     private final EnumResolver enumResolver;
 
     public ModuleRpcs(final EnumResolver enumResolver) {
@@ -34,11 +34,7 @@ public final class ModuleRpcs {
 
     public void addRpc(RuntimeBeanEntry runtimeEntry, Rpc rpc) {
         String yangName = runtimeEntry.getYangName();
 
     public void addRpc(RuntimeBeanEntry runtimeEntry, Rpc rpc) {
         String yangName = runtimeEntry.getYangName();
-        Map<String, InstanceRuntimeRpc> map = rpcMapping.get(yangName);
-        if (map == null) {
-            map = Maps.newHashMap();
-            rpcMapping.put(yangName, map);
-        }
+        Map<String, InstanceRuntimeRpc> map = rpcMapping.computeIfAbsent(yangName, k -> new HashMap<>());
 
         Preconditions.checkState(!map.containsKey(rpc.getYangName()), "Rpc %s for runtime bean %s added twice",
                 rpc.getYangName(), yangName);
 
         Preconditions.checkState(!map.containsKey(rpc.getYangName()), "Rpc %s for runtime bean %s added twice",
                 rpc.getYangName(), yangName);
index 93127d0b1f88acb205237c948e7a273026840ebe..f58a9d8d37c48a6376e266975c323c035bcb0ae6 100644 (file)
@@ -46,11 +46,7 @@ public class Runtime {
         for (ObjectName objectName : instancesToMap) {
             String moduleName = ObjectNameUtil.getFactoryName(objectName);
 
         for (ObjectName objectName : instancesToMap) {
             String moduleName = ObjectNameUtil.getFactoryName(objectName);
 
-            Multimap<String, ObjectName> multimap = retVal.get(moduleName);
-            if (multimap == null) {
-                multimap = HashMultimap.create();
-                retVal.put(moduleName, multimap);
-            }
+            Multimap<String, ObjectName> multimap = retVal.computeIfAbsent(moduleName, k -> HashMultimap.create());
 
             String instanceName = ObjectNameUtil.getInstanceName(objectName);
 
 
             String instanceName = ObjectNameUtil.getInstanceName(objectName);
 
index baa02edf3c355411cd2561a264dd2ba90a940c82..2b7e2622ba213847e9ec88544b4cdf19f019bb6b 100644 (file)
@@ -89,11 +89,8 @@ public class ServiceReferenceRegistryImpl implements CloseableServiceReferenceRe
         Map<String /* namespace */, Map<String /* localName */, ServiceInterfaceAnnotation>> modifiableNamespacesToAnnotations = new HashMap<>();
         Map<String /* service qName*/, ServiceInterfaceAnnotation> modifiableServiceQNamesToAnnotations = new HashMap<>();
         for (ServiceInterfaceAnnotation sia : allAnnotations) {
         Map<String /* namespace */, Map<String /* localName */, ServiceInterfaceAnnotation>> modifiableNamespacesToAnnotations = new HashMap<>();
         Map<String /* service qName*/, ServiceInterfaceAnnotation> modifiableServiceQNamesToAnnotations = new HashMap<>();
         for (ServiceInterfaceAnnotation sia : allAnnotations) {
-            Map<String, ServiceInterfaceAnnotation> ofNamespace = modifiableNamespacesToAnnotations.get(sia.namespace());
-            if (ofNamespace == null) {
-                ofNamespace = new HashMap<>();
-                modifiableNamespacesToAnnotations.put(sia.namespace(), ofNamespace);
-            }
+            Map<String, ServiceInterfaceAnnotation> ofNamespace =
+                    modifiableNamespacesToAnnotations.computeIfAbsent(sia.namespace(), k -> new HashMap<>());
             if (ofNamespace.containsKey(sia.localName())) {
                 LOG.error(
                         "Cannot construct namespacesToAnnotations map, conflict between local names in {}, offending local name: {}, map so far {}",
             if (ofNamespace.containsKey(sia.localName())) {
                 LOG.error(
                         "Cannot construct namespacesToAnnotations map, conflict between local names in {}, offending local name: {}, map so far {}",
@@ -290,11 +287,7 @@ public class ServiceReferenceRegistryImpl implements CloseableServiceReferenceRe
         Map<String /* serviceInterfaceName */, Map<String/* refName */, ObjectName>> result = new HashMap<>();
         for (Entry<ServiceReference, ModuleIdentifier> entry: refNames.entrySet()) {
             String qName = entry.getKey().getServiceInterfaceQName();
         Map<String /* serviceInterfaceName */, Map<String/* refName */, ObjectName>> result = new HashMap<>();
         for (Entry<ServiceReference, ModuleIdentifier> entry: refNames.entrySet()) {
             String qName = entry.getKey().getServiceInterfaceQName();
-            Map<String /* refName */, ObjectName> innerMap = result.get(qName);
-            if (innerMap == null) {
-                innerMap = new HashMap<>();
-                result.put(qName, innerMap);
-            }
+            Map<String /* refName */, ObjectName> innerMap = result.computeIfAbsent(qName, k -> new HashMap<>());
             innerMap.put(entry.getKey().getRefName(), getObjectName(entry.getValue()));
         }
         return result;
             innerMap.put(entry.getKey().getRefName(), getObjectName(entry.getValue()));
         }
         return result;
@@ -421,11 +414,8 @@ public class ServiceReferenceRegistryImpl implements CloseableServiceReferenceRe
         }
         // save to refNames
         refNames.put(serviceReference, moduleIdentifier);
         }
         // save to refNames
         refNames.put(serviceReference, moduleIdentifier);
-        Map<ServiceInterfaceAnnotation, String /* service ref name */> refNamesToAnnotations = modulesToServiceRef.get(moduleIdentifier);
-        if (refNamesToAnnotations == null){
-            refNamesToAnnotations = new HashMap<>();
-            modulesToServiceRef.put(moduleIdentifier, refNamesToAnnotations);
-        }
+        Map<ServiceInterfaceAnnotation, String /* service ref name */> refNamesToAnnotations =
+                modulesToServiceRef.computeIfAbsent(moduleIdentifier, k -> new HashMap<>());
 
         ServiceInterfaceAnnotation annotation = serviceQNamesToAnnotations.get(serviceReference.getServiceInterfaceQName());
         Preconditions.checkNotNull(annotation, "Possible error in code, cannot find annotation for " + serviceReference);
 
         ServiceInterfaceAnnotation annotation = serviceQNamesToAnnotations.get(serviceReference.getServiceInterfaceQName());
         Preconditions.checkNotNull(annotation, "Possible error in code, cannot find annotation for " + serviceReference);
index c17c29c0fd1846ff0ad76a5299fe5bb10cd8c68e..9964ceb675081d88fdb5ced38e87933d3b9c16f6 100644 (file)
@@ -12,12 +12,12 @@ import akka.persistence.AtomicWrite;
 import akka.persistence.PersistentImpl;
 import akka.persistence.PersistentRepr;
 import akka.persistence.journal.japi.AsyncWriteJournal;
 import akka.persistence.PersistentImpl;
 import akka.persistence.PersistentRepr;
 import akka.persistence.journal.japi.AsyncWriteJournal;
-import com.google.common.collect.Maps;
 import com.google.common.util.concurrent.Uninterruptibles;
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
 import com.google.common.util.concurrent.Uninterruptibles;
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -62,11 +62,7 @@ public class InMemoryJournal extends AsyncWriteJournal {
     }
 
     public static void addEntry(String persistenceId, long sequenceNr, Object data) {
     }
 
     public static void addEntry(String persistenceId, long sequenceNr, Object data) {
-        Map<Long, Object> journal = JOURNALS.get(persistenceId);
-        if (journal == null) {
-            journal = Maps.newLinkedHashMap();
-            JOURNALS.put(persistenceId, journal);
-        }
+        Map<Long, Object> journal = JOURNALS.computeIfAbsent(persistenceId, k -> new LinkedHashMap<>());
 
         synchronized (journal) {
             journal.put(sequenceNr, data instanceof Serializable
 
         synchronized (journal) {
             journal.put(sequenceNr, data instanceof Serializable
index 49aec66e724c3f45471d6e213d35cbd4b9d0ae4e..699a5e3daf95a2ccabff8e2f3e5b6195578329be 100644 (file)
@@ -42,12 +42,7 @@ public class InMemorySnapshotStore extends SnapshotStore {
     private static Map<String, List<StoredSnapshot>> snapshots = new ConcurrentHashMap<>();
 
     public static void addSnapshot(String persistentId, Object snapshot) {
     private static Map<String, List<StoredSnapshot>> snapshots = new ConcurrentHashMap<>();
 
     public static void addSnapshot(String persistentId, Object snapshot) {
-        List<StoredSnapshot> snapshotList = snapshots.get(persistentId);
-
-        if (snapshotList == null) {
-            snapshotList = new ArrayList<>();
-            snapshots.put(persistentId, snapshotList);
-        }
+        List<StoredSnapshot> snapshotList = snapshots.computeIfAbsent(persistentId, k -> new ArrayList<>());
 
         synchronized (snapshotList) {
             snapshotList.add(new StoredSnapshot(new SnapshotMetadata(persistentId, snapshotList.size(),
 
         synchronized (snapshotList) {
             snapshotList.add(new StoredSnapshot(new SnapshotMetadata(persistentId, snapshotList.size(),
index 2920f45fe93d198b935fd50690a97ffb0cfd342c..a37dcc6ef90ee086231ce6f5ce4c20af3cb598d7 100644 (file)
@@ -73,11 +73,7 @@ public class FileModuleShardConfigProvider implements ModuleShardConfigProvider
             ConfigObjectWrapper wrapper = new ConfigObjectWrapper(o);
 
             String moduleName = wrapper.stringValue("name");
             ConfigObjectWrapper wrapper = new ConfigObjectWrapper(o);
 
             String moduleName = wrapper.stringValue("name");
-            ModuleConfig.Builder builder = moduleConfigMap.get(moduleName);
-            if (builder == null) {
-                builder = ModuleConfig.builder(moduleName);
-                moduleConfigMap.put(moduleName, builder);
-            }
+            ModuleConfig.Builder builder = moduleConfigMap.computeIfAbsent(moduleName, ModuleConfig::builder);
 
             builder.nameSpace(wrapper.stringValue("namespace"));
             builder.shardStrategy(ShardStrategyFactory.newShardStrategyInstance(moduleName,
 
             builder.nameSpace(wrapper.stringValue("namespace"));
             builder.shardStrategy(ShardStrategyFactory.newShardStrategyInstance(moduleName,
index 2fb9cf1cda0663db408e740f6ff94e66e621e307..06bb712fd87df254aceca283615c7283ca4ce749 100644 (file)
@@ -8,7 +8,6 @@
 
 package org.opendaylight.controller.cluster.datastore.utils;
 
 
 package org.opendaylight.controller.cluster.datastore.utils;
 
-import java.util.Map;
 import org.opendaylight.controller.cluster.access.concepts.MemberName;
 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
 import org.opendaylight.controller.cluster.access.concepts.MemberName;
 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
@@ -80,10 +79,9 @@ public class ClusterUtils {
             builder.append(p.getNodeType().getLocalName());
             if (p instanceof NodeIdentifierWithPredicates) {
                 builder.append("-key_");
             builder.append(p.getNodeType().getLocalName());
             if (p instanceof NodeIdentifierWithPredicates) {
                 builder.append("-key_");
-                final Map<QName, Object> key = ((NodeIdentifierWithPredicates) p).getKeyValues();
-                key.entrySet().forEach(e -> {
-                    builder.append(e.getKey().getLocalName());
-                    builder.append(e.getValue());
+                ((NodeIdentifierWithPredicates) p).getKeyValues().forEach((key, value) -> {
+                    builder.append(key.getLocalName());
+                    builder.append(value);
                     builder.append("-");
                 });
                 builder.append("_");
                     builder.append("-");
                 });
                 builder.append("_");
index fb55b3a299def2cacc5a259bda690290ac270a5e..31bc8b6949156662cecdc9a62f116458b589eaad 100644 (file)
@@ -108,11 +108,8 @@ class DistributedShardFrontend implements ReadableWriteableDOMDataTreeShard {
                     continue;
                 }
 
                     continue;
                 }
 
-                SubshardProducerSpecification spec = affectedSubshards.get(maybeAffected.getPrefix());
-                if (spec == null) {
-                    spec = new SubshardProducerSpecification(maybeAffected);
-                    affectedSubshards.put(maybeAffected.getPrefix(), spec);
-                }
+                SubshardProducerSpecification spec = affectedSubshards.computeIfAbsent(maybeAffected.getPrefix(),
+                    k -> new SubshardProducerSpecification(maybeAffected));
                 spec.addPrefix(bindPath);
             }
         }
                 spec.addPrefix(bindPath);
             }
         }
index 638e58553583dda2b476252777b533d975862591..cd9f7bd37991bb03ba7861a73371bde03badc4c8 100644 (file)
@@ -45,11 +45,7 @@ public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTree
 
     @GuardedBy("this")
     private void storeShard(final DOMDataTreeIdentifier prefix, final ShardRegistration<?> reg) {
 
     @GuardedBy("this")
     private void storeShard(final DOMDataTreeIdentifier prefix, final ShardRegistration<?> reg) {
-        ShardingTableEntry t = shardingTables.get(prefix.getDatastoreType());
-        if (t == null) {
-            t = new ShardingTableEntry();
-            shardingTables.put(prefix.getDatastoreType(), t);
-        }
+        ShardingTableEntry t = shardingTables.computeIfAbsent(prefix.getDatastoreType(), k -> new ShardingTableEntry());
 
         t.store(prefix.getRootIdentifier(), reg);
     }
 
         t.store(prefix.getRootIdentifier(), reg);
     }
index fcd0ebdca065076f6b62c7603a7ef67405164ef6..1fdbb6d2e42e18f6173485c994a25d22ff9651dd 100644 (file)
@@ -64,11 +64,7 @@ final class ShardingTableEntry implements Identifiable<PathArgument> {
 
         while (it.hasNext()) {
             final PathArgument a = it.next();
 
         while (it.hasNext()) {
             final PathArgument a = it.next();
-            ShardingTableEntry child = entry.children.get(a);
-            if (child == null) {
-                child = new ShardingTableEntry(a);
-                entry.children.put(a, child);
-            }
+            ShardingTableEntry child = entry.children.computeIfAbsent(a, ShardingTableEntry::new);
         }
 
         Preconditions.checkState(entry.registration == null);
         }
 
         Preconditions.checkState(entry.registration == null);