mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
46d10b4dbc
commit
01f94defd2
@ -0,0 +1,38 @@
|
|||||||
|
package cn.hutool.core.lang.func;
|
||||||
|
|
||||||
|
import cn.hutool.core.reflect.ConstructorUtil;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 常用Lambda函数封装<br>
|
||||||
|
* 提供常用对象方法的Lambda包装,减少Lambda初始化时间。
|
||||||
|
*
|
||||||
|
* @author looly
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public class FunctionPool {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过{@code String(char[] value, boolean share)}这个内部构造生成一个Lambda函数<br>
|
||||||
|
* 此函数通过传入char[],实现zero-copy的String创建,效率很高。但是要求传入的char[]不可以在其他地方修改。
|
||||||
|
*/
|
||||||
|
public static final BiFunction<char[], Boolean, String> STRING_CREATOR;
|
||||||
|
|
||||||
|
static {
|
||||||
|
final Constructor<String> constructor = ConstructorUtil.getConstructor(String.class, char[].class, boolean.class);
|
||||||
|
STRING_CREATOR = LambdaFactory.build(BiFunction.class, constructor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过{@code String(char[] value, boolean share)}这个内部构造创建String对象。<br>
|
||||||
|
* 此函数通过传入char[],实现zero-copy的String创建,效率很高。但是要求传入的char[]不可以在其他地方修改。
|
||||||
|
*
|
||||||
|
* @param value char[]值,注意这个数组不可修改!!
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
public static String createString(final char[] value) {
|
||||||
|
return STRING_CREATOR.apply(value, true);
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,6 @@ package cn.hutool.core.lang.func;
|
|||||||
|
|
||||||
import cn.hutool.core.exceptions.UtilException;
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.lang.Opt;
|
|
||||||
import cn.hutool.core.lang.mutable.MutableEntry;
|
import cn.hutool.core.lang.mutable.MutableEntry;
|
||||||
import cn.hutool.core.map.WeakConcurrentMap;
|
import cn.hutool.core.map.WeakConcurrentMap;
|
||||||
import cn.hutool.core.reflect.LookupFactory;
|
import cn.hutool.core.reflect.LookupFactory;
|
||||||
|
@ -122,7 +122,8 @@ public class StrUtil extends CharSequenceUtil implements StrPool {
|
|||||||
* 将对象转为字符串
|
* 将对象转为字符串
|
||||||
* <pre>
|
* <pre>
|
||||||
* 1、Byte数组和ByteBuffer会被转换为对应字符串的数组
|
* 1、Byte数组和ByteBuffer会被转换为对应字符串的数组
|
||||||
* 2、对象数组会调用Arrays.toString方法
|
* 2、char[]会直接构造String
|
||||||
|
* 3、对象数组会调用Arrays.toString方法
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param obj 对象
|
* @param obj 对象
|
||||||
@ -136,6 +137,8 @@ public class StrUtil extends CharSequenceUtil implements StrPool {
|
|||||||
|
|
||||||
if (obj instanceof String) {
|
if (obj instanceof String) {
|
||||||
return (String) obj;
|
return (String) obj;
|
||||||
|
}else if(obj instanceof char[]){
|
||||||
|
return new String((char[]) obj);
|
||||||
}else if (obj instanceof byte[]) {
|
}else if (obj instanceof byte[]) {
|
||||||
return str((byte[]) obj, charset);
|
return str((byte[]) obj, charset);
|
||||||
} else if (obj instanceof Byte[]) {
|
} else if (obj instanceof Byte[]) {
|
||||||
@ -178,17 +181,6 @@ public class StrUtil extends CharSequenceUtil implements StrPool {
|
|||||||
return new String(data, charset);
|
return new String(data, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 将Byte数组转为字符串
|
|
||||||
*
|
|
||||||
* @param bytes byte数组
|
|
||||||
* @param charset 字符集
|
|
||||||
* @return 字符串
|
|
||||||
*/
|
|
||||||
public static String str(final Byte[] bytes, final String charset) {
|
|
||||||
return str(bytes, CharsetUtil.charset(charset));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解码字节码
|
* 解码字节码
|
||||||
*
|
*
|
||||||
@ -211,21 +203,6 @@ public class StrUtil extends CharSequenceUtil implements StrPool {
|
|||||||
return str(bytes, charset);
|
return str(bytes, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 将编码的byteBuffer数据转换为字符串
|
|
||||||
*
|
|
||||||
* @param data 数据
|
|
||||||
* @param charset 字符集,如果为空使用当前系统字符集
|
|
||||||
* @return 字符串
|
|
||||||
*/
|
|
||||||
public static String str(final ByteBuffer data, final String charset) {
|
|
||||||
if (data == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return str(data, CharsetUtil.charset(charset));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将编码的byteBuffer数据转换为字符串
|
* 将编码的byteBuffer数据转换为字符串
|
||||||
*
|
*
|
||||||
|
@ -0,0 +1,39 @@
|
|||||||
|
package cn.hutool.core.lang.func;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.ListUtil;
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.core.date.StopWatch;
|
||||||
|
import cn.hutool.core.util.RandomUtil;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class FunctionPoolTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createStringTest() {
|
||||||
|
// 预热
|
||||||
|
FunctionPool.createString("123".toCharArray());
|
||||||
|
|
||||||
|
// 测试数据
|
||||||
|
final ArrayList<char[]> list = ListUtil.of();
|
||||||
|
for (int i = 0; i < 100000; i++) {
|
||||||
|
list.add(RandomUtil.randomString(100).toCharArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
final StopWatch stopWatch = DateUtil.createStopWatch();
|
||||||
|
stopWatch.start("copy creator");
|
||||||
|
for (final char[] value : list) {
|
||||||
|
new String(value);
|
||||||
|
}
|
||||||
|
stopWatch.stop();
|
||||||
|
|
||||||
|
stopWatch.start("zero copy creator");
|
||||||
|
for (final char[] value : list) {
|
||||||
|
FunctionPool.createString(value);
|
||||||
|
}
|
||||||
|
stopWatch.stop();
|
||||||
|
|
||||||
|
//Console.log(stopWatch.prettyPrint());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user