]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Fix adding components to configurationBySolverName map in MappingBase 33/3433/2
authorjsimomaa <jani.simomaa@gmail.com>
Thu, 31 Oct 2019 07:27:35 +0000 (09:27 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 31 Oct 2019 10:48:42 +0000 (12:48 +0200)
The map is now invalidated (= null) when components are added which
means the map will be recalculated next time when retrieved with
getConfigurationBySolverName.

gitlab #402

Change-Id: I8f9436ddd62d86b7b73f98325cf7b67f0fb97cea

bundles/org.simantics.structural.synchronization/src/org/simantics/structural/synchronization/utils/MappingBase.java

index e50d8498d8d0f048084a95c19edae6f7abd64041..7fd78189fe82c22ece853be8c892c08d9d770494 100644 (file)
@@ -1,14 +1,18 @@
 package org.simantics.structural.synchronization.utils;
 
+import java.io.PrintWriter;
+import java.util.Collections;
+import java.util.Map;
+import java.util.function.Consumer;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import gnu.trove.map.hash.THashMap;
 import gnu.trove.procedure.TObjectObjectProcedure;
 import gnu.trove.procedure.TObjectProcedure;
 import gnu.trove.set.hash.THashSet;
 
-import java.io.PrintWriter;
-import java.util.Map;
-import java.util.function.Consumer;
-
 /**
  * The entry point to the mapping structure between Simantics database and a
  * designated solver. It is used to synchronize changes from Simantics to the
@@ -18,6 +22,8 @@ import java.util.function.Consumer;
  */
 abstract public class MappingBase<T extends ComponentBase<T>> {
 
+    private final transient Logger LOGGER = LoggerFactory.getLogger(getClass());
+    
        abstract public T getConfiguration();
     
     /**
@@ -98,8 +104,13 @@ abstract public class MappingBase<T extends ComponentBase<T>> {
 
     public Map<String, T> getConfigurationBySolverName() {
         Map<String, T> result = configurationBySolverName;
-        if (result == null)
-            result = configurationBySolverName = createConfigurationBySolverName(getConfiguration());
+        if (result == null) {
+            T configuration = getConfiguration();
+            if (configuration != null)
+                result = configurationBySolverName = createConfigurationBySolverName(configuration);
+            else
+                result = Collections.emptyMap();
+        }
         return result;
     }
 
@@ -112,7 +123,11 @@ abstract public class MappingBase<T extends ComponentBase<T>> {
     private void browseConfigurationBySolverName(
             THashMap<String, T> configurationBySolverName,
             T configuration) {
-        configurationBySolverName.put(configuration.solverComponentName, configuration);
+        if (configuration.solverComponentName != null) {
+            configurationBySolverName.put(configuration.solverComponentName, configuration);
+        } else if (configuration.componentId != 0) {
+            LOGGER.warn("configuration.solverComponentName is null! configuration uid is {} and component id {}", configuration.getUid(), configuration.componentId);
+        }
         for(T child : configuration.getChildren()) {
             browseConfigurationBySolverName(configurationBySolverName, child);
             child.parent = configuration;
@@ -124,6 +139,7 @@ abstract public class MappingBase<T extends ComponentBase<T>> {
         if(result == null) {
             result = componentFactory.create(uid);
             configurationByUid.put(uid, result);
+            configurationBySolverName = null; // forces recalculation
         }
         else {
             if(result.getParent() == null)
@@ -170,7 +186,7 @@ abstract public class MappingBase<T extends ComponentBase<T>> {
     public void remove(final Solver solver, T component) {
         if(configurationByUid != null)
             configurationByUid.remove(component.uid);
-        if (configurationBySolverName != null)
+        if (configurationBySolverName != null && component.solverComponentName != null)
             configurationBySolverName.remove(component.solverComponentName);
         if(component.getChildMap() != null)
             component.getChildMap().forEachValue(new TObjectProcedure<T>() {