yet another rewrite of the producers...
[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     if (aKey instanceof String)
19       return      entity.hasValueForField((String) aKey)
20               || definition.hasCalculatedField((String) aKey);
21     else
22       return false;
23   }
24
25   public boolean equals(Object anObject) {
26     return        anObject instanceof EntityAdapter
27            && ((EntityAdapter) anObject).entity.equals(entity);
28   }
29
30   public int hashCode() {
31     return entity.hashCode();
32   }
33
34   protected Entity getEntity() {
35     return entity;
36   }
37
38   public Object get(Object aKey) {
39     Object result;
40
41     if (calculatedFieldsCache.containsKey(aKey)) {
42       return calculatedFieldsCache.get(aKey);
43     }
44     else if (aKey instanceof String && definition.hasCalculatedField((String) aKey)) {
45       result = definition.getCalculatedField((String) aKey).getValue(this);
46       calculatedFieldsCache.put(aKey, result);
47
48       return result;
49     }
50     else if (aKey instanceof String) {
51       return entity.getValue((String) aKey);
52     }
53     else {
54       return null;
55     }
56   }
57
58   public boolean isEmpty() {
59     throw new UnsupportedOperationException("EntityAdapter.isEmpty()");
60   }
61
62   public Set keySet() {
63     throw new UnsupportedOperationException("EntityAdapter.keySet()");
64   }
65
66   public Object put(Object aKey, Object value) {
67     throw new UnsupportedOperationException("EntityAdapter.put()");
68   }
69
70   public void putAll(Map t) {
71     throw new UnsupportedOperationException("EntityAdapter.putAll()");
72   }
73
74   public Object remove(Object aKey) {
75     throw new UnsupportedOperationException("EntityAdapter.remove()");
76   }
77
78   public int size() {
79     throw new UnsupportedOperationException("EntityAdapter.size()");
80   }
81
82   public Collection values() {
83     throw new UnsupportedOperationException("EntityAdapter.values()");
84   }
85
86   public void clear() {
87     throw new UnsupportedOperationException("EntityAdapter.clear()");
88   }
89
90   public boolean containsValue(Object value) {
91     throw new UnsupportedOperationException("EntityAdapter.containsValue()");
92   }
93
94   public Set entrySet() {
95     throw new UnsupportedOperationException("EntityAdapter.entrySet()");
96   }
97 }