Source Code Cross Referenced for LRUCache.java in  » Database-ORM » MMBase » org » mmbase » cache » implementation » 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 » Database ORM » MMBase » org.mmbase.cache.implementation 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:        This software is OSI Certified Open Source Software.
004:        OSI Certified is a certification mark of the Open Source Initiative.
005:
006:        The license (Mozilla version 1.0) can be read at the MMBase site.
007:        See http://www.MMBase.org/license
008:
009:         */
010:        package org.mmbase.cache.implementation;
011:
012:        import org.mmbase.cache.CacheImplementationInterface;
013:        import java.util.*;
014:
015:        /**
016:         * A cache implementation backed by a {@link java.util.LinkedHashMap}, in access-order mode, and
017:         * restricted maximal size ('Least Recently Used' cache algorithm).
018:         *
019:         * @author  Michiel Meeuwissen
020:         * @version $Id: LRUCache.java,v 1.2 2008/02/03 17:33:57 nklasens Exp $
021:         * @see    org.mmbase.cache.Cache
022:         * @since MMBase-1.9
023:         */
024:        public class LRUCache<K, V> implements 
025:                CacheImplementationInterface<K, V> {
026:
027:            public int maxSize = 100;
028:            private final Map<K, V> backing;
029:
030:            public LRUCache() {
031:                this (100);
032:            }
033:
034:            public LRUCache(int size) {
035:                maxSize = size;
036:                // caches can typically be accessed/modified by multipible thread, so we need to synchronize
037:                backing = Collections.synchronizedMap(new LinkedHashMap<K, V>(
038:                        size, 0.75f, true) {
039:                    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
040:                        return size() > LRUCache.this .maxSize;
041:                    }
042:                });
043:            }
044:
045:            public int getCount(K key) {
046:                return -1;
047:            }
048:
049:            /**
050:             * Change the maximum size of the table.
051:             * This may result in removal of entries in the table.
052:             * @param size the new desired size
053:             */
054:            public void setMaxSize(int size) {
055:                if (size < 1)
056:                    throw new IllegalArgumentException(
057:                            "Cannot set size of  to non-positive value");
058:                maxSize = size;
059:                while (size() > maxSize()) {
060:                    try {
061:                        Iterator<Entry<K, V>> i = entrySet().iterator();
062:                        i.next();
063:                        i.remove();
064:                    } catch (Exception e) {
065:                        // ConcurentModification?
066:                    }
067:                }
068:            }
069:
070:            public int maxSize() {
071:                return maxSize;
072:            }
073:
074:            /**
075:             * Returns size, maxSize.
076:             */
077:            public String toString() {
078:                return "Size=" + size() + ", Max=" + maxSize;
079:            }
080:
081:            public void config(Map<String, String> map) {
082:                // needs no configuration.
083:            }
084:
085:            // wrapping for synchronization
086:            public int size() {
087:                return backing.size();
088:            }
089:
090:            public boolean isEmpty() {
091:                return backing.isEmpty();
092:            }
093:
094:            public boolean containsKey(Object key) {
095:                return backing.containsKey(key);
096:            }
097:
098:            public boolean containsValue(Object value) {
099:                return backing.containsValue(value);
100:            }
101:
102:            public V get(Object key) {
103:                return backing.get(key);
104:            }
105:
106:            public V put(K key, V value) {
107:                return backing.put(key, value);
108:            }
109:
110:            public V remove(Object key) {
111:                return backing.remove(key);
112:            }
113:
114:            public void putAll(Map<? extends K, ? extends V> map) {
115:                backing.putAll(map);
116:            }
117:
118:            public void clear() {
119:                backing.clear();
120:            }
121:
122:            public Set<K> keySet() {
123:                return backing.keySet();
124:            }
125:
126:            public Set<Map.Entry<K, V>> entrySet() {
127:                return backing.entrySet();
128:            }
129:
130:            public Collection<V> values() {
131:                return backing.values();
132:            }
133:
134:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.