org.xwiki.gwt.user.client
Class Cache

java.lang.Object
  extended by org.xwiki.gwt.user.client.Cache
All Implemented Interfaces:
Updatable

public class Cache
extends java.lang.Object
implements Updatable

Allows us to preserve the state of the editor against the browser's Back button, the soft page refresh and the browser crash by using the browser's cache mechanism. Modern browsers cache HTML form fields like input and textarea as long as they are generated on the server and not on the client using JavaScript. This class uses the value of such an element to save string properties.

Version:
$Id: Cache.java 24900 2009-11-06 16:53:01Z sdumitriu $

Constructor Summary
Cache(Element cacheable)
          Creates a new cache based on the given HTML element whose value can be cached by the browser.
 
Method Summary
 boolean canUpdate()
          
protected  java.util.Map<java.lang.String,java.lang.String> deserialize(java.lang.String data)
          Parses the given string and extracts a map of key-value pairs.
 java.lang.String get(java.lang.String key)
          Retrieves the value of a cached property specified by the given key.
 java.lang.String get(java.lang.String key, java.lang.String defaultValue)
          Retrieves the value of a cached property specified by the given key, falling back on a default value if the key is not found in the cache.
protected  java.util.Map<java.lang.String,java.lang.String> getMap()
           
 java.lang.String put(java.lang.String key, java.lang.String value)
          Adds a new key in the cache or updates the value of an existing key.
protected  java.lang.String serialize(java.util.Map<java.lang.String,java.lang.String> map)
          Converts the given map of key-value pairs to a string from which it can be recomputed.
 void update()
          Updates the state of this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Cache

public Cache(Element cacheable)
Creates a new cache based on the given HTML element whose value can be cached by the browser. Examples of such elements are input and textarea.

The browser doesn't cache the HTML elements added dynamically through JavaScript but only the static ones. Moving an element inside the DOM tree prevents the browser from caching its value even if that element was generated on the server.

Parameters:
cacheable - any HTML element whose value can be cached by the browser
Method Detail

get

public java.lang.String get(java.lang.String key)
Retrieves the value of a cached property specified by the given key.

Parameters:
key - a key in the cache
Returns:
the value associated with the given key or null if there's no such key in the cache

get

public java.lang.String get(java.lang.String key,
                            java.lang.String defaultValue)
Retrieves the value of a cached property specified by the given key, falling back on a default value if the key is not found in the cache.

Parameters:
key - a key in the cache
defaultValue - the value returned if the specified key is not found in the cache
Returns:
the value associated with the given key or defaultValue if there's no such key in the cache

put

public java.lang.String put(java.lang.String key,
                            java.lang.String value)
Adds a new key in the cache or updates the value of an existing key.

Parameters:
key - a key in the cache
value - the new value for the given key
Returns:
the previous value associated with the given key

getMap

protected java.util.Map<java.lang.String,java.lang.String> getMap()
Returns:
the Map used to store the cache for quick access

canUpdate

public boolean canUpdate()

Specified by:
canUpdate in interface Updatable
Returns:
true if this object can be updated at this point, false otherwise
See Also:
Updatable.canUpdate()

update

public void update()
Updates the state of this object.

Specified by:
update in interface Updatable
See Also:
Updatable.update()

deserialize

protected java.util.Map<java.lang.String,java.lang.String> deserialize(java.lang.String data)
Parses the given string and extracts a map of key-value pairs.

Parameters:
data - the string to be deserialized
Returns:
the map whose serialization is the given string
See Also:
serialize(Map)

serialize

protected java.lang.String serialize(java.util.Map<java.lang.String,java.lang.String> map)
Converts the given map of key-value pairs to a string from which it can be recomputed.

Parameters:
map - the map to be serialized
Returns:
a string representation of the given map
See Also:
deserialize(String)


Copyright © 2004-2009 XWiki. All Rights Reserved.