mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add method for extra
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package cn.hutool.core.lang;
|
||||
|
||||
import cn.hutool.core.lang.func.Func0;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
@@ -30,11 +31,26 @@ public final class Singleton {
|
||||
* @param params 构造方法参数
|
||||
* @return 单例对象
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T get(Class<T> clazz, Object... params) {
|
||||
Assert.notNull(clazz, "Class must be not null !");
|
||||
final String key = buildKey(clazz.getName(), params);
|
||||
return (T) POOL.get(key, () -> ReflectUtil.newInstance(clazz, params));
|
||||
return get(key, () -> ReflectUtil.newInstance(clazz, params));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定类的单例对象<br>
|
||||
* 对象存在于池中返回,否则创建,每次调用此方法获得的对象为同一个对象<br>
|
||||
* 注意:单例针对的是类和对象,因此get方法第一次调用时创建的对象始终唯一,也就是说就算参数变更,返回的依旧是第一次创建的对象
|
||||
*
|
||||
* @param <T> 单例对象类型
|
||||
* @param key 自定义键
|
||||
* @param supplier 单例对象的创建函数
|
||||
* @return 单例对象
|
||||
* @since 5.3.3
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T get(String key, Func0<T> supplier) {
|
||||
return (T) POOL.get(key, supplier::call);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,7 +76,18 @@ public final class Singleton {
|
||||
*/
|
||||
public static void put(Object obj) {
|
||||
Assert.notNull(obj, "Bean object must be not null !");
|
||||
POOL.put(obj.getClass().getName(), obj);
|
||||
put(obj.getClass().getName(), obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将已有对象放入单例中,其Class做为键
|
||||
*
|
||||
* @param key 键
|
||||
* @param obj 对象
|
||||
* @since 5.3.3
|
||||
*/
|
||||
public static void put(String key, Object obj) {
|
||||
POOL.put(key, obj);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,10 +97,19 @@ public final class Singleton {
|
||||
*/
|
||||
public static void remove(Class<?> clazz) {
|
||||
if (null != clazz) {
|
||||
POOL.remove(clazz.getName());
|
||||
remove(clazz.getName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除指定Singleton对象
|
||||
*
|
||||
* @param key 键
|
||||
*/
|
||||
public static void remove(String key) {
|
||||
POOL.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有Singleton对象
|
||||
*/
|
||||
|
@@ -838,6 +838,27 @@ public class ArrayUtil {
|
||||
return list.toArray(Arrays.copyOf(array, list.size()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑数组<br>
|
||||
* 编辑过程通过传入的Editor实现来返回需要的元素内容,这个Editor实现可以实现以下功能:
|
||||
*
|
||||
* <pre>
|
||||
* 1、修改元素对象,返回集合中为修改后的对象
|
||||
* </pre>
|
||||
*
|
||||
* 注意:此方法会修改原数组!
|
||||
*
|
||||
* @param <T> 数组元素类型
|
||||
* @param array 数组
|
||||
* @param editor 编辑器接口
|
||||
* @since 5.3.3
|
||||
*/
|
||||
public static <T> void edit(T[] array, Editor<T> editor) {
|
||||
for(int i = 0; i < array.length; i++){
|
||||
array[i] = editor.edit(array[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤<br>
|
||||
* 过滤过程通过传入的Filter实现来过滤返回需要的元素内容,这个Filter实现可以实现以下功能:
|
||||
@@ -2412,6 +2433,39 @@ public class ArrayUtil {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 以 conjunction 为分隔符将数组转换为字符串
|
||||
*
|
||||
* @param <T> 被处理的集合
|
||||
* @param array 数组
|
||||
* @param conjunction 分隔符
|
||||
* @param editor 每个元素的编辑器,null表示不编辑
|
||||
* @return 连接后的字符串
|
||||
* @since 5.3.3
|
||||
*/
|
||||
public static <T> String join(T[] array, CharSequence conjunction, Editor<T> editor) {
|
||||
if (null == array) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
boolean isFirst = true;
|
||||
for (T item : array) {
|
||||
if (isFirst) {
|
||||
isFirst = false;
|
||||
} else {
|
||||
sb.append(conjunction);
|
||||
}
|
||||
if(null != editor){
|
||||
item = editor.edit(item);
|
||||
}
|
||||
if(null != item){
|
||||
sb.append(StrUtil.toString(item));
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 以 conjunction 为分隔符将数组转换为字符串
|
||||
*
|
||||
|
Reference in New Issue
Block a user