From 89abd1b2ffb448c1291051907d23ab2d66661f10 Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Thu, 31 Oct 2019 09:27:35 +0200 Subject: [PATCH] Fix adding components to configurationBySolverName map in MappingBase 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 --- .../synchronization/utils/MappingBase.java | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/bundles/org.simantics.structural.synchronization/src/org/simantics/structural/synchronization/utils/MappingBase.java b/bundles/org.simantics.structural.synchronization/src/org/simantics/structural/synchronization/utils/MappingBase.java index e50d8498d..7fd78189f 100644 --- a/bundles/org.simantics.structural.synchronization/src/org/simantics/structural/synchronization/utils/MappingBase.java +++ b/bundles/org.simantics.structural.synchronization/src/org/simantics/structural/synchronization/utils/MappingBase.java @@ -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> { + private final transient Logger LOGGER = LoggerFactory.getLogger(getClass()); + abstract public T getConfiguration(); /** @@ -98,8 +104,13 @@ abstract public class MappingBase> { public Map getConfigurationBySolverName() { Map 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> { private void browseConfigurationBySolverName( THashMap 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> { 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> { 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() { -- 2.43.2