Source Code Cross Referenced for WeakValuedMap.java in  » ERP-CRM-Financial » jmoney » net » sf » jmoney » model2 » 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 » ERP CRM Financial » jmoney » net.sf.jmoney.model2 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *
003:         *  JMoney - A Personal Finance Manager
004:         *  Copyright (c) 2007 Nigel Westbury <westbury@users.sourceforge.net>
005:         *
006:         *
007:         *  This program is free software; you can redistribute it and/or modify
008:         *  it under the terms of the GNU General Public License as published by
009:         *  the Free Software Foundation; either version 2 of the License, or
010:         *  (at your option) any later version.
011:         *
012:         *  This program is distributed in the hope that it will be useful,
013:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015:         *  GNU General Public License for more details.
016:         *
017:         *  You should have received a copy of the GNU General Public License
018:         *  along with this program; if not, write to the Free Software
019:         *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020:         *
021:         */
022:
023:        package net.sf.jmoney.model2;
024:
025:        import java.lang.ref.Reference;
026:        import java.lang.ref.ReferenceQueue;
027:        import java.lang.ref.WeakReference;
028:        import java.util.HashMap;
029:        import java.util.Map;
030:
031:        public class WeakValuedMap<K, V> {
032:
033:            /**
034:             * We define our own subclass of WeakReference which contains not only the
035:             * value but also the key to make it easier to find the entry in the HashMap
036:             * after it's been garbage collected.
037:             */
038:            private static class WeakValueReference<K, V> extends
039:                    WeakReference<V> {
040:                /**
041:                 * This key is always set when this weak reference is created and added
042:                 * to the map. However, there are two reasons why an entry may be
043:                 * removed from the map. An entry may be removed because there are no
044:                 * strong references to the value, but an entry may also be removed by
045:                 * an explicit call to the <code>remove</code> method. It is possible
046:                 * that an entry may be removed by a call to the <code>remove</code>
047:                 * method and then later the reference queue is processed and a second
048:                 * attempt is made to remove the entry. You may think this is not a
049:                 * problem because the attempt to remove the entry the second time will
050:                 * simply do nothing. However, it is possible that another entry has
051:                 * been put in the map by a call to the <code>put</code> method using
052:                 * the same key value. The queue processor would then be removing not a
053:                 * dead entry but an active entry.
054:                 * 
055:                 * This can be solved as follows. If an entry is explicitly deleted from
056:                 * the map then the entry is removed from the map AND the key in this
057:                 * reference object is set to null.
058:                 */
059:                private K key;
060:
061:                private WeakValueReference(V value, K key,
062:                        ReferenceQueue<V> queue) {
063:                    super (value, queue);
064:                    this .key = key;
065:                }
066:            }
067:
068:            private Map<K, WeakValueReference<K, V>> map = new HashMap<K, WeakValueReference<K, V>>();
069:
070:            private ReferenceQueue<V> referenceQueue = new ReferenceQueue<V>();
071:
072:            /**
073:             * 
074:             * @param key the key which must not be null
075:             * @param value the value to which a weak reference is maintained
076:             */
077:            public void put(K key, V value) {
078:                removeDeadEntries();
079:
080:                map.put(key, new WeakValueReference<K, V>(value, key,
081:                        referenceQueue));
082:            }
083:
084:            /**
085:             * 
086:             * @param key
087:             * @return the value if it is in the map, or null if
088:             * 		either the key was not in the map or if the value
089:             * 		had been garbage collected
090:             */
091:            public V get(K key) {
092:                WeakReference<V> valueReference = map.get(key);
093:                if (valueReference == null) {
094:                    return null;
095:                } else {
096:                    return valueReference.get();
097:                }
098:            }
099:
100:            public void remove(K key) {
101:                /*
102:                 * This line is necessary to stop the reference queue processor from
103:                 * deleting an active entry if the same key was re-used for a new
104:                 * object.
105:                 */
106:                map.get(key).key = null;
107:
108:                map.remove(key);
109:            }
110:
111:            /**
112:             * Check the queue for weak references to the value objects
113:             * that no longer exist.  It really does not matter where this is
114:             * done as long as it is done on the thread for this class (this
115:             * class is not thread-safe).
116:             */
117:            private void removeDeadEntries() {
118:                Reference<? extends V> valueReference = referenceQueue.poll();
119:                while (valueReference != null) {
120:                    Object key = ((WeakValueReference<?, ?>) valueReference).key;
121:                    if (key != null) {
122:                        map.remove(key);
123:                    }
124:                    valueReference = referenceQueue.poll();
125:                }
126:            }
127:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.