Interface SessionData
-
public interface SessionDataThe 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
SessionSerializeris not provided for a given get/set, thegetDefaultSerializer()will be used. TheSessionModuleprovides 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 voidclear()Remove all entries from the session data.default <T> java.util.Optional<T>get(java.lang.Class<T> type)Read the object with the given type, and no name, using thedefault serializer.default <T> java.util.Optional<T>get(java.lang.Class<T> type, SessionSerializer serializer)Read the object with the given type, and no name.default java.util.Optional<?>get(java.lang.String name)Read the object with the given name, using thedefault serializer.default java.util.Optional<?>get(java.lang.String name, SessionSerializer serializer)Read the object with the given name.default <T> java.util.Optional<T>get(SessionKey<T> key)Fetch the object with the given key, using thedefault serializer.<T> java.util.Optional<T>get(SessionKey<T> key, SessionSerializer serializer)Read the object with the given key.default SessionSerializergetDefaultSerializer()default JavaSessionSerializergetJavaSerializer()java.util.Set<SessionKey<?>>getKeys()The keys of all objects currently in the session.SessiongetSession()The correspondingSessionobject.default booleanisDirty()SeeSession.isDirty().default voidremove(java.lang.Class<?> type)Removes the object with the given type and no name, if it exists.default voidremove(java.lang.String name)Removes the object with the name, if it exists.voidremove(SessionKey<?> key)Removes the object with the given key, if it exists.default <T> Trequire(java.lang.Class<T> type)Likeget(Class), but throwsNoSuchElementExceptionon the absence of a value.default <T> Trequire(java.lang.Class<T> type, SessionSerializer serializer)Likeget(Class, SessionSerializer), but throwsNoSuchElementExceptionon the absence of a value.default java.lang.Objectrequire(java.lang.String name)Likeget(String), but throwsNoSuchElementExceptionon the absence of a value.default java.lang.Objectrequire(java.lang.String name, SessionSerializer serializer)Likeget(String, SessionSerializer), but throwsNoSuchElementExceptionon the absence of a value.default <T> Trequire(SessionKey<T> key)Likeget(SessionKey), but throwsNoSuchElementExceptionon the absence of a value.default <T> Trequire(SessionKey<T> key, SessionSerializer serializer)Likeget(SessionKey, SessionSerializer), but throwsNoSuchElementExceptionon the absence of a value.default Operationsave()SeeSession.save().default <T> voidset(java.lang.Class<T> type, T value)Sets the value for the given type, using thedefault serializer.default <T> voidset(java.lang.Class<T> type, T value, SessionSerializer serializer)Sets the value for the given type.default <T> voidset(java.lang.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> voidset(java.lang.String name, T value, SessionSerializer serializer)Sets the value for the given name and type, using the runtime type of the value.default <T> voidset(SessionKey<T> key, T value)Sets the value for the given key, using thedefault serializer.<T> voidset(SessionKey<T> key, T value, SessionSerializer serializer)Sets the value for the given key.default <T> voidset(T value)Sets the value for the type, using the runtime type of the value and thedefault serializer.default <T> voidset(T value, SessionSerializer serializer)Sets the value for the type, using the runtime type of the value.default Operationterminate()SeeSession.terminate().
-
-
-
Method Detail
-
getKeys
java.util.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> java.util.Optional<T> get(SessionKey<T> key) throws java.lang.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:
java.lang.Exception- See Also:
require(SessionKey)
-
get
<T> java.util.Optional<T> get(SessionKey<T> key, SessionSerializer serializer) throws java.lang.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:
java.lang.Exception- See Also:
require(SessionKey, SessionSerializer)
-
get
default java.util.Optional<?> get(java.lang.String name) throws java.lang.ExceptionRead the object with the given name, using thedefault serializer.This method will throw an
IllegalArgumentExceptionif 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:
java.lang.Exception- See Also:
require(String)
-
get
default java.util.Optional<?> get(java.lang.String name, SessionSerializer serializer) throws java.lang.ExceptionRead the object with the given name.This method will throw an
IllegalArgumentExceptionif 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:
java.lang.Exception- See Also:
require(String, SessionSerializer)
-
get
default <T> java.util.Optional<T> get(java.lang.Class<T> type) throws java.lang.ExceptionRead 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:
java.lang.Exception- See Also:
require(Class)
-
get
default <T> java.util.Optional<T> get(java.lang.Class<T> type, SessionSerializer serializer) throws java.lang.ExceptionRead 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:
java.lang.Exception- See Also:
require(Class, SessionSerializer)
-
require
default <T> T require(SessionKey<T> key) throws java.lang.Exception
Likeget(SessionKey), but throwsNoSuchElementExceptionon the absence of a value.- Type Parameters:
T- the type- Parameters:
key- the object key- Returns:
- the value for the given key
- Throws:
java.lang.Exception
-
require
default <T> T require(SessionKey<T> key, SessionSerializer serializer) throws java.lang.Exception
Likeget(SessionKey, SessionSerializer), but throwsNoSuchElementExceptionon 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:
java.lang.Exception
-
require
default <T> T require(java.lang.Class<T> type) throws java.lang.ExceptionLikeget(Class), but throwsNoSuchElementExceptionon the absence of a value.- Type Parameters:
T- the type- Parameters:
type- the type- Returns:
- the value for the given key
- Throws:
java.lang.Exception
-
require
default <T> T require(java.lang.Class<T> type, SessionSerializer serializer) throws java.lang.ExceptionLikeget(Class, SessionSerializer), but throwsNoSuchElementExceptionon the absence of a value.- Type Parameters:
T- the type- Parameters:
type- the typeserializer- the serializer- Returns:
- the value for the given key
- Throws:
java.lang.Exception
-
require
default java.lang.Object require(java.lang.String name) throws java.lang.ExceptionLikeget(String), but throwsNoSuchElementExceptionon the absence of a value.This method will throw an
IllegalArgumentExceptionif 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:
java.lang.Exception
-
require
default java.lang.Object require(java.lang.String name, SessionSerializer serializer) throws java.lang.ExceptionLikeget(String, SessionSerializer), but throwsNoSuchElementExceptionon the absence of a value.This method will throw an
IllegalArgumentExceptionif 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:
java.lang.Exception
-
set
default <T> void set(SessionKey<T> key, T value) throws java.lang.Exception
Sets the value for the given key, using thedefault serializer.- Type Parameters:
T- the type- Parameters:
key- the keyvalue- the value- Throws:
java.lang.Exception
-
set
<T> void set(SessionKey<T> key, T value, SessionSerializer serializer) throws java.lang.Exception
Sets the value for the given key.- Type Parameters:
T- the type- Parameters:
key- the keyvalue- the valueserializer- the serializer- Throws:
java.lang.Exception
-
set
default <T> void set(java.lang.Class<T> type, T value) throws java.lang.ExceptionSets the value for the given type, using thedefault serializer.- Type Parameters:
T- the type- Parameters:
type- the typevalue- the value- Throws:
java.lang.Exception
-
set
default <T> void set(java.lang.Class<T> type, T value, SessionSerializer serializer) throws java.lang.ExceptionSets the value for the given type.- Type Parameters:
T- the type- Parameters:
type- the typevalue- the valueserializer- the serializer- Throws:
java.lang.Exception
-
set
default <T> void set(java.lang.String name, T value) throws java.lang.ExceptionSets 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:
java.lang.Exception
-
set
default <T> void set(java.lang.String name, T value, SessionSerializer serializer) throws java.lang.ExceptionSets 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:
java.lang.Exception
-
set
default <T> void set(T value) throws java.lang.ExceptionSets 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:
java.lang.Exception
-
set
default <T> void set(T value, SessionSerializer serializer) throws java.lang.ExceptionSets the value for the type, using the runtime type of the value.- Type Parameters:
T- the type- Parameters:
value- the valueserializer- the serializer- Throws:
java.lang.Exception
-
remove
void remove(SessionKey<?> key)
Removes the object with the given key, if it exists.- Parameters:
key- the key
-
remove
default void remove(java.lang.Class<?> type)
Removes the object with the given type and no name, if it exists.- Parameters:
type- the type
-
remove
default void remove(java.lang.String name)
Removes the object with the name, if it exists.This method will throw a
NoSuchElementExceptionif 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
Serializableobjects
-
-