public interface SessionData
The session data can be obtained via the Session.getData()
method.
Every item is stored with a SessionKey
.
A key is comprised of a type (as a Class
) and an optional name.
The combination of type and name identifies the value.
Two keys with differing types but identical names are not considered to be equivalent.
When writing session data, the values are serialized immediately. That is, all objects are treated as immutable value objects from the perspective of this object. Any changes made to an object after it has been written to this object will NOT be persisted. If such changes are to be persisted, the object must be written again.
If a SessionSerializer
is not provided for a given get/set, the getDefaultSerializer()
will be used.
The SessionModule
provides a default implementation based on Java serialization.
An alternative implementation can be used by overriding the Guice binding for SessionSerializer
.
The session data is held in memory for a given request until the Session.save()
method is called on the corresponding session.
However, this object internally holds the data in serialized form.
Therefore, every read and write incurs the cost of serialization and deserialization.
Modifier and Type | Method and Description |
---|---|
void |
clear()
Remove all entries from the session data.
|
default <T> Optional<T> |
get(Class<T> type)
Read the object with the given type, and no name, using the
default serializer . |
default <T> Optional<T> |
get(Class<T> type,
SessionSerializer serializer)
Read the object with the given type, and no name.
|
default <T> Optional<T> |
get(SessionKey<T> key)
Fetch the object with the given key, using the
default serializer . |
<T> Optional<T> |
get(SessionKey<T> key,
SessionSerializer serializer)
Read the object with the given key.
|
default Optional<?> |
get(String name)
Read the object with the given name, using the
default serializer . |
default Optional<?> |
get(String name,
SessionSerializer serializer)
Read the object with the given name.
|
default SessionSerializer |
getDefaultSerializer()
|
default JavaSessionSerializer |
getJavaSerializer()
|
Set<SessionKey<?>> |
getKeys()
The keys of all objects currently in the session.
|
Session |
getSession()
The corresponding
Session object. |
default boolean |
isDirty()
See
Session.isDirty() . |
default void |
remove(Class<?> type)
Removes the object with the given type and no name, if it exists.
|
void |
remove(SessionKey<?> key)
Removes the object with the given key, if it exists.
|
default void |
remove(String name)
Removes the object with the name, if it exists.
|
default <T> T |
require(Class<T> type)
Like
get(Class) , but throws NoSuchElementException on the absence of a value. |
default <T> T |
require(Class<T> type,
SessionSerializer serializer)
Like
get(Class, SessionSerializer) , but throws NoSuchElementException on the absence of a value. |
default <T> T |
require(SessionKey<T> key)
Like
get(SessionKey) , but throws NoSuchElementException on the absence of a value. |
default <T> T |
require(SessionKey<T> key,
SessionSerializer serializer)
Like
get(SessionKey, SessionSerializer) , but throws NoSuchElementException on the absence of a value. |
default Object |
require(String name)
Like
get(String) , but throws NoSuchElementException on the absence of a value. |
default Object |
require(String name,
SessionSerializer serializer)
Like
get(String, SessionSerializer) , but throws NoSuchElementException on the absence of a value. |
default Operation |
save()
See
Session.save() . |
default <T> void |
set(Class<T> type,
T value)
Sets the value for the given type, using the
default serializer . |
default <T> void |
set(Class<T> type,
T value,
SessionSerializer serializer)
Sets the value for the given type.
|
default <T> void |
set(SessionKey<T> key,
T value)
Sets the value for the given key, using the
default serializer . |
<T> void |
set(SessionKey<T> key,
T value,
SessionSerializer serializer)
Sets the value for the given key.
|
default <T> void |
set(String name,
T value)
Sets the value for the given name and type, using the runtime type of the value and the
default serializer . |
default <T> void |
set(String name,
T value,
SessionSerializer serializer)
Sets the value for the given name and type, using the runtime type of the value.
|
default <T> void |
set(T value)
Sets the value for the type, using the runtime type of the value and the
default serializer . |
default <T> void |
set(T value,
SessionSerializer serializer)
Sets the value for the type, using the runtime type of the value.
|
default Operation |
terminate()
See
Session.terminate() . |
Set<SessionKey<?>> getKeys()
default <T> Optional<T> get(SessionKey<T> key)
default serializer
.T
- the type of objectkey
- the keyrequire(SessionKey)
<T> Optional<T> get(SessionKey<T> key, SessionSerializer serializer)
T
- the type of objectkey
- the keyserializer
- the serializerrequire(SessionKey, SessionSerializer)
default Optional<?> get(String name)
default serializer
.
This method will throw an IllegalArgumentException
if there is more than one object who's key has the given name.
name
- the object namerequire(String)
default Optional<?> get(String name, SessionSerializer serializer)
This method will throw an IllegalArgumentException
if there is more than one object who's key has the given name.
name
- the object nameserializer
- the serializerrequire(String, SessionSerializer)
default <T> Optional<T> get(Class<T> type)
default serializer
.T
- the typetype
- the typerequire(Class)
default <T> Optional<T> get(Class<T> type, SessionSerializer serializer)
T
- the typetype
- the typeserializer
- the serializerrequire(Class, SessionSerializer)
default <T> T require(SessionKey<T> key)
get(SessionKey)
, but throws NoSuchElementException
on the absence of a value.T
- the typekey
- the object keydefault <T> T require(SessionKey<T> key, SessionSerializer serializer)
get(SessionKey, SessionSerializer)
, but throws NoSuchElementException
on the absence of a value.T
- the typekey
- the object keyserializer
- the serializerdefault <T> T require(Class<T> type)
get(Class)
, but throws NoSuchElementException
on the absence of a value.T
- the typetype
- the typedefault <T> T require(Class<T> type, SessionSerializer serializer)
get(Class, SessionSerializer)
, but throws NoSuchElementException
on the absence of a value.T
- the typetype
- the typeserializer
- the serializerdefault Object require(String name)
get(String)
, but throws NoSuchElementException
on the absence of a value.
This method will throw an IllegalArgumentException
if there is more than one object who's key has the given name.
name
- the object namedefault Object require(String name, SessionSerializer serializer)
get(String, SessionSerializer)
, but throws NoSuchElementException
on the absence of a value.
This method will throw an IllegalArgumentException
if there is more than one object who's key has the given name.
name
- the object nameserializer
- the serializerdefault <T> void set(SessionKey<T> key, T value)
default serializer
.T
- the typekey
- the keyvalue
- the value<T> void set(SessionKey<T> key, T value, SessionSerializer serializer)
T
- the typekey
- the keyvalue
- the valueserializer
- the serializerdefault <T> void set(Class<T> type, T value)
default serializer
.T
- the typetype
- the typevalue
- the valuedefault <T> void set(Class<T> type, T value, SessionSerializer serializer)
T
- the typetype
- the typevalue
- the valueserializer
- the serializerdefault <T> void set(String name, T value)
default serializer
.T
- the typename
- the namevalue
- the valuedefault <T> void set(String name, T value, SessionSerializer serializer)
T
- the typename
- the namevalue
- the valueserializer
- the serializerdefault <T> void set(T value)
default serializer
.T
- the typevalue
- the valuedefault <T> void set(T value, SessionSerializer serializer)
T
- the typevalue
- the valueserializer
- the serializervoid remove(SessionKey<?> key)
key
- the keydefault void remove(Class<?> type)
type
- the typedefault void remove(String name)
This method will throw a NoSuchElementException
if there are more than one entries with the given name.
name
- the namevoid clear()
default boolean isDirty()
Session.isDirty()
.default Operation save()
Session.save()
.default Operation terminate()
Session.terminate()
.default SessionSerializer getDefaultSerializer()
default JavaSessionSerializer getJavaSerializer()
Serializable
objects