net.sf.ehcache

Class Element

public final class Element extends Object implements Serializable, 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

UNKNOWN: SerializableHasSerializationMethods

Constructor Summary
Element(Serializable key, Serializable value, long version)
A full constructor.
Element(Object key, Object value, long version)
A full constructor.
Element(Serializable key, Serializable value)
Constructor.
Element(Object key, Object value)
Constructor.
Method Summary
Objectclone()
Clones an Element.
booleanequals(Object object)
Equals comparison with another element, based on the key.
longgetCreationTime()
Gets the creationTime attribute of the ElementAttributes object.
longgetHitCount()
Gets the hit count on this element.
SerializablegetKey()
Gets the key attribute of the Element object.
longgetLastAccessTime()
Gets the last access time.
longgetNextToLastAccessTime()
Gets the next to last access time.
ObjectgetObjectKey()
Gets the key attribute of the Element object.
ObjectgetObjectValue()
Gets the value attribute of the Element object as an Object.
longgetSerializedSize()
The size of this object in serialized form.
SerializablegetValue()
Gets the value attribute of the Element object.
longgetVersion()
Gets the version attribute of the ElementAttributes object.
inthashCode()
Gets the hascode, based on the key.
booleanisKeySerializable()
Whether the element's key may be Serialized.
booleanisSerializable()
Whether the element may be Serialized.
voidresetAccessStatistics()
Resets the hit count to 0 and the last access time to 0.
voidsetCreateTime()
Sets the creationTime attribute of the ElementAttributes object.
voidsetVersion(long version)
Sets the version attribute of the ElementAttributes object.
StringtoString()
Returns a String representation of the Element.
voidupdateAccessStatistics()
Sets the last access time to now.

Constructor Detail

Element

public Element(Serializable key, 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(Object key, 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(Serializable key, Serializable value)
Constructor.

Parameters: key value

Element

public Element(Object key, Object value)
Constructor.

Parameters: key value

Since: 1.2

Method Detail

clone

public final Object clone()
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: CloneNotSupportedException

equals

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

getCreationTime

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

Returns: The creationTime value

getHitCount

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

getKey

public final 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

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.

getNextToLastAccessTime

final long getNextToLastAccessTime()
Gets the next to last access time. This is package protected as it should not be used outside internal Cache housekeeping.

See Also: getLastAccessTime

getObjectKey

public final 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

getObjectValue

public final 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

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

getValue

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

Returns: The value which must be Serializable. If not use Element. If the value is not Serializable, null is returned and an info log message emitted

See Also: getObjectValue

getVersion

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

Returns: The version value

hashCode

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

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

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

resetAccessStatistics

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

setCreateTime

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

setVersion

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

Parameters: version The new version value

toString

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

updateAccessStatistics

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