Interface SessionData
-
public interface SessionData
The data associated with the user session.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 aClass
) 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, thegetDefaultSerializer()
will be used. TheSessionModule
provides a default implementation based on Java serialization. An alternative implementation can be used by overriding the Guice binding forSessionSerializer
.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.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method 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 thedefault serializer
.default <T> Optional<T>
get(Class<T> type, SessionSerializer serializer)
Read the object with the given type, and no name.default Optional<?>
get(String name)
Read the object with the given name, using thedefault serializer
.default Optional<?>
get(String name, SessionSerializer serializer)
Read the object with the given name.default <T> Optional<T>
get(SessionKey<T> key)
Fetch the object with the given key, using thedefault serializer
.<T> Optional<T>
get(SessionKey<T> key, SessionSerializer serializer)
Read the object with the given key.default SessionSerializer
getDefaultSerializer()
default JavaSessionSerializer
getJavaSerializer()
Set<SessionKey<?>>
getKeys()
The keys of all objects currently in the session.Session
getSession()
The correspondingSession
object.default boolean
isDirty()
SeeSession.isDirty()
.default void
remove(Class<?> type)
Removes the object with the given type and no name, if it exists.default void
remove(String name)
Removes the object with the name, if it exists.void
remove(SessionKey<?> key)
Removes the object with the given key, if it exists.default <T> T
require(Class<T> type)
Likeget(Class)
, but throwsNoSuchElementException
on the absence of a value.default <T> T
require(Class<T> type, SessionSerializer serializer)
Likeget(Class, SessionSerializer)
, but throwsNoSuchElementException
on the absence of a value.default Object
require(String name)
Likeget(String)
, but throwsNoSuchElementException
on the absence of a value.default Object
require(String name, SessionSerializer serializer)
Likeget(String, SessionSerializer)
, but throwsNoSuchElementException
on the absence of a value.default <T> T
require(SessionKey<T> key)
Likeget(SessionKey)
, but throwsNoSuchElementException
on the absence of a value.default <T> T
require(SessionKey<T> key, SessionSerializer serializer)
Likeget(SessionKey, SessionSerializer)
, but throwsNoSuchElementException
on the absence of a value.default Operation
save()
SeeSession.save()
.default <T> void
set(Class<T> type, T value)
Sets the value for the given type, using thedefault serializer
.default <T> void
set(Class<T> type, T value, SessionSerializer serializer)
Sets the value for the given type.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 thedefault 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(SessionKey<T> key, T value)
Sets the value for the given key, using thedefault serializer
.<T> void
set(SessionKey<T> key, T value, SessionSerializer serializer)
Sets the value for the given key.default <T> void
set(T value)
Sets the value for the type, using the runtime type of the value and thedefault 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()
SeeSession.terminate()
.
-
-
-
Method Detail
-
getKeys
Set<SessionKey<?>> getKeys()
The keys of all objects currently in the session.- Returns:
- the keys of all objects currently in the session
-
get
default <T> Optional<T> get(SessionKey<T> key) throws Exception
Fetch the object with the given key, using thedefault serializer
.- Type Parameters:
T
- the type of object- Parameters:
key
- the key- Returns:
- the value for the given key
- Throws:
Exception
- See Also:
require(SessionKey)
-
get
<T> Optional<T> get(SessionKey<T> key, SessionSerializer serializer) throws Exception
Read the object with the given key.- Type Parameters:
T
- the type of object- Parameters:
key
- the keyserializer
- the serializer- Returns:
- the value for the given key
- Throws:
Exception
- See Also:
require(SessionKey, SessionSerializer)
-
get
default Optional<?> get(String name) throws Exception
Read the object with the given name, using thedefault serializer
.This method will throw an
IllegalArgumentException
if there is more than one object who's key has the given name.- Parameters:
name
- the object name- Returns:
- the value for the given key
- Throws:
Exception
- See Also:
require(String)
-
get
default Optional<?> get(String name, SessionSerializer serializer) throws Exception
Read the object with the given name.This method will throw an
IllegalArgumentException
if there is more than one object who's key has the given name.- Parameters:
name
- the object nameserializer
- the serializer- Returns:
- the value for the given key
- Throws:
Exception
- See Also:
require(String, SessionSerializer)
-
get
default <T> Optional<T> get(Class<T> type) throws Exception
Read the object with the given type, and no name, using thedefault serializer
.- Type Parameters:
T
- the type- Parameters:
type
- the type- Returns:
- the value for the given key
- Throws:
Exception
- See Also:
require(Class)
-
get
default <T> Optional<T> get(Class<T> type, SessionSerializer serializer) throws Exception
Read the object with the given type, and no name.- Type Parameters:
T
- the type- Parameters:
type
- the typeserializer
- the serializer- Returns:
- the value for the given key
- Throws:
Exception
- See Also:
require(Class, SessionSerializer)
-
require
default <T> T require(SessionKey<T> key) throws Exception
Likeget(SessionKey)
, but throwsNoSuchElementException
on the absence of a value.- Type Parameters:
T
- the type- Parameters:
key
- the object key- Returns:
- the value for the given key
- Throws:
Exception
-
require
default <T> T require(SessionKey<T> key, SessionSerializer serializer) throws Exception
Likeget(SessionKey, SessionSerializer)
, but throwsNoSuchElementException
on the absence of a value.- Type Parameters:
T
- the type- Parameters:
key
- the object keyserializer
- the serializer- Returns:
- the value for the given key
- Throws:
Exception
-
require
default <T> T require(Class<T> type) throws Exception
Likeget(Class)
, but throwsNoSuchElementException
on the absence of a value.- Type Parameters:
T
- the type- Parameters:
type
- the type- Returns:
- the value for the given key
- Throws:
Exception
-
require
default <T> T require(Class<T> type, SessionSerializer serializer) throws Exception
Likeget(Class, SessionSerializer)
, but throwsNoSuchElementException
on the absence of a value.- Type Parameters:
T
- the type- Parameters:
type
- the typeserializer
- the serializer- Returns:
- the value for the given key
- Throws:
Exception
-
require
default Object require(String name) throws Exception
Likeget(String)
, but throwsNoSuchElementException
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.- Parameters:
name
- the object name- Returns:
- the value for the given key
- Throws:
Exception
-
require
default Object require(String name, SessionSerializer serializer) throws Exception
Likeget(String, SessionSerializer)
, but throwsNoSuchElementException
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.- Parameters:
name
- the object nameserializer
- the serializer- Returns:
- the value for the given key
- Throws:
Exception
-
set
default <T> void set(SessionKey<T> key, T value) throws Exception
Sets the value for the given key, using thedefault serializer
.- Type Parameters:
T
- the type- Parameters:
key
- the keyvalue
- the value- Throws:
Exception
-
set
<T> void set(SessionKey<T> key, T value, SessionSerializer serializer) throws Exception
Sets the value for the given key.- Type Parameters:
T
- the type- Parameters:
key
- the keyvalue
- the valueserializer
- the serializer- Throws:
Exception
-
set
default <T> void set(Class<T> type, T value) throws Exception
Sets the value for the given type, using thedefault serializer
.- Type Parameters:
T
- the type- Parameters:
type
- the typevalue
- the value- Throws:
Exception
-
set
default <T> void set(Class<T> type, T value, SessionSerializer serializer) throws Exception
Sets the value for the given type.- Type Parameters:
T
- the type- Parameters:
type
- the typevalue
- the valueserializer
- the serializer- Throws:
Exception
-
set
default <T> void set(String name, T value) throws Exception
Sets the value for the given name and type, using the runtime type of the value and thedefault serializer
.- Type Parameters:
T
- the type- Parameters:
name
- the namevalue
- the value- Throws:
Exception
-
set
default <T> void set(String name, T value, SessionSerializer serializer) throws Exception
Sets the value for the given name and type, using the runtime type of the value.- Type Parameters:
T
- the type- Parameters:
name
- the namevalue
- the valueserializer
- the serializer- Throws:
Exception
-
set
default <T> void set(T value) throws Exception
Sets the value for the type, using the runtime type of the value and thedefault serializer
.- Type Parameters:
T
- the type- Parameters:
value
- the value- Throws:
Exception
-
set
default <T> void set(T value, SessionSerializer serializer) throws Exception
Sets the value for the type, using the runtime type of the value.- Type Parameters:
T
- the type- Parameters:
value
- the valueserializer
- the serializer- Throws:
Exception
-
remove
void remove(SessionKey<?> key)
Removes the object with the given key, if it exists.- Parameters:
key
- the key
-
remove
default void remove(Class<?> type)
Removes the object with the given type and no name, if it exists.- Parameters:
type
- the type
-
remove
default void remove(String name)
Removes the object with the name, if it exists.This method will throw a
NoSuchElementException
if there are more than one entries with the given name.- Parameters:
name
- the name
-
clear
void clear()
Remove all entries from the session data.
-
isDirty
default boolean isDirty()
SeeSession.isDirty()
.- Returns:
- whether or not any changes have been made to the session data since it was accessed
-
save
default Operation save()
SeeSession.save()
.- Returns:
- the save operation
-
terminate
default Operation terminate()
SeeSession.terminate()
.- Returns:
- the terminate operation
-
getDefaultSerializer
default SessionSerializer getDefaultSerializer()
- Returns:
- the serializer to use by default
-
getJavaSerializer
default JavaSessionSerializer getJavaSerializer()
- Returns:
- a serializer for
Serializable
objects
-
-