1 package fi.vtt.simantics.procore.internal;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
6 import java.util.Iterator;
10 import org.simantics.db.ObjectResourceIdMap;
11 import org.simantics.db.Resource;
12 import org.simantics.db.exception.ResourceNotFoundException;
13 import org.simantics.db.impl.ResourceImpl;
15 import gnu.trove.impl.Constants;
16 import gnu.trove.map.hash.TObjectIntHashMap;
17 import gnu.trove.procedure.TObjectIntProcedure;
18 import gnu.trove.procedure.TObjectProcedure;
20 final class ObjectResourceMap<T> implements Map<T, Resource>, ObjectResourceIdMap<T> {
22 private final SessionImplSocket session;
23 private final TObjectIntHashMap<T> backend;
25 ObjectResourceMap(SessionImplSocket session) {
26 this.session = session;
27 backend = new TObjectIntHashMap<>(Constants.DEFAULT_CAPACITY, Constants.DEFAULT_LOAD_FACTOR, 0);
30 ObjectResourceMap(SessionImplSocket session, int capacity) {
31 this.session = session;
32 backend = new TObjectIntHashMap<>(capacity, Constants.DEFAULT_LOAD_FACTOR, 0);
37 return backend.size();
40 public boolean isEmpty() {
41 return backend.isEmpty();
45 public boolean containsKey(Object key) {
46 return backend.contains(key);
50 public boolean containsValue(Object value) {
51 ResourceImpl impl = (ResourceImpl) value;
52 return backend.containsValue(impl.id);
56 public Resource get(Object key) {
58 int result = backend.get(key);
61 return session.getResourceByKey(result);
62 } catch (ResourceNotFoundException e) {
69 public Resource put(T key, Resource value) {
70 ResourceImpl impl = (ResourceImpl) value;
71 int i = backend.put(key, impl.id);
76 return session.getResourceByKey(i);
77 } catch (ResourceNotFoundException e) {
84 public Resource remove(Object key) {
85 throw new UnsupportedOperationException("remove not supported, structure is immutable");
89 public void putAll(Map<? extends T, ? extends Resource> map) {
90 @SuppressWarnings("unchecked")
91 ObjectResourceMap<T> other = (ObjectResourceMap<T>) map;
92 other.backend.forEachEntry(new TObjectIntProcedure<T>() {
95 public boolean execute(T a, int b) {
103 public void clear() {
104 throw new UnsupportedOperationException("clear not supported, structure is immutable");
108 public Set<T> keySet() {
109 final Set<T> result = new HashSet<>();
110 backend.forEach(new TObjectProcedure<T>() {
113 public boolean execute(T object) {
122 public Collection<Resource> values() {
123 ArrayList<Resource> result = new ArrayList<>();
124 for (int key : backend.values()) {
126 result.add(session.getResourceByKey(key));
127 } catch (ResourceNotFoundException e) {
135 public Set<java.util.Map.Entry<T, Resource>> entrySet() {
136 final HashSet<java.util.Map.Entry<T, Resource>> result = new HashSet<>();
137 backend.forEachEntry(new TObjectIntProcedure<T>() {
140 public boolean execute(final T a, final int b) {
141 return result.add(new Map.Entry<T, Resource>() {
149 public Resource getValue() {
150 return new ResourceImpl(session.resourceSupport, b);
154 public Resource setValue(Resource value) {
155 throw new UnsupportedOperationException("Map.Entry.setValue not supported, structure is immutable");
165 public int hashCode() {
166 return backend.hashCode();
170 public boolean equals(Object obj) {
175 if (getClass() != obj.getClass()) {
176 if (obj instanceof Map) {
177 // Nonoptimal fallback for comparing against generic Map
178 Map<?,?> m = (Map<?,?>) obj;
179 if (m.size() != size())
182 Iterator<Entry<T,Resource>> i = entrySet().iterator();
183 while (i.hasNext()) {
184 Entry<T,Resource> e = i.next();
186 Resource value = e.getValue();
188 if (!(m.get(key)==null && m.containsKey(key)))
191 if (!value.equals(m.get(key)))
196 } catch (ClassCastException unused) {
198 } catch (NullPointerException unused) {
204 ObjectResourceMap<?> other = (ObjectResourceMap<?>) obj;
205 return session == other.session && backend.equals(other.backend);
209 public void putId(T t, int r) {
214 public int getId(T t) {
215 return backend.get(t);