001: /* StringKeysMap.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue Dec 6 22:35:32 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.web.servlet.xel;
020:
021: import java.util.Map;
022: import java.util.AbstractMap;
023: import java.util.Iterator;
024: import java.util.Enumeration;
025:
026: /**
027: * A sketetal implementation for Map to wrap something with enumeration of
028: * keys, which must be String.
029: *
030: * @author tomyeh
031: * @since 3.0.0
032: */
033: public abstract class StringKeysMap extends AbstractMap {
034: //-- Map --//
035: public boolean containsKey(Object key) {
036: return (key instanceof String)
037: && getValue((String) key) != null;
038: }
039:
040: public Object get(Object key) {
041: return key instanceof String ? getValue((String) key) : null;
042: }
043:
044: /** Returns the value associated with the specified key. */
045: abstract protected Object getValue(String key);
046:
047: /** Returns an enumeration of keys. */
048: abstract protected Enumeration getKeys();
049:
050: /** Sets the value associated with the specified key. */
051: abstract protected void setValue(String key, Object value);
052:
053: /** Removes the specified key. */
054: abstract protected void removeValue(String key);
055:
056: private class Entry implements Map.Entry {
057: private final String _key;
058:
059: private Entry(String key) {
060: _key = key;
061: }
062:
063: public boolean equals(Object o) {
064: return (o instanceof Entry)
065: && _key.equals(((Entry) o)._key);
066: }
067:
068: public int hashCode() {
069: return _key.hashCode();
070: }
071:
072: public Object getKey() {
073: return _key;
074: }
075:
076: public Object getValue() {
077: return StringKeysMap.this .getValue(_key);
078: }
079:
080: public Object setValue(Object value) {
081: final Object old = getValue();
082: StringKeysMap.this .setValue(_key, value);
083: return old;
084: }
085: }
086:
087: /** The iterator class used to iterator the entries in this map.
088: */
089: public class EntryIter implements Iterator {
090: private final Enumeration _keys = getKeys();
091: private String _key;
092:
093: public boolean hasNext() {
094: return _keys.hasMoreElements();
095: }
096:
097: public Object next() {
098: _key = (String) _keys.nextElement();
099: return new Entry(_key);
100: }
101:
102: public void remove() {
103: StringKeysMap.this.removeValue(_key);
104: }
105: }
106: }
|