001: package org.apache.ojb.broker.util;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: /**
022: * replacement for the JDK1.4 version
023: * User: Matthew Baird
024: * Date: Jul 8, 2003
025: * Time: 8:37:00 AM
026: */
027: public final class IdentityHashMap extends HashMap {
028: private static final class IdentityKey {
029: private final Object m_key;
030:
031: public IdentityKey(final Object key) {
032: m_key = key;
033: }
034:
035: public boolean equals(final Object o) {
036: return (o == m_key);
037: }
038:
039: public int hashCode() {
040: return System.identityHashCode(m_key);
041: }
042: }
043:
044: /**
045: * Constructor for IdentityHashMap.
046: * @param initialCapacity
047: * @param loadFactor
048: */
049: public IdentityHashMap(final int initialCapacity,
050: final float loadFactor) {
051: super (initialCapacity, loadFactor);
052: }
053:
054: /**
055: * Constructor for IdentityHashMap.
056: * @param initialCapacity
057: */
058: public IdentityHashMap(final int initialCapacity) {
059: super (initialCapacity);
060: }
061:
062: /**
063: * Constructor for IdentityHashMap.
064: */
065: public IdentityHashMap() {
066: super ();
067: }
068:
069: /**
070: * Constructor for IdentityHashMap.
071: * @param t
072: */
073: public IdentityHashMap(final Map t) {
074: super (t);
075: }
076:
077: /**
078: * @see java.util.Map#get(java.lang.Object)
079: */
080: public Object get(final Object key) {
081: return super .get(new IdentityKey(key));
082: }
083:
084: /**
085: * @see java.util.Map#put(java.lang.Object, java.lang.Object)
086: */
087: public Object put(final Object key, final Object value) {
088: return super .put(new IdentityKey(key), value);
089: }
090:
091: /**
092: * adds an object to the Map. new IdentityKey(obj) is used as key
093: */
094: public Object add(final Object value) {
095: final Object key = new IdentityKey(value);
096: if (!super .containsKey(key)) {
097: return super .put(key, value);
098: } else {
099: return null;
100: }
101: }
102:
103: /**
104: * @see java.util.Map#remove(java.lang.Object)
105: */
106: public Object remove(final Object key) {
107: return super .remove(new IdentityKey(key));
108: }
109:
110: /**
111: * @see java.util.Map#containsKey(java.lang.Object)
112: */
113: public boolean containsKey(final Object key) {
114: return super .containsKey(new IdentityKey(key));
115: }
116: }
|