At last the EntityBatchingProducerNode is working. This will replace the old
[mir.git] / source / mir / entity / adapter / EntityAdapter.java
1 package mir.entity.adapter;
2
3 import java.util.*;
4 import mir.entity.*;
5
6 public class EntityAdapter implements Map {
7   private Entity entity;
8   private EntityAdapterDefinition definition;
9   private Map calculatedFieldsCache;
10
11   public EntityAdapter(Entity anEntity, EntityAdapterDefinition aDefinition) {
12     entity = anEntity;
13     definition = aDefinition;
14     calculatedFieldsCache = new HashMap();
15   }
16
17   public boolean containsKey(Object aKey) {
18     try {
19       if (aKey instanceof String)
20         return      entity.hasValueForField((String) aKey)
21                 || definition.hasCalculatedField((String) aKey)
22                 || entity.getFields().contains(aKey);
23     }
24     catch (Throwable t) {
25     }
26
27     return false;
28   }
29
30   public boolean equals(Object anObject) {
31     return        anObject instanceof EntityAdapter
32            && ((EntityAdapter) anObject).entity.equals(entity);
33   }
34
35   public int hashCode() {
36     return entity.hashCode();
37   }
38
39   public Entity getEntity() {
40     return entity;
41   }
42
43   public Object get(Object aKey) {
44     Object result;
45
46     if (calculatedFieldsCache.containsKey(aKey)) {
47       return calculatedFieldsCache.get(aKey);
48     }
49     else if (aKey instanceof String && definition.hasCalculatedField((String) aKey)) {
50       result = definition.getCalculatedField((String) aKey).getValue(this);
51       calculatedFieldsCache.put(aKey, result);
52
53       return result;
54     }
55     else if (aKey instanceof String) {
56       return entity.getValue((String) aKey);
57     }
58     else {
59       return null;
60     }
61   }
62
63   public boolean isEmpty() {
64     throw new UnsupportedOperationException("EntityAdapter.isEmpty()");
65   }
66
67   public Set keySet() {
68     throw new UnsupportedOperationException("EntityAdapter.keySet()");
69   }
70
71   public Object put(Object aKey, Object value) {
72     throw new UnsupportedOperationException("EntityAdapter.put()");
73   }
74
75   public void putAll(Map t) {
76     throw new UnsupportedOperationException("EntityAdapter.putAll()");
77   }
78
79   public Object remove(Object aKey) {
80     throw new UnsupportedOperationException("EntityAdapter.remove()");
81   }
82
83   public int size() {
84     throw new UnsupportedOperationException("EntityAdapter.size()");
85   }
86
87   public Collection values() {
88     throw new UnsupportedOperationException("EntityAdapter.values()");
89   }
90
91   public void clear() {
92     throw new UnsupportedOperationException("EntityAdapter.clear()");
93   }
94
95   public boolean containsValue(Object value) {
96     throw new UnsupportedOperationException("EntityAdapter.containsValue()");
97   }
98
99   public Set entrySet() {
100     throw new UnsupportedOperationException("EntityAdapter.entrySet()");
101   }
102 }