mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
重构Jdk自带的Lambda体系,支持序列化+包裹受检异常
改动如下: 1. AnnotationUtil 115行 简化 predicate::test 为 predicate 2. 调整 Func1 为 SerFunction 3. 调整 Func0 为 SerSupplier 4. 移除 GenericBuilder 对于多参数构造双冒号简写支持,直接采用lambda方式,例如GenericBuilder.of(Box::new, 2048L, "Hello Partner!", 222, 333, 444)改为GenericBuilder.of(() -> new Box(2048L, "Hello Partner!", 222, 333, 444)) 5. 移除 CheckedUtil,现有重构后的Lambda 支持包裹异常 6. 移除 Func,该函数式接口属于泛型可变参数,不推荐使用 7. 移除 Supplier1,1参数Supplier应该使用SerFunction替代 8. 移除 Supplier2,2参数Supplier应该使用SerBiFunction替代 9. 移除 Supplier3,3参数Supplier应该使用SerFunction3替代(因第4条更改思路,该SerFunction3并未添加) 10. 移除 Supplier4,4参数Supplier应该使用SerFunction4替代(因第4条更改思路,该SerFunction4并未添加) 11. 移除 Supplier5,5参数Supplier应该使用SerFunction5替代(因第4条更改思路,该SerFunction5并未添加) 12. 移除 VoidFunc,该函数式接口属于泛型可变参数,不推荐使用 13. 调整 VoidFunc0 为 SerRunnable 14. 调整 VoidFunc1 为 SerConsumer 15. 调整 EntryStream 泛型命名、完善javadoc 16. EnumUtil 273行 简化 field::callWithRuntimeException 为 field
This commit is contained in:
@@ -52,7 +52,7 @@ public class GenericBuilderTest {
|
||||
|
||||
// 多参数构造
|
||||
final Box box1 = GenericBuilder
|
||||
.of(Box::new, 2048L, "Hello Partner!", 222, 333, 444)
|
||||
.of(() -> new Box(2048L, "Hello Partner!", 222, 333, 444))
|
||||
.with(Box::alis)
|
||||
.build();
|
||||
|
||||
@@ -65,10 +65,10 @@ public class GenericBuilderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildMapTest(){
|
||||
public void buildMapTest() {
|
||||
//Map创建
|
||||
final HashMap<String, String> colorMap = GenericBuilder
|
||||
.of(HashMap<String,String>::new)
|
||||
.of(HashMap<String, String>::new)
|
||||
.with(Map::put, "red", "#FF0000")
|
||||
.with(Map::put, "yellow", "#FFFF00")
|
||||
.with(Map::put, "blue", "#0000FF")
|
||||
|
@@ -1,62 +0,0 @@
|
||||
package cn.hutool.core.exceptions;
|
||||
|
||||
import cn.hutool.core.lang.func.Func1;
|
||||
import cn.hutool.core.lang.func.VoidFunc0;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
||||
/**
|
||||
* 方便的执行会抛出受检查类型异常的方法调用或者代码段
|
||||
* <p>
|
||||
* 该工具通过函数式的方式将那些需要抛出受检查异常的表达式或者代码段转化成一个标准的java8 functional 对象
|
||||
* </p>
|
||||
*
|
||||
* @author conder
|
||||
*/
|
||||
public class CheckedUtilTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void sleepTest() {
|
||||
final VoidFunc0 func = () -> Thread.sleep(1000L);
|
||||
func.callWithRuntimeException();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void supplierTest() {
|
||||
final File noFile = new File("./no-file");
|
||||
try {
|
||||
//本行代码原本需要抛出受检查异常,现在只抛出运行时异常
|
||||
CheckedUtil.uncheck(() -> new FileInputStream(noFile)).call();
|
||||
} catch (final Exception re) {
|
||||
Assert.assertTrue(re instanceof RuntimeException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void functionTest() {
|
||||
final Func1<String, String> afunc = (funcParam) -> {
|
||||
if (funcParam.length() > 5) {
|
||||
throw new Exception("这是受检查异常需要屌用处显示处理");
|
||||
}
|
||||
return funcParam.toUpperCase();
|
||||
};
|
||||
|
||||
//afunc.apply("hello world"); 直接调用需要处理异常
|
||||
|
||||
|
||||
try {
|
||||
//本行代码原本需要抛出受检查异常,现在只抛出运行时异常
|
||||
CheckedUtil.uncheck(afunc).call("hello world");
|
||||
} catch (final Exception re) {
|
||||
Assert.assertTrue(re instanceof RuntimeException);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -14,14 +14,14 @@ public class LambdaUtilTest {
|
||||
|
||||
@Test
|
||||
public void getMethodNameTest() {
|
||||
final Func1<MyTeacher, String> lambda = MyTeacher::getAge;
|
||||
final SerFunction<MyTeacher, String> lambda = MyTeacher::getAge;
|
||||
final String methodName = LambdaUtil.getMethodName(lambda);
|
||||
Assert.assertEquals("getAge", methodName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFieldNameTest() {
|
||||
final Func1<MyTeacher, String> lambda = MyTeacher::getAge;
|
||||
final SerFunction<MyTeacher, String> lambda = MyTeacher::getAge;
|
||||
final String fieldName = LambdaUtil.getFieldName(lambda);
|
||||
Assert.assertEquals("age", fieldName);
|
||||
}
|
||||
@@ -30,31 +30,31 @@ public class LambdaUtilTest {
|
||||
public <T> void resolveTest() {
|
||||
Stream.<Runnable>of(() -> {
|
||||
// 引用构造函数
|
||||
final Func0<MyTeacher> lambda = MyTeacher::new;
|
||||
final SerSupplier<MyTeacher> lambda = MyTeacher::new;
|
||||
final LambdaInfo lambdaInfo = LambdaUtil.resolve(lambda);
|
||||
Assert.assertEquals(0, lambdaInfo.getParameterTypes().length);
|
||||
Assert.assertEquals(MyTeacher.class, lambdaInfo.getReturnType());
|
||||
}, () -> {
|
||||
// 数组构造函数引用(此处数组构造参数)
|
||||
final Func1<Integer, MyTeacher[]> lambda = MyTeacher[]::new;
|
||||
final SerFunction<Integer, MyTeacher[]> lambda = MyTeacher[]::new;
|
||||
final LambdaInfo lambdaInfo = LambdaUtil.resolve(lambda);
|
||||
Assert.assertEquals(int.class, lambdaInfo.getParameterTypes()[0]);
|
||||
Assert.assertEquals(MyTeacher[].class, lambdaInfo.getReturnType());
|
||||
}, () -> {
|
||||
// 引用静态方法
|
||||
final Func0<String> lambda = MyTeacher::takeAge;
|
||||
final SerSupplier<String> lambda = MyTeacher::takeAge;
|
||||
final LambdaInfo lambdaInfo = LambdaUtil.resolve(lambda);
|
||||
Assert.assertEquals(0, lambdaInfo.getParameterTypes().length);
|
||||
Assert.assertEquals(String.class, lambdaInfo.getReturnType());
|
||||
}, () -> {
|
||||
// 引用特定对象的实例方法
|
||||
final Func0<String> lambda = new MyTeacher()::getAge;
|
||||
final SerSupplier<String> lambda = new MyTeacher()::getAge;
|
||||
final LambdaInfo lambdaInfo = LambdaUtil.resolve(lambda);
|
||||
Assert.assertEquals(0, lambdaInfo.getParameterTypes().length);
|
||||
Assert.assertEquals(String.class, lambdaInfo.getReturnType());
|
||||
}, () -> {
|
||||
// 引用特定类型的任意对象的实例方法
|
||||
final Func1<MyTeacher, String> lambda = MyTeacher::getAge;
|
||||
final SerFunction<MyTeacher, String> lambda = MyTeacher::getAge;
|
||||
final LambdaInfo lambdaInfo = LambdaUtil.resolve(lambda);
|
||||
Assert.assertEquals(0, lambdaInfo.getParameterTypes().length);
|
||||
Assert.assertEquals(String.class, lambdaInfo.getReturnType());
|
||||
@@ -80,15 +80,15 @@ public class LambdaUtilTest {
|
||||
Assert.assertEquals(void.class, lambdaInfo.getReturnType());
|
||||
}, () -> {
|
||||
// 一些特殊的lambda
|
||||
Assert.assertEquals("T[]", LambdaUtil.<Func<Object, Stream<?>>>resolve(Stream::of).getParameterTypes()[0].getTypeName());
|
||||
Assert.assertEquals(MyTeacher[][].class, LambdaUtil.<Func1<Integer, MyTeacher[][]>>resolve(MyTeacher[][]::new).getReturnType());
|
||||
Assert.assertEquals(Integer[][][].class, LambdaUtil.<VoidFunc1<Integer[][][]>>resolve(a -> {}).getParameterTypes()[0]);
|
||||
Assert.assertEquals(Integer[][][].class, LambdaUtil.resolve((Serializable & Consumer3<Integer[][][], Integer[][], Integer>) (a, b, c) -> {}).getParameterTypes()[0]);
|
||||
Assert.assertEquals("T", LambdaUtil.<SerFunction<Object, Stream<?>>>resolve(Stream::of).getParameterTypes()[0].getTypeName());
|
||||
Assert.assertEquals(MyTeacher[][].class, LambdaUtil.<SerFunction<Integer, MyTeacher[][]>>resolve(MyTeacher[][]::new).getReturnType());
|
||||
Assert.assertEquals(Integer[][][].class, LambdaUtil.<SerConsumer<Integer[][][]>>resolve(a -> {}).getParameterTypes()[0]);
|
||||
Assert.assertEquals(Integer[][][].class, LambdaUtil.resolve((Serializable & SerConsumer3<Integer[][][], Integer[][], Integer>) (a, b, c) -> {}).getParameterTypes()[0]);
|
||||
}).forEach(Runnable::run);
|
||||
|
||||
}
|
||||
|
||||
interface SerThiCons<P1, P2, P3> extends Consumer3<P1, P2, P3>, Serializable {
|
||||
interface SerThiCons<P1, P2, P3> extends SerConsumer3<P1, P2, P3>, Serializable {
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -96,48 +96,48 @@ public class LambdaUtilTest {
|
||||
final MyTeacher myTeacher = new MyTeacher();
|
||||
Stream.<Runnable>of(() -> {
|
||||
// 引用特定类型的任意对象的实例方法
|
||||
final Func1<MyTeacher, String> lambda = MyTeacher::getAge;
|
||||
final SerFunction<MyTeacher, String> lambda = MyTeacher::getAge;
|
||||
Assert.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda));
|
||||
}, () -> {
|
||||
// 枚举测试,不会导致类型擦除
|
||||
final Func1<LambdaKindEnum, Integer> lambda = LambdaKindEnum::ordinal;
|
||||
final SerFunction<LambdaKindEnum, Integer> lambda = LambdaKindEnum::ordinal;
|
||||
Assert.assertEquals(LambdaKindEnum.class, LambdaUtil.getRealClass(lambda));
|
||||
}, () -> {
|
||||
// 调用父类方法,能获取到正确的子类类型
|
||||
final Func1<MyTeacher, ?> lambda = MyTeacher::getId;
|
||||
final SerFunction<MyTeacher, ?> lambda = MyTeacher::getId;
|
||||
Assert.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda));
|
||||
}, () -> {
|
||||
// 引用特定对象的实例方法
|
||||
final Func0<String> lambda = myTeacher::getAge;
|
||||
final SerSupplier<String> lambda = myTeacher::getAge;
|
||||
Assert.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda));
|
||||
}, () -> {
|
||||
// 枚举测试,只能获取到枚举类型
|
||||
final Func0<Integer> lambda = LambdaKindEnum.REF_NONE::ordinal;
|
||||
final SerSupplier<Integer> lambda = LambdaKindEnum.REF_NONE::ordinal;
|
||||
Assert.assertEquals(Enum.class, LambdaUtil.getRealClass(lambda));
|
||||
}, () -> {
|
||||
// 调用父类方法,只能获取到父类类型
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
final VoidFunc0 lambda = myTeacher::getId;
|
||||
final SerSupplier<?> lambda = myTeacher::getId;
|
||||
Assert.assertEquals(Entity.class, LambdaUtil.getRealClass(lambda));
|
||||
}, () -> {
|
||||
// 引用静态带参方法,能够获取到正确的参数类型
|
||||
final Func1<MyTeacher, String> lambda = MyTeacher::takeAgeBy;
|
||||
final SerFunction<MyTeacher, String> lambda = MyTeacher::takeAgeBy;
|
||||
Assert.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda));
|
||||
}, () -> {
|
||||
// 引用父类静态带参方法,只能获取到父类类型
|
||||
final Func0<?> lambda = MyTeacher::takeId;
|
||||
final SerSupplier<?> lambda = MyTeacher::takeId;
|
||||
Assert.assertEquals(Entity.class, LambdaUtil.getRealClass(lambda));
|
||||
}, () -> {
|
||||
// 引用静态无参方法,能够获取到正确的类型
|
||||
final Func0<String> lambda = MyTeacher::takeAge;
|
||||
final SerSupplier<String> lambda = MyTeacher::takeAge;
|
||||
Assert.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda));
|
||||
}, () -> {
|
||||
// 引用父类静态无参方法,能够获取到正确的参数类型
|
||||
final Func1<MyTeacher, ?> lambda = MyTeacher::takeIdBy;
|
||||
final SerFunction<MyTeacher, ?> lambda = MyTeacher::takeIdBy;
|
||||
Assert.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda));
|
||||
}, () -> {
|
||||
// 数组测试
|
||||
final VoidFunc1<String[]> lambda = (String[] stringList) -> {};
|
||||
final SerConsumer<String[]> lambda = (String[] stringList) -> {};
|
||||
Assert.assertEquals(String[].class, LambdaUtil.getRealClass(lambda));
|
||||
}).forEach(Runnable::run);
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@ import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class FuncMapTest {
|
||||
public class SerFunctionMapTest {
|
||||
|
||||
@Test
|
||||
public void putGetTest(){
|
Reference in New Issue
Block a user