]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/LazyModule.java
Merge "Initialize new cache in flush instead of setting it null"
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / module / LazyModule.java
index ce3b7cc5fd22ff49fd4b2a01c5205d3647c2f82e..dc1e40e7321e3097d84be7c439aeb7a5a0c10cb6 100644 (file)
@@ -2,9 +2,9 @@ package org.simantics.scl.compiler.module;
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.function.Function;
+import java.util.Map;
 
 import org.simantics.scl.compiler.constants.Constant;
 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
@@ -28,9 +28,9 @@ import gnu.trove.map.hash.THashMap;
 public abstract class LazyModule implements Module {
 
     private final String moduleName;
-    private final ConcurrentHashMap<String, SCLValue> values = new ConcurrentHashMap<String, SCLValue>();
-    private final ConcurrentHashMap<String, SCLRelation> relations = new ConcurrentHashMap<String, SCLRelation>();
-    private final ConcurrentHashMap<String, SCLEntityType> entityTypes = new ConcurrentHashMap<String, SCLEntityType>();
+    private final Map<String, SCLValue> values = Collections.synchronizedMap(new HashMap<String, SCLValue>());
+    private final Map<String, SCLRelation> relations = Collections.synchronizedMap(new HashMap<String, SCLRelation>());
+    private final Map<String, SCLEntityType> entityTypes = Collections.synchronizedMap(new HashMap<String, SCLEntityType>());
     
     protected abstract SCLValue createValue(String name);
     
@@ -64,11 +64,15 @@ public abstract class LazyModule implements Module {
     public void findValuesForPrefix(final Collection<SCLValue> values, final String prefix) {          
     }
 
-    private final Function<String, SCLValue> createValue = this::createValue;
-    
     @Override
     public SCLValue getValue(String name) {
-        return values.computeIfAbsent(name, createValue);
+        if(values.containsKey(name))
+            return values.get(name);
+        else {
+            SCLValue value = createValue(name);
+            values.put(name, value);
+            return value;
+        }
     }
     
     @Override
@@ -76,16 +80,24 @@ public abstract class LazyModule implements Module {
         return null;
     }
     
-    private final Function<String, SCLRelation> createRelation = this::createRelation;
-    
     public SCLRelation getRelation(String name) {
-        return relations.computeIfAbsent(name, createRelation);
+        if(relations.containsKey(name))
+            return relations.get(name);
+        else {
+            SCLRelation relation = createRelation(name);
+            relations.put(name, relation);
+            return relation;
+        }
     }
     
-    private final Function<String, SCLEntityType> createEntityType = this::createEntityType;
-    
     public SCLEntityType getEntityType(String name) {
-        return entityTypes.computeIfAbsent(name, createEntityType);
+        if(entityTypes.containsKey(name))
+            return entityTypes.get(name);
+        else {
+            SCLEntityType entityType = createEntityType(name);
+            entityTypes.put(name, entityType);
+            return entityType;
+        }
     } 
 
     @Override