]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Fix adding components to configurationBySolverName map in MappingBase 34/3434/1
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 11:24:01 +0000 (13:24 +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.client/src/org/simantics/structural/synchronization/base/MappingBase.java

index aa1afd2f03c4d7227a246b0044e1da9bf61c8bee..0714a1b11373fcffa49186690f2c6324fb14cbae 100644 (file)
@@ -1,13 +1,18 @@
 package org.simantics.structural.synchronization.base;
 
+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;
-
 /**
  * The entry point to the mapping structure between Simantics database and a
  * designated solver. It is used to synchronize changes from Simantics to the
@@ -17,6 +22,8 @@ import java.util.Map;
  */
 abstract public class MappingBase<T extends ComponentBase<T>> {
 
+    private final transient Logger LOGGER = LoggerFactory.getLogger(getClass());
+    
        abstract public T getConfiguration();
     
     /**
@@ -97,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;
     }
 
@@ -111,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;
@@ -123,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)
@@ -169,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>() {
@@ -254,4 +271,11 @@ abstract public class MappingBase<T extends ComponentBase<T>> {
         return !pendingRemoval.isEmpty();
     }
 
+    public void forEachPendingRemoval(Consumer<T> consumer) {
+        pendingRemoval.forEach(c -> {
+            consumer.accept(c);
+            return true;
+        });
+    }
+
 }