Source Code Cross Referenced for ValueCache.java in  » IDE-Eclipse » jdt » org » eclipse » jdi » internal » 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 » IDE Eclipse » jdt » org.eclipse.jdi.internal 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2000, 2005 IBM Corporation and others.
003:         * All rights reserved. This program and the accompanying materials
004:         * are made available under the terms of the Eclipse Public License v1.0
005:         * which accompanies this distribution, and is available at
006:         * http://www.eclipse.org/legal/epl-v10.html
007:         * 
008:         * Contributors:
009:         *     IBM Corporation - initial API and implementation
010:         *******************************************************************************/package org.eclipse.jdi.internal;
011:
012:        import java.lang.ref.Reference;
013:        import java.lang.ref.ReferenceQueue;
014:        import java.lang.ref.SoftReference;
015:        import java.util.ArrayList;
016:        import java.util.Collection;
017:        import java.util.Hashtable;
018:        import java.util.Iterator;
019:        import java.util.List;
020:        import java.util.Map;
021:
022:        /**
023:         * This class is used to cache values.
024:         * It uses soft references to store cached values. Once a value is garbage collected by the VM,
025:         * the corresponding entry is removed from the cache on the next invocation of put() or get().
026:         *
027:         * Note that WeakHashMap can't be used for this purpose because in WeakHashMap
028:         * soft references are only used for the keys, and values may not have 'strong' references
029:         * to keys otherwise they will never be garbage collected.
030:         *
031:         */
032:        public class ValueCache {
033:            /**
034:             * Map to store <key, Reference> pairs,
035:             * where Reference is a soft reference to an Object.
036:             */
037:            private Map cacheTable = new Hashtable();
038:            /**
039:             * Map to store <Reference, key> pairs,
040:             * to find the cacheTable-key of a garbage collected Reference.
041:             */
042:            private Map refTable = new Hashtable();
043:
044:            /**
045:             * The reference-queue that is registered with the soft references.
046:             * The garbage collector will enqueue soft references that are garbage collected.
047:             */
048:            private ReferenceQueue refQueue = new ReferenceQueue();
049:
050:            /**
051:             * Clean up all entries from the table for which the values were garbage collected.
052:             */
053:            private void cleanup() {
054:                Reference ref;
055:                while ((ref = refQueue.poll()) != null) {
056:                    Object key = refTable.get(ref);
057:                    if (key != null)
058:                        cacheTable.remove(key);
059:                    refTable.remove(ref);
060:                }
061:            }
062:
063:            /**
064:             * Put a new entry in the cache under the given key.
065:             */
066:            public void put(Object key, Object value) {
067:                cleanup();
068:                SoftReference ref = new SoftReference(value, refQueue);
069:                cacheTable.put(key, ref);
070:                refTable.put(ref, key);
071:            }
072:
073:            /**
074:             * Get entry from the cache.
075:             * @return Returns value that is cached under the given key,
076:             * or null of one of the following is true:
077:             *  - The value has not been cached.
078:             *  - The value had been cached but is garbage collected.
079:             */
080:            public Object get(Object key) {
081:                cleanup();
082:                Object value = null;
083:                SoftReference ref = (SoftReference) cacheTable.get(key);
084:                if (ref != null) {
085:                    value = ref.get();
086:                }
087:                return value;
088:            }
089:
090:            /**
091:             * Returns a Collection view of the values contained in this cache.
092:             */
093:            public Collection values() {
094:                cleanup();
095:                List returnValues = new ArrayList();
096:                synchronized (cacheTable) {
097:                    Iterator iter = cacheTable.values().iterator();
098:                    SoftReference ref;
099:                    Object value;
100:                    while (iter.hasNext()) {
101:                        ref = (SoftReference) iter.next();
102:                        value = ref.get();
103:                        if (value != null) {
104:                            returnValues.add(value);
105:                        }
106:                    }
107:                }
108:                return returnValues;
109:            }
110:
111:            /**
112:             * Returns a Collection view of the values contained in this cache that have the same
113:             * runtime class as the given Class.
114:             */
115:            public Collection valuesWithType(Class type) {
116:                cleanup();
117:                List returnValues = new ArrayList();
118:                synchronized (cacheTable) {
119:                    Iterator iter = cacheTable.values().iterator();
120:                    SoftReference ref;
121:                    Object value;
122:                    while (iter.hasNext()) {
123:                        ref = (SoftReference) iter.next();
124:                        value = ref.get();
125:                        if (value != null && value.getClass().equals(type)) {
126:                            returnValues.add(value);
127:                        }
128:                    }
129:                }
130:                return returnValues;
131:            }
132:
133:            /** 
134:             * Removes the key and its corresponding value from this cache.
135:             * @return Returns The value to which the key had been mapped in this hashtable,
136:             * or null if the key did not have a mapping.
137:             */
138:            public Object remove(Object key) {
139:                cleanup();
140:                Object value = null;
141:                SoftReference ref = (SoftReference) cacheTable.get(key);
142:                if (ref != null) {
143:                    value = ref.get();
144:                    refTable.remove(ref);
145:                }
146:                cacheTable.remove(key);
147:                return value;
148:            }
149:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.