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    {
029            private static final class IdentityKey
030            {
031                    private final Object m_key;
032    
033                    public IdentityKey(final Object key)
034                    {
035                            m_key = key;
036                    }
037    
038                    public boolean equals(final Object o)
039                    {
040                            return (o == m_key);
041                    }
042    
043                    public int hashCode()
044                    {
045                            return System.identityHashCode(m_key);
046                    }
047            }
048    
049            /**
050             * Constructor for IdentityHashMap.
051             * @param initialCapacity
052             * @param loadFactor
053             */
054            public IdentityHashMap(final int initialCapacity, final float loadFactor)
055            {
056                    super(initialCapacity, loadFactor);
057            }
058    
059            /**
060             * Constructor for IdentityHashMap.
061             * @param initialCapacity
062             */
063            public IdentityHashMap(final int initialCapacity)
064            {
065                    super(initialCapacity);
066            }
067    
068            /**
069             * Constructor for IdentityHashMap.
070             */
071            public IdentityHashMap()
072            {
073                    super();
074            }
075    
076            /**
077             * Constructor for IdentityHashMap.
078             * @param t
079             */
080            public IdentityHashMap(final Map t)
081            {
082                    super(t);
083            }
084    
085            /**
086             * @see java.util.Map#get(java.lang.Object)
087             */
088            public Object get(final Object key)
089            {
090                    return super.get(new IdentityKey(key));
091            }
092    
093            /**
094             * @see java.util.Map#put(java.lang.Object, java.lang.Object)
095             */
096            public Object put(final Object key, final Object value)
097            {
098                    return super.put(new IdentityKey(key), value);
099            }
100    
101            /**
102             * adds an object to the Map. new IdentityKey(obj) is used as key
103             */
104            public Object add(final Object value)
105            {
106                    final Object key = new IdentityKey(value);
107                    if (!super.containsKey(key))
108                    {
109                            return super.put(key, value);
110                    }
111                    else
112                    {
113                            return null;
114                    }
115            }
116    
117            /**
118             * @see java.util.Map#remove(java.lang.Object)
119             */
120            public Object remove(final Object key)
121            {
122                    return super.remove(new IdentityKey(key));
123            }
124    
125            /**
126             * @see java.util.Map#containsKey(java.lang.Object)
127             */
128            public boolean containsKey(final Object key)
129            {
130                    return super.containsKey(new IdentityKey(key));
131            }
132    }
133