001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: MapAdapter.java,v 1.6 2005/02/16 11:28:13 jesper Exp $
023: package net.infonode.util.collection.map;
024:
025: import net.infonode.util.collection.map.base.ConstMapIterator;
026: import net.infonode.util.collection.map.base.Map;
027: import net.infonode.util.collection.map.base.MapIterator;
028:
029: import java.util.HashMap;
030:
031: public class MapAdapter implements Map {
032: private static class Iterator implements MapIterator {
033: private java.util.Iterator iterator;
034: private java.util.Map.Entry nextEntry;
035:
036: Iterator(java.util.Iterator iterator) {
037: this .iterator = iterator;
038: next();
039: }
040:
041: public void remove() {
042: iterator.remove();
043: }
044:
045: public boolean atEntry() {
046: return nextEntry != null;
047: }
048:
049: public Object getKey() {
050: return nextEntry.getKey();
051: }
052:
053: public Object getValue() {
054: return nextEntry.getValue();
055: }
056:
057: public void next() {
058: nextEntry = iterator.hasNext() ? (java.util.Map.Entry) iterator
059: .next()
060: : null;
061: }
062: }
063:
064: private HashMap map;
065:
066: public MapAdapter() {
067: }
068:
069: public MapAdapter(HashMap map) {
070: this .map = map;
071: }
072:
073: public Object put(Object key, Object value) {
074: if (map == null)
075: map = new HashMap(4);
076:
077: return map.put(key, value);
078: }
079:
080: public Object remove(Object key) {
081: return map == null ? null : map.remove(key);
082: }
083:
084: public void clear() {
085: if (map != null)
086: map.clear();
087: }
088:
089: public MapIterator iterator() {
090: return map == null ? (MapIterator) EmptyIterator.INSTANCE
091: : (MapIterator) new Iterator(map.entrySet().iterator());
092: }
093:
094: public Object get(Object key) {
095: return map == null ? null : map.get(key);
096: }
097:
098: public boolean containsKey(Object key) {
099: return map != null && map.containsKey(key);
100: }
101:
102: public boolean containsValue(Object value) {
103: return map != null && map.containsValue(value);
104: }
105:
106: public boolean isEmpty() {
107: return map == null || map.isEmpty();
108: }
109:
110: public ConstMapIterator constIterator() {
111: return iterator();
112: }
113:
114: public int size() {
115: return map == null ? 0 : map.size();
116: }
117: }
|