1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.morph.context.support;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import net.sf.composite.util.ObjectUtils;
28 import net.sf.morph.context.Context;
29 import net.sf.morph.context.DelegatingContext;
30 import net.sf.morph.util.ContainerUtils;
31
32 /**
33 * A bridge between the Context and Map APIs; this class can be used to
34 * implement the methods in the Map interface.
35 *
36 * @author Matt Sgarlata
37 * @since Nov 22, 2004
38 */
39 public class ContextMapBridge {
40
41 protected Object getDelegate(Context context) {
42 return context instanceof DelegatingContext
43 ? ((DelegatingContext) context).getDelegate() : null;
44 }
45
46 public int size(Context context) {
47 return getPropertyNames(context).length;
48 }
49
50 public void clear(Context context) {
51 Object delegate = getDelegate(context);
52 if (!(delegate instanceof Map)) {
53 throw new UnsupportedOperationException();
54 }
55 ((Map) delegate).clear();
56 }
57
58 public boolean isEmpty(Context context) {
59 return ObjectUtils.isEmpty(getPropertyNames(context));
60 }
61
62 public boolean containsKey(Context context, Object key) {
63 return ContainerUtils.contains(getPropertyNames(context), key);
64 }
65
66 public boolean containsValue(Context context, Object value) {
67 String[] propertyNames = getPropertyNames(context);
68 if (ObjectUtils.isEmpty(propertyNames)) {
69 return false;
70 }
71 for (int i=0; i<propertyNames.length; i++) {
72 if (ObjectUtils.equals(context.get(propertyNames[i]), value)) {
73 return true;
74 }
75 }
76 return false;
77 }
78
79 public Collection values(Context context) {
80 String[] propertyNames = getPropertyNames(context);
81 if (ObjectUtils.isEmpty(propertyNames)) {
82 return Collections.EMPTY_LIST;
83 }
84 List values = new ArrayList(propertyNames.length);
85 for (int i=0; i<propertyNames.length; i++) {
86 values.add(context.get(propertyNames[i]));
87 }
88 return values;
89 }
90
91 public void putAll(Context context, Map t) {
92 checkContextNotNull(context);
93 if (t == null || t.isEmpty()) {
94 return;
95 }
96 for (Iterator it = t.entrySet().iterator(); it.hasNext();) {
97 Map.Entry e = (Map.Entry) it.next();
98 put(context, e.getKey(), e.getValue());
99 }
100 }
101
102 public Set entrySet(Context context) {
103 String[] propertyNames = getPropertyNames(context);
104 if (ObjectUtils.isEmpty(propertyNames)) {
105 return Collections.EMPTY_SET;
106 }
107 Set set = ContainerUtils.createOrderedSet();
108 for (int i=0; i<propertyNames.length; i++) {
109 set.add(new MapEntry(propertyNames[i],
110 context.get(propertyNames[i]), false));
111 }
112 return set;
113 }
114
115 public Set keySet(Context context) {
116 String[] propertyNames = getPropertyNames(context);
117 if (ObjectUtils.isEmpty(propertyNames)) {
118 return Collections.EMPTY_SET;
119 }
120 else {
121 Set s = ContainerUtils.createOrderedSet();
122 s.addAll(Arrays.asList(propertyNames));
123 return s;
124 }
125 }
126
127 public Object get(Context context, Object key) {
128 return checkContextNotNull(context).get((String) key);
129 }
130
131 public Object remove(Context context, Object key) {
132 Object delegate = getDelegate(context);
133 if (delegate instanceof Map) {
134 return ((Map) delegate).remove(key);
135 }
136 throw new UnsupportedOperationException();
137 }
138
139 public Object put(Context context, Object key, Object value) {
140 if (!(key instanceof String)) {
141 throw new IllegalArgumentException("Only string keys can be used");
142 }
143 Object originalValue = checkContextNotNull(context).get((String) key);
144 context.set((String) key, value);
145 return originalValue;
146 }
147
148 protected Context checkContextNotNull(Context context)
149 throws IllegalArgumentException {
150 if (context == null) {
151 throw new IllegalArgumentException("context cannot be null");
152 }
153 return context;
154 }
155
156 /**
157 * Convenience method
158 * @param context non-null Context
159 * @return propertyNames
160 * @since Morph 1.1
161 */
162 protected String[] getPropertyNames(Context context) {
163 return checkContextNotNull(context).getPropertyNames();
164 }
165 }