001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v1.identicator;
024:
025: import java.util.*;
026: import com.mchange.v1.util.*;
027:
028: /*
029: * Implementation notes: many AbstractMap methods are written in
030: * terms of entrySet(). It is most important to get that right.
031: */
032: abstract class IdMap extends AbstractMap implements Map {
033: Map inner;
034: Identicator id;
035:
036: protected IdMap(Map inner, Identicator id) {
037: this .inner = inner;
038: this .id = id;
039: }
040:
041: public Object put(Object key, Object value) {
042: return inner.put(createIdKey(key), value);
043: }
044:
045: public boolean containsKey(Object key) {
046: return inner.containsKey(createIdKey(key));
047: }
048:
049: public Object get(Object key) {
050: return inner.get(createIdKey(key));
051: }
052:
053: public Object remove(Object key) {
054: return inner.remove(createIdKey(key));
055: }
056:
057: protected Object removeIdHashKey(IdHashKey idhk) {
058: return inner.remove(idhk);
059: }
060:
061: public Set entrySet() {
062: return new UserEntrySet();
063: }
064:
065: protected final Set internalEntrySet() {
066: return inner.entrySet();
067: }
068:
069: protected abstract IdHashKey createIdKey(Object o);
070:
071: protected final Entry createIdEntry(Object key, Object val) {
072: return new SimpleMapEntry(createIdKey(key), val);
073: }
074:
075: protected final Entry createIdEntry(Entry entry) {
076: return createIdEntry(entry.getKey(), entry.getValue());
077: }
078:
079: private final class UserEntrySet extends AbstractSet {
080: Set innerEntries = inner.entrySet();
081:
082: public Iterator iterator() {
083: return new WrapperIterator(innerEntries.iterator(), true) {
084: protected Object transformObject(Object o) {
085: return new UserEntry((Entry) o);
086: }
087: };
088: }
089:
090: public int size() {
091: return innerEntries.size();
092: }
093:
094: public boolean contains(Object o) {
095: if (o instanceof Entry) {
096: Entry entry = (Entry) o;
097: return innerEntries.contains(createIdEntry(entry));
098: } else
099: return false;
100: }
101:
102: public boolean remove(Object o) {
103: if (o instanceof Entry) {
104: Entry entry = (Entry) o;
105: return innerEntries.remove(createIdEntry(entry));
106: } else
107: return false;
108: }
109:
110: public void clear() {
111: inner.clear();
112: }
113: }
114:
115: protected static class UserEntry extends AbstractMapEntry {
116: private Entry innerEntry;
117:
118: UserEntry(Entry innerEntry) {
119: this .innerEntry = innerEntry;
120: }
121:
122: public final Object getKey() {
123: return ((IdHashKey) innerEntry.getKey()).getKeyObj();
124: }
125:
126: public final Object getValue() {
127: return innerEntry.getValue();
128: }
129:
130: public final Object setValue(Object value) {
131: return innerEntry.setValue(value);
132: }
133: }
134: }
|