All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.simter.Context Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package tech.simter;

import java.util.HashMap;
import java.util.Map;

/**
 * A simple and powerful utils class for share data during the same thread lifecycle
 * 

This tools use thread-local variables to make each thread has its own initialized share data. * You don't need to transfer context data through method arguments, just use `Context.get(key)` to get * its value inside the method. *

For example: *

 * // set share data anywhere
 * Context.set("userId", new Long(0));
 * Context.set("userName", "RJ");
 *
 * void someMethodInOtherClass(){
 *   // get shared data by key anywhere
 *   Long userId = Context.get("userId");
 *   String userName = Context.get("userName");
 *
 *   // or get all shared data
 *   Map<String, Object> all = Context.get();
 *   ...
 * }
 *
 * // delete shared data anywhere
 * Context.remove("userId");
 * 
*

You can see that it just like to get a static constant value. But you need to know the difference. * A static constant always has the same value event in different thread. * The context data is isolated between each thread. * * @author RJ */ public class Context { // containing the thread-local share data private static ThreadLocal> share = ThreadLocal.withInitial(HashMap::new); /** * Get all share date * * @return all share date during thread lifecycle */ public static Map get() { return share.get(); } /** * Get the specific key value * * @param key the key * @param the expected return type * @return the value or null if there was no mapping for key */ @SuppressWarnings("unchecked") public static V get(String key) { return (V) share.get().get(key); } /** * Set the specific key value * * @param key the key * @param value the value */ public static void set(String key, Object value) { share.get().put(key, value); } /** * Remove the specific key * * @param key the key * @param the expected return type * @return the previous value associated with key, or null if there was no mapping for key. */ @SuppressWarnings("unchecked") public static V remove(String key) { return (V) share.get().remove(key); } /** * Removes all of the share data from this context. The context will be empty after this call returns. */ public static void clear() { share.get().clear(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy