View Javadoc

1   package org.apache.ojb.broker.util;
2   
3   /* Copyright 2002-2005 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  /**
22   * replacement for the JDK1.4 version
23   * User: Matthew Baird
24   * Date: Jul 8, 2003
25   * Time: 8:37:00 AM
26   */
27  public final class IdentityHashMap extends HashMap
28  {
29  	private static final class IdentityKey
30  	{
31  		private final Object m_key;
32  
33  		public IdentityKey(final Object key)
34  		{
35  			m_key = key;
36  		}
37  
38  		public boolean equals(final Object o)
39  		{
40  			return (o == m_key);
41  		}
42  
43  		public int hashCode()
44  		{
45  			return System.identityHashCode(m_key);
46  		}
47  	}
48  
49  	/**
50  	 * Constructor for IdentityHashMap.
51  	 * @param initialCapacity
52  	 * @param loadFactor
53  	 */
54  	public IdentityHashMap(final int initialCapacity, final float loadFactor)
55  	{
56  		super(initialCapacity, loadFactor);
57  	}
58  
59  	/**
60  	 * Constructor for IdentityHashMap.
61  	 * @param initialCapacity
62  	 */
63  	public IdentityHashMap(final int initialCapacity)
64  	{
65  		super(initialCapacity);
66  	}
67  
68  	/**
69  	 * Constructor for IdentityHashMap.
70  	 */
71  	public IdentityHashMap()
72  	{
73  		super();
74  	}
75  
76  	/**
77  	 * Constructor for IdentityHashMap.
78  	 * @param t
79  	 */
80  	public IdentityHashMap(final Map t)
81  	{
82  		super(t);
83  	}
84  
85  	/**
86  	 * @see java.util.Map#get(java.lang.Object)
87  	 */
88  	public Object get(final Object key)
89  	{
90  		return super.get(new IdentityKey(key));
91  	}
92  
93  	/**
94  	 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
95  	 */
96  	public Object put(final Object key, final Object value)
97  	{
98  		return super.put(new IdentityKey(key), value);
99  	}
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