Source Code Cross Referenced for TimeBoundHashMap.java in  » Portal » Open-Portal » com » sun » portal » providers » simplewebservice » util » 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 » Portal » Open Portal » com.sun.portal.providers.simplewebservice.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * $Id: TimeBoundHashMap.java,v 1.4 2003/06/03 10:45:56 sy131129 Exp $
003:         * Copyright 2002-2003 Sun Microsystems, Inc. All
004:         * rights reserved. Use of this product is subject
005:         * to license terms. Federal Acquisitions:
006:         * Commercial Software -- Government Users
007:         * Subject to Standard License Terms and
008:         * Conditions.
009:         *
010:         * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011:         * are trademarks or registered trademarks of Sun Microsystems,
012:         * Inc. in the United States and other countries.
013:         */package com.sun.portal.providers.simplewebservice.util;
014:
015:        import java.util.HashMap;
016:        import java.util.Map;
017:        import java.util.Set;
018:        import java.util.Iterator;
019:
020:        /**
021:         * TimeBoundHashMap is a HashMap sub class that purges entries using
022:         * a last accessed time mechanism.
023:         * <P>
024:         * The time out control is done when using the accessor methods of the Map,
025:         * no extra Thread is used for it.
026:         * <P>
027:         * This class is not Thread-safe, if a TimeBoundHashMap instance is to be
028:         * used in a multithreaded fashion it should be wrapped with a synchronized
029:         * Map (see the Collections.synchronizedMap() method).
030:         * <P>
031:         *
032:         *
033:         */
034:        public class TimeBoundHashMap extends HashMap {
035:            private long _timeOut;
036:            private long _lastCheck;
037:            private Map _lastAccess;
038:
039:            /**
040:             * Creates an empty TimeBoundHashMap with a given last accessed time out.
041:             * <P>
042:             *
043:             * @param timeOut last accessed time out in seconds.
044:             *
045:             */
046:            public TimeBoundHashMap(int timeOut) {
047:                super ();
048:                init(timeOut);
049:            }
050:
051:            /**
052:             * Creates an empty TimeBoundHashMap with a specific initial capacity
053:             * and a given last accessed time out.
054:             * <P>
055:             *
056:             * @param initialCapacity initial capacity of the Map.
057:             *
058:             * @param timeOut last accessed time out in seconds.
059:             *
060:             */
061:            public TimeBoundHashMap(int initialCapacity, int timeOut) {
062:                super (initialCapacity);
063:                init(timeOut);
064:            }
065:
066:            /**
067:             * Creates an empty TimeBoundHashMap with a specific initial capacity,
068:             * a given load factor and a given last accessed time out.
069:             * <P>
070:             *
071:             * @param initialCapacity initial capacity of the Map.
072:             *
073:             * @param loadFactor load factor for the Map.
074:             *
075:             * @param timeOut last accessed time out in seconds.
076:             *
077:             */
078:            public TimeBoundHashMap(int initialCapacity, float loadFactor,
079:                    int timeOut) {
080:                super (initialCapacity, loadFactor);
081:                init(timeOut);
082:            }
083:
084:            /**
085:             * Adds time out control and delegates to HashMap.put() method.
086:             *
087:             */
088:            public Object put(Object key, Object value) {
089:                long currentTime = System.currentTimeMillis();
090:                _lastAccess.put(key, new Long(currentTime));
091:                purge();
092:                return super .put(key, value);
093:            }
094:
095:            /**
096:             * Adds time out control and delegates to HashMap.get() method.
097:             *
098:             */
099:            public Object get(Object key) {
100:                Object obj = super .get(key);
101:                if (obj != null) {
102:                    long currentTime = System.currentTimeMillis();
103:                    _lastAccess.put(key, new Long(currentTime));
104:                }
105:                purge();
106:                return obj;
107:            }
108:
109:            /**
110:             * Adds time out control and delegates to HashMap.remove() method.
111:             *
112:             */
113:            public Object remove(Object key) {
114:                _lastAccess.remove(key);
115:                return super .remove(key);
116:            }
117:
118:            /**
119:             * Adds time out control and delegates to HashMap.entrySet() method.
120:             *
121:             */
122:            public Set entrySet() {
123:                Set set = super .entrySet();
124:                Iterator i = _lastAccess.keySet().iterator();
125:                Long currentTime = new Long(System.currentTimeMillis());
126:                while (i.hasNext()) {
127:                    _lastAccess.put(i.next(), currentTime);
128:                }
129:                return set;
130:            }
131:
132:            /**
133:             * Initialized the instance attributes, called from the constructors.
134:             *
135:             * @param timeOut last accessed time out in seconds.
136:             *
137:             */
138:            private void init(int timeOut) {
139:                _timeOut = timeOut * 1000;
140:                _lastCheck = System.currentTimeMillis();
141:                _lastAccess = new HashMap();
142:            }
143:
144:            /**
145:             * Resets the cache time out.
146:             * ONLY to be called before the put and get are called.
147:             *
148:             * @param timeOut last accessed time out in seconds.
149:             *
150:             */
151:            public void setTimeOut(int timeOut) {
152:                _timeOut = timeOut * 1000;
153:            }
154:
155:            /**
156:             * Gets the cache time out.
157:             *
158:             */
159:            public int getTimeOut() {
160:                return (int) _timeOut;
161:            }
162:
163:            /**
164:             * Removes entries that have expired.
165:             *
166:             */
167:            private void purge() {
168:                long currentTime = System.currentTimeMillis();
169:                if ((currentTime - _lastCheck) > (_timeOut / 8)) {
170:                    Iterator i = _lastAccess.keySet().iterator();
171:                    while (i.hasNext()) {
172:                        Object key = i.next();
173:                        long lastTime = ((Long) _lastAccess.get(key))
174:                                .longValue();
175:                        if ((currentTime - lastTime) > _timeOut) {
176:                            i.remove();
177:                            super.remove(key);
178:                        }
179:                    }
180:                    _lastCheck = currentTime;
181:                }
182:            }
183:
184:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.