]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/RandomAccessValueSupportImpl.java
Merge commit 'd7afa23'
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / RandomAccessValueSupportImpl.java
1 package fi.vtt.simantics.procore.internal;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.Collections;\r
6 import java.util.HashMap;\r
7 import java.util.Map;\r
8 \r
9 import org.simantics.db.Disposable;\r
10 import org.simantics.db.Resource;\r
11 import org.simantics.db.impl.internal.RandomAccessValueSupport;\r
12 import org.simantics.db.impl.internal.ResourceData;\r
13 import org.simantics.utils.datastructures.Pair;\r
14 \r
15 /**\r
16  * A simple implementation of {@link RandomAccessValueSupport}.\r
17  * \r
18  * @author Tuukka Lehtonen\r
19  */\r
20 public class RandomAccessValueSupportImpl implements RandomAccessValueSupport {\r
21 \r
22     private final static boolean DEBUG = false;\r
23 \r
24     /**\r
25      * The map of services maintained by the workbench window. These services\r
26      * are initialized during workbench window during the\r
27      * {@link #configureShell(Shell)}. This value is <code>null</code> until\r
28      * a service is registered.\r
29      */\r
30     private Map<Resource, ResourceData> values = new HashMap<Resource, ResourceData>();\r
31 \r
32     /**\r
33      * Constructs a new random access value registry.\r
34      */\r
35     public RandomAccessValueSupportImpl() {\r
36     }\r
37 \r
38     @Override\r
39     public ResourceData get(Resource resource) {\r
40         synchronized (this) {\r
41             return values.get(resource);\r
42         }\r
43     }\r
44 \r
45     /**\r
46      * Registers a service with this locator. If there is an existing service\r
47      * matching the same <code>api</code> and it implements\r
48      * {@link Disposable}, it will be disposed.\r
49      * \r
50      * @param api\r
51      *            This is the interface that the service implements. Must not be\r
52      *            <code>null</code>.\r
53      * @param service\r
54      *            The service to register. This must be some implementation of\r
55      *            <code>api</code>. This value must not be <code>null</code>.\r
56      */\r
57     public void put(final Resource resource, final ResourceData data) {\r
58         synchronized (this) {\r
59             final ResourceData current = values.get(resource);\r
60             if (current != null)\r
61                 throw new IllegalStateException(\r
62                         "Value already registered for resource " + resource\r
63                         + ": " + current + ". Tried to register "\r
64                         + data);\r
65 \r
66             if (DEBUG)\r
67                 System.out.println(this + ": register " + resource + " -> " + data);\r
68 \r
69             values.put(resource, data);\r
70         }\r
71     }\r
72 \r
73     @Override\r
74     public Collection<Pair<Resource, ResourceData>> entries() {\r
75         synchronized (this) {\r
76             if (values.isEmpty())\r
77                 return Collections.emptyList();\r
78             Collection<Pair<Resource, ResourceData>> result = new ArrayList<Pair<Resource, ResourceData>>( values.size() );\r
79             for (Map.Entry<Resource, ResourceData> entry : values.entrySet()) {\r
80                 result.add( Pair.make( entry.getKey(), entry.getValue() ) );\r
81             }\r
82             return result;\r
83         }\r
84     }\r
85 \r
86     @Override\r
87     public Collection<Pair<Resource, ResourceData>> removeAll() {\r
88         synchronized (this) {\r
89             if (values.isEmpty())\r
90                 return Collections.emptyList();\r
91             if (DEBUG)\r
92                 System.out.println(this + ": remove " + values.size() + " registrations");\r
93             Collection<Pair<Resource, ResourceData>> result = new ArrayList<Pair<Resource, ResourceData>>( values.size() );\r
94             for (Map.Entry<Resource, ResourceData> entry : values.entrySet()) {\r
95                 result.add( Pair.make( entry.getKey(), entry.getValue() ) );\r
96             }\r
97             values.clear();\r
98             return result;\r
99         }\r
100     }\r
101 \r
102 }\r