001: package org.obe.spi.util;
002:
003: import org.obe.client.api.repository.RepositoryException;
004: import org.obe.engine.util.Comparators;
005: import org.obe.spi.model.AttributeInstance;
006: import org.obe.spi.model.AttributeReadOnlyException;
007: import org.obe.spi.model.AttributedEntity;
008: import org.obe.xpdl.model.data.Type;
009:
010: import java.util.*;
011:
012: /**
013: * Provides keyed access to attribute values by adapting an
014: * {@link AttributedEntity}. Instances of this class are maps whose keys are
015: * attribute names and whose values are the corresponding attribute values.
016: *
017: * @author Adrian Price
018: */
019: public class AttributeValueMap implements Map {
020: private final Map _delegate;
021: private Set _entries;
022:
023: private static class AttributeValueEntry implements Map.Entry {
024: private final Map.Entry _delegate;
025:
026: AttributeValueEntry(Map.Entry delegate) {
027: _delegate = delegate;
028: }
029:
030: public int hashCode() {
031: return _delegate.hashCode();
032: }
033:
034: public Object getKey() {
035: return _delegate.getKey();
036: }
037:
038: public Object getValue() {
039: return ((AttributeInstance) _delegate.getValue())
040: .getValue();
041: }
042:
043: public boolean equals(Object obj) {
044: return _delegate.equals(obj);
045: }
046:
047: public Object setValue(Object value) {
048: AttributeInstance attrInst = (AttributeInstance) _delegate
049: .getValue();
050: Object oldValue = attrInst.getValue();
051: try {
052: attrInst.setValue(Type.DEFAULT_TYPE, value);
053: } catch (AttributeReadOnlyException e) {
054: throw new UnsupportedOperationException(e.getMessage());
055: }
056: return oldValue;
057: }
058: }
059:
060: private static class AttributeValueEntrySet implements Set {
061: private final Set _delegate;
062:
063: private AttributeValueEntrySet(Set delegate) {
064: _delegate = delegate;
065: }
066:
067: public int hashCode() {
068: return _delegate.hashCode();
069: }
070:
071: public int size() {
072: return _delegate.size();
073: }
074:
075: public void clear() {
076: _delegate.clear();
077: }
078:
079: public boolean isEmpty() {
080: return _delegate.isEmpty();
081: }
082:
083: public Object[] toArray() {
084: Object[] a = _delegate.toArray();
085: for (int i = 0; i < a.length; i++) {
086: a[i] = new AttributeValueEntry((Entry) a[i]);
087: }
088: return a;
089: }
090:
091: public Object[] toArray(Object[] a) {
092: a = _delegate.toArray(a);
093: for (int i = 0; i < a.length; i++) {
094: a[i] = new AttributeValueEntry((Entry) a[i]);
095: }
096: return a;
097: }
098:
099: public boolean add(Object o) {
100: return _delegate.add(o);
101: }
102:
103: public boolean contains(Object o) {
104: return _delegate.contains(o);
105: }
106:
107: public boolean equals(Object obj) {
108: return _delegate.equals(obj);
109: }
110:
111: public boolean remove(Object o) {
112: return _delegate.remove(o);
113: }
114:
115: public boolean addAll(Collection coll) {
116: return _delegate.addAll(coll);
117: }
118:
119: public boolean containsAll(Collection coll) {
120: return _delegate.containsAll(coll);
121: }
122:
123: public boolean removeAll(Collection coll) {
124: return _delegate.removeAll(coll);
125: }
126:
127: public boolean retainAll(Collection coll) {
128: return _delegate.retainAll(coll);
129: }
130:
131: public Iterator iterator() {
132: return new AttributeValueEntrySetIterator(_delegate
133: .iterator());
134: }
135: }
136:
137: private static class AttributeValueEntrySetIterator implements
138: Iterator {
139: private final Iterator _delegate;
140:
141: AttributeValueEntrySetIterator(Iterator delegate) {
142: _delegate = delegate;
143: }
144:
145: public void remove() {
146: _delegate.remove();
147: }
148:
149: public boolean hasNext() {
150: return _delegate.hasNext();
151: }
152:
153: public Object next() {
154: return new AttributeValueEntry((Entry) _delegate.next());
155: }
156: }
157:
158: public AttributeValueMap(AttributedEntity entity)
159: throws RepositoryException {
160:
161: _delegate = entity.getAttributeInstances();
162: }
163:
164: public int hashCode() {
165: return _delegate.hashCode();
166: }
167:
168: public int size() {
169: return _delegate.size();
170: }
171:
172: public void clear() {
173: _delegate.clear();
174: }
175:
176: public boolean isEmpty() {
177: return _delegate.isEmpty();
178: }
179:
180: public boolean containsKey(Object key) {
181: return _delegate.containsKey(key);
182: }
183:
184: public boolean containsValue(Object value) {
185: Collection values = _delegate.values();
186: for (Iterator iter = values.iterator(); iter.hasNext();) {
187: AttributeInstance attrInst = (AttributeInstance) iter
188: .next();
189: if (Comparators.equals(attrInst.getValue(), value)) {
190: return true;
191: }
192: }
193: return false;
194: }
195:
196: public boolean equals(Object obj) {
197: return _delegate.equals(obj);
198: }
199:
200: public Collection values() {
201: Collection attrs = _delegate.values();
202: Collection values = new ArrayList(attrs.size());
203: for (Iterator iter = attrs.iterator(); iter.hasNext();) {
204: AttributeInstance attrInst = (AttributeInstance) iter
205: .next();
206: values.add(attrInst.getValue());
207: }
208: return values;
209: }
210:
211: public void putAll(Map t) {
212: _delegate.putAll(t);
213: }
214:
215: public Set entrySet() {
216: if (_entries == null) {
217: Set entries = _delegate.entrySet();
218: _entries = new AttributeValueEntrySet(entries);
219: }
220: return _entries;
221: }
222:
223: public Set keySet() {
224: return _delegate.keySet();
225: }
226:
227: public Object get(Object key) {
228: AttributeInstance attrInst = (AttributeInstance) _delegate
229: .get(key);
230: return attrInst == null ? null : attrInst.getValue();
231: }
232:
233: public Object remove(Object key) {
234: return _delegate.remove(key);
235: }
236:
237: public Object put(Object key, Object value) {
238: return _delegate.put(key, value);
239: }
240: }
|