Java Doc for CacheMap.java in  » Ajax » zk » org » zkoss » 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 » Ajax » zk » org.zkoss.util 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.zkoss.util.CacheMap

All known Subclasses:   org.zkoss.util.resource.ResourceCache,
CacheMap
public class CacheMap implements Map,Cache,java.io.Serializable,Cloneable(Code)
The cache map. The key-to-value mappings hold in this map is temporary. They are removed when GC demanding memory and a criteria is met. The criteria is whether the mapping is old enough (called lifetime), or the upper bound is hit (called max-size).

The criteria can be changed by overriding CacheMap.canExpunge . When to check the criteria can be changed by overriding CacheMap.shallExpunge .

If the criteria is totally independent of GC, you could override CacheMap.newQueue to return null. Then, CacheMap.shallExpunge always returns true (rather than when GC is activated) -- of course, you could override CacheMap.shallExpunge , too.

It is different from WeakHashMap:

  • The mapping might be removed even if the key is hold somewhere (i.e., strong reachable).
  • The mapping might not be removed when GC demanding memory if the criteria doesn't meet.
  • It is not serializable.

Like other maps, it is not thread-safe. To get one, use java.util.Collections.synchronizedMap.

Implementation Note: there is another version of CacheMap that uses WeakReference for each value (refer to obsolete). The drawback is that all mapping will be queued and need to be examined, because GC tends to release all reference at once.

We don't use PhantomReference because it is still required to re-create the reference after enqueued.
author:
   tomyeh


Inner Class :final protected static class Value implements java.io.Serializable,Cloneable

Field Summary
final public static  intDEFAULT_MAXSIZE
    
final protected static  intEXPUNGE_CONTINUE
     Returns by CacheMap.canExpunge to denote the searching of the next mapping shall continue.
final protected static  intEXPUNGE_NO
     Returns by CacheMap.canExpunge to denote it shall not be expunged.
final protected static  intEXPUNGE_STOP
     Returns by CacheMap.canExpunge to denote the searching of the next mapping shall stop.
final protected static  intEXPUNGE_YES
     Returns by CacheMap.canExpunge to denote it shall be expunged.

Constructor Summary
public  CacheMap(int maxSize, int lifetime)
     Constructs a cache map with the specified max size and lifetime.
public  CacheMap()
     Constructs a cachemap by using LinkedHashMap internally.
public  CacheMap(int cap)
     Constructs a cachemap by using LinkedHashMap internally.
public  CacheMap(int cap, float load)
     Constructs a cachemap by using LinkedHashMap internally.

Method Summary
protected  intcanExpunge(Value v)
     Tests whether certain value is OK to expunge.

Note: values are tested thru CacheMap.canExpunge only if CacheMap.shallExpunge returns true.

Deriving classes might override this method to return different value for different criteria.

The return value coulde be a combination of EXPUNGE_xxx. One of EXPUNGE_YES and EXPUNGE_NO is returned to denote whether to expunge the mapping.

public  voidclear()
    
public  Objectclone()
     To make sure that it is in the acess order.
public  booleancontainsKey(Object key)
    
public  booleancontainsValue(Object value)
    
public  SetentrySet()
    
public  booleanequals(Object o)
    
public  Objectget(Object key)
    
public  intgetLifetime()
     Gets the minimal lifetime, unit=milliseconds.
public  intgetMaxSize()
     Gets the maximal allowed size.
public  ObjectgetWithoutExpunge(Object key)
     Returns the value without trying to expunge first.
public  inthashCode()
    
public  booleanisEmpty()
     Gets the last accessed time, in system millisecs.
public  booleanisEmptyWithoutExpunge()
     Returns whether it is empty without trying to expunge first.
public  SetkeySet()
    
protected  ReferenceQueuenewQueue()
     Creates the reference queue.
protected  voidonExpunge(Value v)
     Called when a pair of key and value having been expunged.
public  Objectput(Object key, Object value)
    
public  voidputAll(Map map)
    
public  Objectremove(Object key)
    
public  voidsetLifetime(int lifetime)
     Sets the minimal lifetime.
public  voidsetMaxSize(int maxsize)
     Sets the maximal allowed size.
protected  booleanshallExpunge()
     Returns whether it is time to expunge.
public  intsize()
    
public  intsizeWithoutExpunge()
     Returns the size without trying to expunge first.
public  StringtoString()
    
public  Collectionvalues()
    

Field Detail
DEFAULT_MAXSIZE
final public static int DEFAULT_MAXSIZE(Code)
Cache.DEFAULT_MAX_SIZE



EXPUNGE_CONTINUE
final protected static int EXPUNGE_CONTINUE(Code)
Returns by CacheMap.canExpunge to denote the searching of the next mapping shall continue.



EXPUNGE_NO
final protected static int EXPUNGE_NO(Code)
Returns by CacheMap.canExpunge to denote it shall not be expunged.



EXPUNGE_STOP
final protected static int EXPUNGE_STOP(Code)
Returns by CacheMap.canExpunge to denote the searching of the next mapping shall stop.



EXPUNGE_YES
final protected static int EXPUNGE_YES(Code)
Returns by CacheMap.canExpunge to denote it shall be expunged.




Constructor Detail
CacheMap
public CacheMap(int maxSize, int lifetime)(Code)
Constructs a cache map with the specified max size and lifetime.
since:
   3.0.0



CacheMap
public CacheMap()(Code)
Constructs a cachemap by using LinkedHashMap internally.



CacheMap
public CacheMap(int cap)(Code)
Constructs a cachemap by using LinkedHashMap internally.



CacheMap
public CacheMap(int cap, float load)(Code)
Constructs a cachemap by using LinkedHashMap internally.




Method Detail
canExpunge
protected int canExpunge(Value v)(Code)
Tests whether certain value is OK to expunge.

Note: values are tested thru CacheMap.canExpunge only if CacheMap.shallExpunge returns true.

Deriving classes might override this method to return different value for different criteria.

The return value coulde be a combination of EXPUNGE_xxx. One of EXPUNGE_YES and EXPUNGE_NO is returned to denote whether to expunge the mapping. One of EXPUNGE_CONTINUE and EXPUNGE_STOP is returned to denote whether to continue the searching of the next mapping for expunging.

Normally, you return either (EXPUNGE_YES|EXPUNGE_CONTINUE) or (EXPUNG_NO|EXPUNGE_STOP). Notice that the mapping is queried in the last-access order. Thus, you rarely needs to return (EXPUNGE_NO|EXPUNGE_CONTINUE) unless the appropriate one might be out of this order.

This implementation compares the access time and size. It returns (EXPUNGE_YES|EXPUNGE_CONTINUE) if OK, and (EXPUNGE_NO|EXPUNGE_STOP) if not. a combination of EXPUNGE_xxx
See Also:   CacheMap.shallExpunge




clear
public void clear()(Code)



clone
public Object clone()(Code)
To make sure that it is in the acess order.



containsKey
public boolean containsKey(Object key)(Code)



containsValue
public boolean containsValue(Object value)(Code)



entrySet
public Set entrySet()(Code)



equals
public boolean equals(Object o)(Code)



get
public Object get(Object key)(Code)



getLifetime
public int getLifetime()(Code)
Gets the minimal lifetime, unit=milliseconds. An mapping won't be removed by GC unless the minimal lifetime or the maximal allowed size exceeds.
See Also:   CacheMap.getMaxSize



getMaxSize
public int getMaxSize()(Code)
Gets the maximal allowed size. Defalut: CacheMap.DEFAULT_MAX_SIZE . An mapping won't be removed by GC unless the minimal lifetime or the maximal allowed size exceeds.
See Also:   CacheMap.getLifetime



getWithoutExpunge
public Object getWithoutExpunge(Object key)(Code)
Returns the value without trying to expunge first. It is useful if you want to preserve all entries.



hashCode
public int hashCode()(Code)



isEmpty
public boolean isEmpty()(Code)
Gets the last accessed time, in system millisecs. the last accessed time; 0 if not found



isEmptyWithoutExpunge
public boolean isEmptyWithoutExpunge()(Code)
Returns whether it is empty without trying to expunge first.
since:
   3.0.1



keySet
public Set keySet()(Code)



newQueue
protected ReferenceQueue newQueue()(Code)
Creates the reference queue. It is called only once in the constructor (so it is meaningless to change the returned value after constructed).

Default: new ReferenceQueue();
Override this method to return null if you want to expunge items every time CacheMap.get or CacheMap.put is called -- not only GC is activated. In other words, if CacheMap.newQueue returns null, CacheMap.shallExpunge always returns true (unless you override it too).




onExpunge
protected void onExpunge(Value v)(Code)
Called when a pair of key and value having been expunged. This method is called after it is removed, so you could add it back.

Default: does nothing




put
public Object put(Object key, Object value)(Code)



putAll
public void putAll(Map map)(Code)



remove
public Object remove(Object key)(Code)



setLifetime
public void setLifetime(int lifetime)(Code)
Sets the minimal lifetime. Default: CacheMap.DEFAULT_LIFETIME .
Parameters:
  lifetime - the lifetime, unit=milliseconds;if non-posive, they will be removed immediately.
See Also:   CacheMap.getLifetime



setMaxSize
public void setMaxSize(int maxsize)(Code)
Sets the maximal allowed size.
See Also:   CacheMap.getMaxSize



shallExpunge
protected boolean shallExpunge()(Code)
Returns whether it is time to expunge. Once shallExpunge returns true, values are examined one-by-one thru CacheMap.canExpunge , and expunged if EXPUNGE_YES.

This implementation returns true only if CacheMap.newQueue returns null (in constructor) or GC was activated. You might override it to enforce expunge besides GC.
See Also:   CacheMap.canExpunge




size
public int size()(Code)



sizeWithoutExpunge
public int sizeWithoutExpunge()(Code)
Returns the size without trying to expunge first.
since:
   3.0.1



toString
public String toString()(Code)



values
public Collection values()(Code)



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.