]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Add configurationBySolverName map to MappingBase 64/3364/1
authorjsimomaa <jani.simomaa@gmail.com>
Fri, 18 Oct 2019 07:05:39 +0000 (10:05 +0300)
committerjsimomaa <jani.simomaa@gmail.com>
Fri, 18 Oct 2019 07:05:39 +0000 (10:05 +0300)
gitlab #402

Cherry picked manually from I3a315e48a79efbcbca73408de8f0f172cf6d8344

Change-Id: I0e7ec025e0eb2e0e215661eb92eee8376b83408a

bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/MappingBase.java

index 78678b5054d95b2559ef8ff6f37bbc6f7b4650e0..aa1afd2f03c4d7227a246b0044e1da9bf61c8bee 100644 (file)
@@ -6,6 +6,7 @@ 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
@@ -22,6 +23,11 @@ abstract public class MappingBase<T extends ComponentBase<T>> {
      * Set of all components indexed by their UID.
      */
     transient protected THashMap<String, T> configurationByUid;
+
+    /**
+     * Set of all components indexed by their solver name.
+     */
+    transient protected Map<String, T> configurationBySolverName;
     
     /** 
      * Set of components whose removal is delayed because they might
@@ -89,6 +95,29 @@ 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());
+        return result;
+    }
+
+    protected Map<String, T> createConfigurationBySolverName(T configuration) {
+        THashMap<String, T> configurationBySolverName = new THashMap<>();
+        browseConfigurationBySolverName(configurationBySolverName, configuration);
+        return configurationBySolverName;
+    }
+
+    private void browseConfigurationBySolverName(
+            THashMap<String, T> configurationBySolverName,
+            T configuration) {
+        configurationBySolverName.put(configuration.solverComponentName, configuration);
+        for(T child : configuration.getChildren()) {
+            browseConfigurationBySolverName(configurationBySolverName, child);
+            child.parent = configuration;
+        }
+    }
+
     public T detachOrCreateComponent(String uid) {
         T result = configurationByUid.get(uid);
         if(result == null) {
@@ -140,6 +169,8 @@ 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)
+            configurationBySolverName.remove(component.solverComponentName);
         if(component.getChildMap() != null)
             component.getChildMap().forEachValue(new TObjectProcedure<T>() {
                 @Override
@@ -212,6 +243,10 @@ abstract public class MappingBase<T extends ComponentBase<T>> {
     public void dispose() {
         if (configurationByUid != null)
             configurationByUid.clear();
+        if (configurationBySolverName != null) {
+            configurationBySolverName.clear();
+            configurationBySolverName = null;
+        }
         pendingRemoval.clear();
     }