1 package fi.vtt.simantics.procore.internal;
\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
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
16 * A simple implementation of {@link RandomAccessValueSupport}.
\r
18 * @author Tuukka Lehtonen
\r
20 public class RandomAccessValueSupportImpl implements RandomAccessValueSupport {
\r
22 private final static boolean DEBUG = false;
\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
30 private Map<Resource, ResourceData> values = new HashMap<Resource, ResourceData>();
\r
33 * Constructs a new random access value registry.
\r
35 public RandomAccessValueSupportImpl() {
\r
39 public ResourceData get(Resource resource) {
\r
40 synchronized (this) {
\r
41 return values.get(resource);
\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
51 * This is the interface that the service implements. Must not be
\r
52 * <code>null</code>.
\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
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
67 System.out.println(this + ": register " + resource + " -> " + data);
\r
69 values.put(resource, data);
\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
87 public Collection<Pair<Resource, ResourceData>> removeAll() {
\r
88 synchronized (this) {
\r
89 if (values.isEmpty())
\r
90 return Collections.emptyList();
\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