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 f1c9092..1d528e5 100644 (file)
@@ -43,11 +43,7 @@ public class ValidationException extends Exception {
                     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 "
index 781bda5..2b4e633 100644 (file)
@@ -9,10 +9,10 @@
 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 java.util.HashMap;
 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) {
-        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();
-            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();
 
-            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);
@@ -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) {
 
-        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()) {
@@ -312,11 +306,9 @@ public class ConfigSubsystemFacade implements Closeable {
                 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);
             }
@@ -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) {
-        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()) {
 
-            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<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()));
@@ -365,7 +357,7 @@ public class ConfigSubsystemFacade implements Closeable {
     }
 
     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));
         }
@@ -374,7 +366,7 @@ public class ConfigSubsystemFacade implements Closeable {
     }
 
     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());
         }
index dd4bd5b..37043eb 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.collect.Maps;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
 import javax.management.ObjectName;
 import javax.management.openmbean.OpenType;
@@ -48,23 +49,17 @@ public class RpcFacade {
 
     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()) {
 
-            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()) {
 
-                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();
 
@@ -108,7 +103,7 @@ public class RpcFacade {
 
     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();
index 21b74da..d40bfe1 100644 (file)
@@ -220,11 +220,7 @@ public class Config {
 
         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);
index b9e5f0f..eab120a 100644 (file)
@@ -9,7 +9,7 @@
 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;
@@ -64,7 +64,7 @@ public class ServiceRegistryWrapper {
     }
 
     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()){
@@ -75,18 +75,11 @@ public class ServiceRegistryWrapper {
 
                 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();
-                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,
index 4ee1bca..9853bc7 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.collect.Maps;
+import java.util.HashMap;
 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";
 
-    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()) {
 
-                    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
-                            .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
@@ -86,7 +80,7 @@ public final class Services {
     // 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);
@@ -98,20 +92,13 @@ public final class Services {
 
             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);
 
-            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);
index 6c26bf1..8ce71a1 100644 (file)
@@ -9,7 +9,7 @@
 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;
@@ -17,8 +17,8 @@ import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry.Rpc;
 
 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) {
@@ -34,11 +34,7 @@ public final class ModuleRpcs {
 
     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);
index 93127d0..f58a9d8 100644 (file)
@@ -46,11 +46,7 @@ public class Runtime {
         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);
 
index baa02ed..2b7e262 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, 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 {}",
@@ -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 /* 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;
@@ -421,11 +414,8 @@ public class ServiceReferenceRegistryImpl implements CloseableServiceReferenceRe
         }
         // 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);
index c17c29c..9964ceb 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 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 java.util.LinkedHashMap;
 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) {
-        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
index 49aec66..699a5e3 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) {
-        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(),
index 2920f45..a37dcc6 100644 (file)
@@ -73,11 +73,7 @@ public class FileModuleShardConfigProvider implements ModuleShardConfigProvider
             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,
index 2fb9cf1..06bb712 100644 (file)
@@ -8,7 +8,6 @@
 
 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;
@@ -80,10 +79,9 @@ public class ClusterUtils {
             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("_");
index fb55b3a..31bc8b6 100644 (file)
@@ -108,11 +108,8 @@ class DistributedShardFrontend implements ReadableWriteableDOMDataTreeShard {
                     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);
             }
         }
index 638e585..cd9f7bd 100644 (file)
@@ -45,11 +45,7 @@ public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTree
 
     @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);
     }
index fcd0ebd..1fdbb6d 100644 (file)
@@ -64,11 +64,7 @@ final class ShardingTableEntry implements Identifiable<PathArgument> {
 
         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);

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.