]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/ContextLocal.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / ContextLocal.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.datastructures;\r
13 \r
14 import java.util.concurrent.ConcurrentHashMap;\r
15 \r
16 import org.simantics.utils.datastructures.disposable.AbstractDisposable;\r
17 \r
18 /**\r
19  * This class provides context-local variables. There is a different value for \r
20  * each context. This class handles disposing of all values if optional\r
21  * disposer is provided.  \r
22  *\r
23  * @param <T>\r
24  */\r
25 public class ContextLocal<T> extends AbstractDisposable {\r
26         \r
27         protected ConcurrentHashMap<Object, T> reg = new ConcurrentHashMap<Object, T>();\r
28         \r
29         public static interface Disposer<T> {\r
30                 public void dispose(T object);\r
31         }\r
32         \r
33         Disposer<T> disposer;\r
34         \r
35         /**\r
36          * Creates context-local variable container.\r
37          */\r
38         public ContextLocal() \r
39         {               \r
40         }\r
41         \r
42         /**\r
43          * Create context-local variable container.\r
44          * Container created with this method takes ownership of its object.\r
45          * \r
46          * @param disposed\r
47          */\r
48         public ContextLocal(Disposer<T> disposer)\r
49         {\r
50                 this.disposer = disposer;\r
51         }\r
52         \r
53         /**\r
54          * Gets the object associated in the specified context\r
55          * \r
56          * @param context\r
57          * @return associated Object or <code>null</code> if not found\r
58          */\r
59         public T get(Object context) {\r
60                 return reg.get(context);\r
61         }\r
62 \r
63         /**\r
64          * Associates the specified Object with the specified context.\r
65          * \r
66          * @param context\r
67          * @param monitor\r
68          */\r
69         public void replace(Object context, T value) {\r
70                 T old = value != null ? reg.put(context, value) : reg.remove(context);\r
71                 if (old!=null && disposer!=null)\r
72                         disposer.dispose(old);\r
73         }\r
74         \r
75         public void remove(Object context) {\r
76                 replace(context, null);\r
77         }\r
78 \r
79         @Override\r
80         protected void doDispose() {\r
81                 if (disposer!=null)\r
82                         for (T lr : reg.values()) {\r
83                                 disposer.dispose(lr);\r
84                         }\r
85                 reg.clear();\r
86         }\r
87 \r
88 \r
89 }\r
90 \r