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