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