From 012235d05d8dc7c2decdc7229d93033b0399ecbb Mon Sep 17 00:00:00 2001
From: xiejun <xiejun@vci-tech.com>
Date: 星期日, 10 十一月 2024 15:49:53 +0800
Subject: [PATCH] 集成获取mdm分发通用数据格式接口集成
---
Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ThreadLocalUtil.java | 136 +++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 136 insertions(+), 0 deletions(-)
diff --git a/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ThreadLocalUtil.java b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ThreadLocalUtil.java
new file mode 100644
index 0000000..580b223
--- /dev/null
+++ b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ThreadLocalUtil.java
@@ -0,0 +1,136 @@
+/*
+ *
+ * Copyright 2019 http://www.hswebframework.org
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.springblade.core.tool.utils;
+
+import org.springframework.lang.Nullable;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Supplier;
+
+/**
+ * ThreadLocal 宸ュ叿绫�,閫氳繃鍦═hreadLocal瀛樺偍map淇℃伅,鏉ュ疄鐜板湪ThreadLocal涓淮鎶ゅ涓俊鎭�
+ * <br>e.g.<code>
+ * ThreadLocalUtils.put("key",value);<br>
+ * ThreadLocalUtils.get("key");<br>
+ * ThreadLocalUtils.remove("key");<br>
+ * ThreadLocalUtils.getAndRemove("key");<br>
+ * ThreadLocalUtils.get("key",()->defaultValue);<br>
+ * ThreadLocalUtils.clear();<br>
+ * </code>
+ *
+ * @author zhouhao
+ * @since 2.0
+ */
+@SuppressWarnings("unchecked")
+public class ThreadLocalUtil {
+ private static final ThreadLocal<Map<String, Object>> LOCAL = ThreadLocal.withInitial(HashMap::new);
+
+ /**
+ * @return threadLocal涓殑鍏ㄩ儴鍊�
+ */
+ public static Map<String, Object> getAll() {
+ return new HashMap<>(LOCAL.get());
+ }
+
+ /**
+ * 璁剧疆涓�涓�煎埌ThreadLocal
+ *
+ * @param key 閿�
+ * @param value 鍊�
+ * @param <T> 鍊肩殑绫诲瀷
+ * @return 琚斁鍏ョ殑鍊�
+ * @see Map#put(Object, Object)
+ */
+ public static <T> T put(String key, T value) {
+ LOCAL.get().put(key, value);
+ return value;
+ }
+
+ /**
+ * 璁剧疆涓�涓�煎埌ThreadLocal
+ *
+ * @param map map
+ * @return 琚斁鍏ョ殑鍊�
+ * @see Map#putAll(Map)
+ */
+ public static void put(Map<String, Object> map) {
+ LOCAL.get().putAll(map);
+ }
+
+ /**
+ * 鍒犻櫎鍙傛暟瀵瑰簲鐨勫��
+ *
+ * @param key
+ * @see Map#remove(Object)
+ */
+ public static void remove(String key) {
+ LOCAL.get().remove(key);
+ }
+
+ /**
+ * 娓呯┖ThreadLocal
+ *
+ * @see Map#clear()
+ */
+ public static void clear() {
+ LOCAL.remove();
+ }
+
+ /**
+ * 浠嶵hreadLocal涓幏鍙栧��
+ *
+ * @param key 閿�
+ * @param <T> 鍊兼硾鍨�
+ * @return 鍊�, 涓嶅瓨鍦ㄥ垯杩斿洖null, 濡傛灉绫诲瀷涓庢硾鍨嬩笉涓�鑷�, 鍙兘鎶涘嚭{@link ClassCastException}
+ * @see Map#get(Object)
+ * @see ClassCastException
+ */
+ @Nullable
+ public static <T> T get(String key) {
+ return ((T) LOCAL.get().get(key));
+ }
+
+ /**
+ * 浠嶵hreadLocal涓幏鍙栧��,骞舵寚瀹氫竴涓綋鍊间笉瀛樺湪鐨勬彁渚涜��
+ *
+ * @see Supplier
+ */
+ @Nullable
+ public static <T> T getIfAbsent(String key, Supplier<T> supplierOnNull) {
+ return ((T) LOCAL.get().computeIfAbsent(key, k -> supplierOnNull.get()));
+ }
+
+ /**
+ * 鑾峰彇涓�涓�煎悗鐒跺悗鍒犻櫎鎺�
+ *
+ * @param key 閿�
+ * @param <T> 鍊肩被鍨�
+ * @return 鍊�, 涓嶅瓨鍦ㄥ垯杩斿洖null
+ * @see this#get(String)
+ * @see this#remove(String)
+ */
+ public static <T> T getAndRemove(String key) {
+ try {
+ return get(key);
+ } finally {
+ remove(key);
+ }
+ }
+
+}
--
Gitblit v1.9.3