1 package fi.vtt.simantics.procore.internal;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
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;
16 * A simple implementation of {@link RandomAccessValueSupport}.
18 * @author Tuukka Lehtonen
20 public class RandomAccessValueSupportImpl implements RandomAccessValueSupport {
22 private final static boolean DEBUG = false;
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.
30 private Map<Resource, ResourceData> values = new HashMap<Resource, ResourceData>();
33 * Constructs a new random access value registry.
35 public RandomAccessValueSupportImpl() {
39 public ResourceData get(Resource resource) {
41 return values.get(resource);
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.
51 * This is the interface that the service implements. Must not be
54 * The service to register. This must be some implementation of
55 * <code>api</code>. This value must not be <code>null</code>.
57 public void put(final Resource resource, final ResourceData data) {
59 final ResourceData current = values.get(resource);
61 throw new IllegalStateException(
62 "Value already registered for resource " + resource
63 + ": " + current + ". Tried to register "
67 System.out.println(this + ": register " + resource + " -> " + data);
69 values.put(resource, data);
74 public Collection<Pair<Resource, ResourceData>> entries() {
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() ) );
87 public Collection<Pair<Resource, ResourceData>> removeAll() {
90 return Collections.emptyList();
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() ) );