Source Code Cross Referenced for ReferenceIdentityMap.java in  » Library » Apache-common-Collections » org » apache » commons » collections » map » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache common Collections » org.apache.commons.collections.map 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Copyright 2004 The Apache Software Foundation
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         */
016:        package org.apache.commons.collections.map;
017:
018:        import java.io.IOException;
019:        import java.io.ObjectInputStream;
020:        import java.io.ObjectOutputStream;
021:        import java.io.Serializable;
022:        import java.lang.ref.Reference;
023:
024:        /**
025:         * A <code>Map</code> implementation that allows mappings to be
026:         * removed by the garbage collector and matches keys and values based
027:         * on <code>==</code> not <code>equals()</code>.
028:         * <p>
029:         * <p>
030:         * When you construct a <code>ReferenceIdentityMap</code>, you can specify what kind
031:         * of references are used to store the map's keys and values.
032:         * If non-hard references are used, then the garbage collector can remove
033:         * mappings if a key or value becomes unreachable, or if the JVM's memory is
034:         * running low. For information on how the different reference types behave,
035:         * see {@link Reference}.
036:         * <p>
037:         * Different types of references can be specified for keys and values.
038:         * The default constructor uses hard keys and soft values, providing a
039:         * memory-sensitive cache.
040:         * <p>
041:         * This map is similar to
042:         * {@link org.apache.commons.collections.map.ReferenceMap ReferenceMap}.
043:         * It differs in that keys and values in this class are compared using <code>==</code>.
044:         * <p>
045:         * This map will violate the detail of various Map and map view contracts.
046:         * As a general rule, don't compare this map to other maps.
047:         * <p>
048:         * This {@link java.util.Map Map} implementation does <i>not</i> allow null elements.
049:         * Attempting to add a null key or value to the map will raise a <code>NullPointerException</code>.
050:         * <p>
051:         * This implementation is not synchronized.
052:         * You can use {@link java.util.Collections#synchronizedMap} to 
053:         * provide synchronized access to a <code>ReferenceIdentityMap</code>.
054:         * Remember that synchronization will not stop the garbage collecter removing entries.
055:         * <p>
056:         * All the available iterators can be reset back to the start by casting to
057:         * <code>ResettableIterator</code> and calling <code>reset()</code>.
058:         * <p>
059:         * <strong>Note that ReferenceIdentityMap is not synchronized and is not thread-safe.</strong>
060:         * If you wish to use this map from multiple threads concurrently, you must use
061:         * appropriate synchronization. The simplest approach is to wrap this map
062:         * using {@link java.util.Collections#synchronizedMap}. This class may throw 
063:         * exceptions when accessed by concurrent threads without synchronization.
064:         *
065:         * @see java.lang.ref.Reference
066:         * 
067:         * @since Commons Collections 3.0 (previously in main package v2.1)
068:         * @version $Revision: 348007 $ $Date: 2005-11-21 22:52:57 +0000 (Mon, 21 Nov 2005) $
069:         * 
070:         * @author Stephen Colebourne
071:         */
072:        public class ReferenceIdentityMap extends AbstractReferenceMap
073:                implements  Serializable {
074:
075:            /** Serialization version */
076:            private static final long serialVersionUID = -1266190134568365852L;
077:
078:            /**
079:             * Constructs a new <code>ReferenceIdentityMap</code> that will
080:             * use hard references to keys and soft references to values.
081:             */
082:            public ReferenceIdentityMap() {
083:                super (HARD, SOFT, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, false);
084:            }
085:
086:            /**
087:             * Constructs a new <code>ReferenceIdentityMap</code> that will
088:             * use the specified types of references.
089:             *
090:             * @param keyType  the type of reference to use for keys;
091:             *   must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
092:             * @param valueType  the type of reference to use for values;
093:             *   must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
094:             */
095:            public ReferenceIdentityMap(int keyType, int valueType) {
096:                super (keyType, valueType, DEFAULT_CAPACITY,
097:                        DEFAULT_LOAD_FACTOR, false);
098:            }
099:
100:            /**
101:             * Constructs a new <code>ReferenceIdentityMap</code> that will
102:             * use the specified types of references.
103:             *
104:             * @param keyType  the type of reference to use for keys;
105:             *   must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
106:             * @param valueType  the type of reference to use for values;
107:             *   must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
108:             * @param purgeValues should the value be automatically purged when the 
109:             *   key is garbage collected 
110:             */
111:            public ReferenceIdentityMap(int keyType, int valueType,
112:                    boolean purgeValues) {
113:                super (keyType, valueType, DEFAULT_CAPACITY,
114:                        DEFAULT_LOAD_FACTOR, purgeValues);
115:            }
116:
117:            /**
118:             * Constructs a new <code>ReferenceIdentityMap</code> with the
119:             * specified reference types, load factor and initial capacity.
120:             *
121:             * @param keyType  the type of reference to use for keys;
122:             *   must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
123:             * @param valueType  the type of reference to use for values;
124:             *   must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
125:             * @param capacity  the initial capacity for the map
126:             * @param loadFactor  the load factor for the map
127:             */
128:            public ReferenceIdentityMap(int keyType, int valueType,
129:                    int capacity, float loadFactor) {
130:                super (keyType, valueType, capacity, loadFactor, false);
131:            }
132:
133:            /**
134:             * Constructs a new <code>ReferenceIdentityMap</code> with the
135:             * specified reference types, load factor and initial capacity.
136:             *
137:             * @param keyType  the type of reference to use for keys;
138:             *   must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
139:             * @param valueType  the type of reference to use for values;
140:             *   must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
141:             * @param capacity  the initial capacity for the map
142:             * @param loadFactor  the load factor for the map
143:             * @param purgeValues  should the value be automatically purged when the 
144:             *   key is garbage collected 
145:             */
146:            public ReferenceIdentityMap(int keyType, int valueType,
147:                    int capacity, float loadFactor, boolean purgeValues) {
148:                super (keyType, valueType, capacity, loadFactor, purgeValues);
149:            }
150:
151:            //-----------------------------------------------------------------------
152:            /**
153:             * Gets the hash code for the key specified.
154:             * <p>
155:             * This implementation uses the identity hash code.
156:             * 
157:             * @param key  the key to get a hash code for
158:             * @return the hash code
159:             */
160:            protected int hash(Object key) {
161:                return System.identityHashCode(key);
162:            }
163:
164:            /**
165:             * Gets the hash code for a MapEntry.
166:             * <p>
167:             * This implementation uses the identity hash code.
168:             * 
169:             * @param key  the key to get a hash code for, may be null
170:             * @param value  the value to get a hash code for, may be null
171:             * @return the hash code, as per the MapEntry specification
172:             */
173:            protected int hashEntry(Object key, Object value) {
174:                return System.identityHashCode(key)
175:                        ^ System.identityHashCode(value);
176:            }
177:
178:            /**
179:             * Compares two keys for equals.
180:             * <p>
181:             * This implementation converts the key from the entry to a real reference
182:             * before comparison and uses <code>==</code>.
183:             * 
184:             * @param key1  the first key to compare passed in from outside
185:             * @param key2  the second key extracted from the entry via <code>entry.key</code>
186:             * @return true if equal by identity
187:             */
188:            protected boolean isEqualKey(Object key1, Object key2) {
189:                key2 = (keyType > HARD ? ((Reference) key2).get() : key2);
190:                return (key1 == key2);
191:            }
192:
193:            /**
194:             * Compares two values for equals.
195:             * <p>
196:             * This implementation uses <code>==</code>.
197:             * 
198:             * @param value1  the first value to compare passed in from outside
199:             * @param value2  the second value extracted from the entry via <code>getValue()</code>
200:             * @return true if equal by identity
201:             */
202:            protected boolean isEqualValue(Object value1, Object value2) {
203:                return (value1 == value2);
204:            }
205:
206:            //-----------------------------------------------------------------------
207:            /**
208:             * Write the map out using a custom routine.
209:             */
210:            private void writeObject(ObjectOutputStream out) throws IOException {
211:                out.defaultWriteObject();
212:                doWriteObject(out);
213:            }
214:
215:            /**
216:             * Read the map in using a custom routine.
217:             */
218:            private void readObject(ObjectInputStream in) throws IOException,
219:                    ClassNotFoundException {
220:                in.defaultReadObject();
221:                doReadObject(in);
222:            }
223:
224:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.