ehcache

net.sf.ehcache
Class Element

java.lang.Object
  extended bynet.sf.ehcache.Element
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable

public final class Element
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable

A Cache Element, consisting of a key, value and attributes.

From ehcache-1.2, Elements can have keys and values that are Serializable or Objects. To preserve backward compatibility, special accessor methods for Object keys and values are provided: getObjectKey() and getObjectValue(). If placing Objects in ehcace, developers must use the new getObject... methods to avoid CacheExceptions. The get... methods are reserved for Serializable keys and values.

Version:
$Id: Element.java 52 2006-04-24 14:50:03Z gregluck $
Author:
Greg Luck
See Also:
Serialized Form

Constructor Summary
Element(java.lang.Object key, java.lang.Object value)
          Constructor.
Element(java.lang.Object key, java.lang.Object value, long version)
          A full constructor.
Element(java.io.Serializable key, java.io.Serializable value)
          Constructor.
Element(java.io.Serializable key, java.io.Serializable value, long version)
          A full constructor.
 
Method Summary
 java.lang.Object clone()
          Clones an Element.
 boolean equals(java.lang.Object object)
          Equals comparison with another element, based on the key.
 long getCreationTime()
          Gets the creationTime attribute of the ElementAttributes object.
 long getHitCount()
          Gets the hit count on this element.
 java.io.Serializable getKey()
          Gets the key attribute of the Element object.
 long getLastAccessTime()
          Gets the last access time.
 java.lang.Object getObjectKey()
          Gets the key attribute of the Element object.
 java.lang.Object getObjectValue()
          Gets the value attribute of the Element object as an Object.
 long getSerializedSize()
          The size of this object in serialized form.
 java.io.Serializable getValue()
          Gets the value attribute of the Element object.
 long getVersion()
          Gets the version attribute of the ElementAttributes object.
 int hashCode()
          Gets the hascode, based on the key.
 boolean isKeySerializable()
          Whether the element's key may be Serialized.
 boolean isSerializable()
          Whether the element may be Serialized.
 void resetAccessStatistics()
          Resets the hit count to 0 and the last access time to 0.
 void setCreateTime()
          Sets the creationTime attribute of the ElementAttributes object.
 void setVersion(long version)
          Sets the version attribute of the ElementAttributes object.
 java.lang.String toString()
          Returns a String representation of the Element.
 void updateAccessStatistics()
          Sets the last access time to now.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Element

public Element(java.io.Serializable key,
               java.io.Serializable value,
               long version)
A full constructor.

Creation time is set to the current time. Last Access Time and Previous To Last Access Time are not set.

Since:
.4

Element

public Element(java.lang.Object key,
               java.lang.Object value,
               long version)
A full constructor.

Creation time is set to the current time. Last Access Time and Previous To Last Access Time are not set.

Since:
1.2

Element

public Element(java.io.Serializable key,
               java.io.Serializable value)
Constructor.

Parameters:
key -
value -

Element

public Element(java.lang.Object key,
               java.lang.Object value)
Constructor.

Parameters:
key -
value -
Since:
1.2
Method Detail

getKey

public final java.io.Serializable getKey()
Gets the key attribute of the Element object.

Returns:
The key value. If the key is not Serializable, null is returned and an info log message emitted
See Also:
getObjectKey()

getObjectKey

public final java.lang.Object getObjectKey()
Gets the key attribute of the Element object.

This method is provided for those wishing to use ehcache as a memory only cache and enables retrieval of non-Serializable values from elements.

Returns:
The key as an Object. i.e no restriction is placed on it
See Also:
getKey()

getValue

public final java.io.Serializable getValue()
Gets the value attribute of the Element object.

Returns:
The value which must be Serializable. If not use getObjectValue(). If the value is not Serializable, null is returned and an info log message emitted
See Also:
getObjectValue()

getObjectValue

public final java.lang.Object getObjectValue()
Gets the value attribute of the Element object as an Object.

This method is provided for those wishing to use ehcache as a memory only cache and enables retrieval of non-Serializable values from elements.

Returns:
The value as an Object. i.e no restriction is placed on it
Since:
1.2
See Also:
getValue()

equals

public final boolean equals(java.lang.Object object)
Equals comparison with another element, based on the key.


hashCode

public final int hashCode()
Gets the hascode, based on the key.


setVersion

public final void setVersion(long version)
Sets the version attribute of the ElementAttributes object.

Parameters:
version - The new version value

getCreationTime

public final long getCreationTime()
Gets the creationTime attribute of the ElementAttributes object.

Returns:
The creationTime value

setCreateTime

public final void setCreateTime()
Sets the creationTime attribute of the ElementAttributes object.


getVersion

public final long getVersion()
Gets the version attribute of the ElementAttributes object.

Returns:
The version value

getLastAccessTime

public final long getLastAccessTime()
Gets the last access time. Access means a get. So a newly created Element will have a last access time equal to its create time.


getHitCount

public final long getHitCount()
Gets the hit count on this element.


resetAccessStatistics

public final void resetAccessStatistics()
Resets the hit count to 0 and the last access time to 0.


updateAccessStatistics

public final void updateAccessStatistics()
Sets the last access time to now.


toString

public final java.lang.String toString()
Returns a String representation of the Element.


clone

public final java.lang.Object clone()
                             throws java.lang.CloneNotSupportedException
Clones an Element. A completely new object is created, with no common references with the existing one.

This method will not work unless the Object is Serializable

Warning: This can be very slow on large object graphs. If you use this method you should write a performance test to verify suitability.

Returns:
a new Element, with exactly the same field values as the one it was cloned from.
Throws:
java.lang.CloneNotSupportedException

getSerializedSize

public final long getSerializedSize()
The size of this object in serialized form. This is not the same thing as the memory size, which is JVM dependent. Relative values should be meaningful, however.

Warning: This method can be very slow for values which contain large object graphs.

Returns:
The serialized size in bytes

isSerializable

public final boolean isSerializable()
Whether the element may be Serialized.

While Element implements Serializable, it is possible to create non Serializable elements for use in MemoryStores. This method checks that an instance of Element really is Serializable and will not throw a NonSerializableException if Serialized.

Returns:
true if the element is Serializable
Since:
1.2

isKeySerializable

public final boolean isKeySerializable()
Whether the element's key may be Serialized.

While Element implements Serializable, it is possible to create non Serializable elements and/or non Serializable keys for use in MemoryStores.

This method checks that an instance of an Element's key really is Serializable and will not throw a NonSerializableException if Serialized.

Returns:
true if the element's key is Serializable
Since:
1.2

ehcache

true