]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/unification/UMapUtils.java
Added missing parts from SVN org.simantics.root project.
[simantics/platform.git] / bundles / org.simantics.scl.runtime / src / org / simantics / scl / runtime / unification / UMapUtils.java
1 package org.simantics.scl.runtime.unification;
2
3 import gnu.trove.map.hash.THashMap;
4
5 import java.util.Map;
6
7 import org.simantics.scl.runtime.function.Function;
8 import org.simantics.scl.runtime.tuple.Tuple0;
9
10 public class UMapUtils {
11     public static void put(Map<Object,Object> map, Object key, Object value) {
12         if(map.containsKey(key))
13             Unification.unify(map.get(key), value);
14         else
15             map.put(key, value);
16     }
17     
18     @SuppressWarnings({ "rawtypes", "unchecked" })
19     public static Object get(final Function def, THashMap<Object,Object> map, Object key) {
20         try {
21             Object value = map.get(key);
22             if(value == null) {
23                 if(!map.containsKey(key)) {
24                     value = def.apply(Tuple0.INSTANCE);
25                     map.put(key, value);
26                 }
27                 return value;
28             }
29             else {
30                 Object extrValue = Unification.extractWithDefault(def, value);
31                 if(extrValue != value)
32                     map.put(key, extrValue);
33                 return extrValue;
34             }
35         } catch(RuntimeException e) {
36             throw new RuntimeException("While generating value for key " + key + ".", e);
37         }
38     }
39 }