55 */
66package io .jooby ;
77
8+ import javax .annotation .Nonnull ;
9+ import javax .annotation .Nullable ;
810import java .util .HashMap ;
911import java .util .Map ;
1012
11- public class RequestScope {
13+ /**
14+ * Thread-Local request scope implementation useful for save/store request attribute and access
15+ * to them using a static way.
16+ *
17+ * This is part of public API but usage must be keep to minimum.
18+ *
19+ * @author edgar
20+ */
21+ public final class RequestScope {
1222
1323 private static final ThreadLocal <Map <Object , Object >> CONTEXT_TL = new ThreadLocal <>();
1424
25+ private RequestScope () {
26+ }
27+
1528 /**
1629 * Check to see if there is already a value associated with the current
1730 * thread for the given key.
1831 *
1932 * @param key The key against which to check for a given value within the current thread.
2033 * @return True if there is currently a session bound.
2134 */
22- public static boolean hasBind (Object key ) {
35+ public static boolean hasBind (@ Nonnull Object key ) {
2336 return get (key ) != null ;
2437 }
2538
@@ -28,21 +41,23 @@ public static boolean hasBind(Object key) {
2841 *
2942 * @param key The key to be bound.
3043 * @param value The value to be bound.
44+ * @param <T> Bind type.
3145 * @return Any previously bound session (should be null in most cases).
3246 */
33- public static <T > T bind (Object key , T value ) {
34- return (T ) sessionMap (true ).put (key , value );
47+ public static @ Nullable <T > T bind (@ Nonnull Object key , @ Nonnull T value ) {
48+ return (T ) threadMap (true ).put (key , value );
3549 }
3650
3751 /**
3852 * Unbinds the session (if one) current associated with the context for the
3953 * given session.
4054 *
4155 * @param key The factory for which to unbind the current session.
56+ * @param <T> Bind type.
4257 * @return The bound session if one, else null.
4358 */
44- public static <T > T unbind (Object key ) {
45- final Map <Object , Object > sessionMap = sessionMap ();
59+ public static @ Nullable <T > T unbind (@ Nonnull Object key ) {
60+ final Map <Object , Object > sessionMap = threadMap ();
4661 T existing = null ;
4762 if (sessionMap != null ) {
4863 existing = (T ) sessionMap .remove (key );
@@ -51,20 +66,27 @@ public static <T> T unbind(Object key) {
5166 return existing ;
5267 }
5368
54- public static <T > T get (Object key ) {
55- final Map <Object , Object > sessionMap = sessionMap ();
69+ /**
70+ * Get a previously bind value for the given key or <code>null</code>.
71+ *
72+ * @param key Key.
73+ * @param <T> Object type.
74+ * @return Binded value or <code>null</code>.
75+ */
76+ public static @ Nullable <T > T get (@ Nonnull Object key ) {
77+ final Map <Object , Object > sessionMap = threadMap ();
5678 if (sessionMap == null ) {
5779 return null ;
5880 } else {
5981 return (T ) sessionMap .get (key );
6082 }
6183 }
6284
63- private static Map <Object , Object > sessionMap () {
64- return sessionMap (false );
85+ private static Map <Object , Object > threadMap () {
86+ return threadMap (false );
6587 }
6688
67- private static Map <Object , Object > sessionMap (boolean createMap ) {
89+ private static Map <Object , Object > threadMap (boolean createMap ) {
6890 Map <Object , Object > sessionMap = CONTEXT_TL .get ();
6991 if (sessionMap == null && createMap ) {
7092 sessionMap = new HashMap <>();
@@ -74,7 +96,7 @@ private static Map<Object, Object> sessionMap(boolean createMap) {
7496 }
7597
7698 private static void doCleanup () {
77- final Map <Object , Object > ctx = sessionMap (false );
99+ final Map <Object , Object > ctx = threadMap (false );
78100 if (ctx != null ) {
79101 if (ctx .isEmpty ()) {
80102 CONTEXT_TL .remove ();
0 commit comments