From: jsimomaa Date: Thu, 31 Oct 2019 07:27:35 +0000 (+0200) Subject: Fix adding components to configurationBySolverName map in MappingBase X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=728669b1b89ec60d4257752dbccae3b486318abe;p=simantics%2Fplatform.git 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 --- diff --git a/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/MappingBase.java b/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/MappingBase.java index aa1afd2f0..0714a1b11 100644 --- a/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/MappingBase.java +++ b/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/MappingBase.java @@ -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> { + private final transient Logger LOGGER = LoggerFactory.getLogger(getClass()); + abstract public T getConfiguration(); /** @@ -97,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; } @@ -111,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; @@ -123,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) @@ -169,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() { @@ -254,4 +271,11 @@ abstract public class MappingBase> { return !pendingRemoval.isEmpty(); } + public void forEachPendingRemoval(Consumer consumer) { + pendingRemoval.forEach(c -> { + consumer.accept(c); + return true; + }); + } + }