001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: /*
035: * Created on May 18, 2005
036: */
037: package com.icesoft.faces.context;
038:
039: import java.util.AbstractCollection;
040: import java.util.AbstractSet;
041: import java.util.ArrayList;
042: import java.util.Collection;
043: import java.util.Enumeration;
044: import java.util.Iterator;
045: import java.util.List;
046: import java.util.Map;
047: import java.util.NoSuchElementException;
048: import java.util.Set;
049:
050: public abstract class AbstractAttributeMap implements Map {
051:
052: private Collection values;
053: private Set keySet;
054: private Set entrySet;
055:
056: /* (
057: * @see java.util.Map#size()
058: */
059: public int size() {
060: int size = 0;
061: Enumeration e = getAttributeNames();
062: while (e.hasMoreElements()) {
063: e.nextElement();
064: size++;
065: }
066: return size;
067: }
068:
069: /*
070: * @see java.util.Map#clear()
071: */
072: public void clear() {
073: Enumeration e = getAttributeNames();
074: List keys = new ArrayList();
075: while (e.hasMoreElements())
076: keys.add(e.nextElement());
077:
078: Iterator iterator = keys.iterator();
079: while (iterator.hasNext())
080: removeAttribute((String) iterator.next());
081: }
082:
083: /*
084: * @see java.util.Map#isEmpty()
085: */
086: public boolean isEmpty() {
087: return !getAttributeNames().hasMoreElements();
088: }
089:
090: /* (non-Javadoc)
091: * @see java.util.Map#containsKey(java.lang.Object)
092: */
093: public boolean containsKey(Object key) {
094: return getAttribute(key.toString()) != null;
095: }
096:
097: /* (non-Javadoc)
098: * @see java.util.Map#containsValue(java.lang.Object)
099: */
100: public boolean containsValue(Object value) {
101: if (value == null)
102: return false;
103:
104: Enumeration e = getAttributeNames();
105: while (e.hasMoreElements()) {
106: if (value.equals(getAttribute(e.nextElement().toString()))) {
107: return true;
108: }
109: }
110: return false;
111: }
112:
113: /* (non-Javadoc)
114: * @see java.util.Map#values()
115: */
116: public Collection values() {
117: return (values != null) ? values : (values = new Values());
118: }
119:
120: /* (non-Javadoc)
121: * @see java.util.Map#putAll(java.util.Map)
122: */
123: public void putAll(Map map) {
124: Iterator iterator = map.entrySet().iterator();
125: while (iterator.hasNext()) {
126: Entry entry = (Entry) iterator.next();
127: setAttribute(entry.getKey().toString(), entry.getValue());
128: }
129: }
130:
131: /* (non-Javadoc)
132: * @see java.util.Map#entrySet()
133: */
134: public Set entrySet() {
135: return (entrySet != null) ? entrySet
136: : (entrySet = new EntrySet());
137: }
138:
139: /* (non-Javadoc)
140: * @see java.util.Map#keySet()
141: */
142: public Set keySet() {
143: return (keySet != null) ? keySet : (keySet = new KeySet());
144: }
145:
146: /* (non-Javadoc)
147: * @see java.util.Map#get(java.lang.Object)
148: */
149: public Object get(Object key) {
150: return getAttribute(key.toString());
151: }
152:
153: /*
154: * @see java.util.Map#remove(java.lang.Object)
155: */
156: public Object remove(Object key) {
157: Object associatedValue = getAttribute(key.toString());
158: removeAttribute(key.toString());
159: return associatedValue;
160: }
161:
162: /* (non-Javadoc)
163: * @see java.util.Map#put(java.lang.Object, java.lang.Object)
164: */
165: public Object put(Object key, Object value) {
166: Object previousValue = getAttribute(key.toString());
167: setAttribute(key.toString(), value);
168: return previousValue;
169: }
170:
171: abstract protected Object getAttribute(String key);
172:
173: abstract protected void setAttribute(String key, Object value);
174:
175: abstract protected void removeAttribute(String key);
176:
177: abstract protected Enumeration getAttributeNames();
178:
179: private class KeySet extends AbstractSet {
180:
181: /*
182: * @see java.util.AbstractCollection#iterator()
183: */
184: public Iterator iterator() {
185: return new KeyIterator();
186: }
187:
188: /*
189: * @see java.util.AbstractCollection#size()
190: */
191: public int size() {
192: return AbstractAttributeMap.this .size();
193: }
194:
195: public boolean isEmpty() {
196: return AbstractAttributeMap.this .isEmpty();
197: }
198:
199: public boolean contains(Object key) {
200: return AbstractAttributeMap.this .containsKey(key);
201: }
202:
203: public boolean remove(Object key) {
204: return AbstractAttributeMap.this .remove(key) != null;
205: }
206:
207: public void clear() {
208: AbstractAttributeMap.this .clear();
209: }
210: }
211:
212: private class KeyIterator implements Iterator {
213:
214: protected final Enumeration e = getAttributeNames();
215: protected Object currentKey;
216:
217: /*
218: * @see java.util.Iterator#hasNext()
219: */
220: public boolean hasNext() {
221: return e.hasMoreElements();
222: }
223:
224: /*
225: * @see java.util.Iterator#next()
226: */
227: public Object next() {
228: return currentKey = e.nextElement();
229: }
230:
231: /*
232: * @see java.util.Iterator#remove()
233: */
234: public void remove() {
235: if (currentKey == null) {
236: throw new NoSuchElementException(
237: "No element is pointed for remove operation");
238: }
239: AbstractAttributeMap.this .remove(currentKey);
240: }
241: }
242:
243: private class Values extends AbstractCollection {
244:
245: public Iterator iterator() {
246: return new ValuesIterator();
247: }
248:
249: public boolean contains(Object value) {
250: return containsValue(value);
251: }
252:
253: public boolean remove(Object value) {
254: if (value == null)
255: return false;
256:
257: Iterator iterator = iterator();
258: while (iterator.hasNext()) {
259: if (value.equals(iterator.next())) {
260: iterator.remove();
261: return true;
262: }
263: }
264: return false;
265: }
266:
267: public int size() {
268: return AbstractAttributeMap.this .size();
269: }
270:
271: public boolean isEmpty() {
272: return AbstractAttributeMap.this .isEmpty();
273: }
274:
275: public void clear() {
276: AbstractAttributeMap.this .clear();
277: }
278: }
279:
280: private class ValuesIterator extends KeyIterator {
281: public Object next() {
282: super .next();
283: return AbstractAttributeMap.this .get(currentKey);
284: }
285: }
286:
287: private class EntrySet extends KeySet {
288: public Iterator iterator() {
289: return new EntryIterator();
290: }
291:
292: public boolean contains(Object object) {
293: if (object == null)
294: return false;
295:
296: if (!(object instanceof Entry))
297: return false;
298:
299: Entry entry = (Entry) object;
300: Object key = entry.getKey();
301: Object value = entry.getValue();
302:
303: if (key == null || value == null)
304: return false;
305:
306: return value.equals(AbstractAttributeMap.this .get(key));
307: }
308:
309: public boolean remove(Object object) {
310: if (object == null)
311: return false;
312:
313: if (!(object instanceof Entry))
314: return false;
315:
316: Entry entry = (Entry) object;
317: Object key = entry.getKey();
318: Object value = entry.getValue();
319: if (!value.equals(AbstractAttributeMap.this .get(key))
320: || key == null || value == null)
321: return false;
322:
323: return AbstractAttributeMap.this .remove(key) != null;
324: }
325: }
326:
327: private class EntryIterator extends KeyIterator {
328: public Object next() {
329: super .next();
330: return new EntrySetEntry(currentKey);
331: }
332: }
333:
334: private class EntrySetEntry implements Entry {
335: private final Object currentKey;
336:
337: public EntrySetEntry(Object currentKey) {
338: this .currentKey = currentKey;
339: }
340:
341: public Object getKey() {
342: return currentKey;
343: }
344:
345: public Object getValue() {
346: return AbstractAttributeMap.this .get(currentKey);
347: }
348:
349: public Object setValue(Object value) {
350: return AbstractAttributeMap.this.put(currentKey, value);
351: }
352: }
353: }
|