]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/SCLContext.java
8703819776a95f521109b36eadbbbcec847ca6f8
[simantics/platform.git] / bundles / org.simantics.scl.runtime / src / org / simantics / scl / runtime / SCLContext.java
1 package org.simantics.scl.runtime;
2
3 import gnu.trove.map.hash.THashMap;
4
5 public class SCLContext extends THashMap<String,Object> {
6     private static ThreadLocal<SCLContext> CONTEXT = new ThreadLocal<SCLContext>();
7     private static ThreadLocal<OldContextNode> OLD_CONTEXT = new ThreadLocal<OldContextNode>();
8     
9     private static class OldContextNode {
10         final SCLContext context;
11         final OldContextNode next;
12         
13         public OldContextNode(SCLContext context, OldContextNode next) {
14             this.context = context;
15             this.next = next;
16         }
17     }
18     
19     public static SCLContext getCurrent() {
20         SCLContext context = CONTEXT.get();
21         if(context == null) {
22             context = new SCLContext();
23             CONTEXT.set(context);
24         }
25         return context;
26     }
27     
28     public static void push(SCLContext newContext) {
29         SCLContext oldContext = CONTEXT.get();
30         if(oldContext != null)
31             OLD_CONTEXT.set(new OldContextNode(oldContext, OLD_CONTEXT.get()));
32         CONTEXT.set(newContext);
33     }
34     
35     public static void pop() {
36         OldContextNode node = OLD_CONTEXT.get();
37         if(node == null)
38             CONTEXT.set(null);
39         else {
40             CONTEXT.set(node.context);
41             OLD_CONTEXT.set(node.next);
42         }
43     }
44     
45     /*
46     @Override
47     public Object get(Object key) {
48         Object result = super.get(key);
49         System.out.println(this + ": " + key + " -> " + result);
50         return result;
51     }
52     
53     @Override
54     public Object put(String key, Object value) {
55         System.out.println(this + ": " + key + " <- " + value);
56         return super.put(key, value);
57     }*/
58 }