add map support

This commit is contained in:
Looly 2022-02-16 13:21:05 +08:00
parent 9ecca7a3c8
commit bac27e87db
7 changed files with 49 additions and 41 deletions

View File

@ -6,6 +6,8 @@
### 🐣新特性
* 【poi 】 ExcelUtil.readBySax增加对POI-5.2.0的兼容性issue#I4TJF4@gitee
* 【extra 】 Ftp增加构造issue#I4TKXP@gitee
* 【core 】 GenericBuilder支持Map构建pr#540@Github
### 🐞Bug修复

View File

@ -1,13 +1,12 @@
package cn.hutool.core.builder;
import cn.hutool.core.lang.func.Consumer2;
import cn.hutool.core.lang.func.Consumer3;
import cn.hutool.core.lang.func.Supplier1;
import cn.hutool.core.lang.func.Supplier2;
import cn.hutool.core.lang.func.Supplier3;
import cn.hutool.core.lang.func.Supplier4;
import cn.hutool.core.lang.func.Supplier5;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
@ -214,8 +213,8 @@ public class GenericBuilder<T> implements Builder<T> {
* @param <P2> 参数二类型
* @return GenericBuilder对象
*/
public <P1, P2> GenericBuilder<T> with(Consumer2<T, P1, P2> consumer, P1 p1, P2 p2) {
modifiers.add(consumer.toConsumer(p1, p2));
public <P1, P2> GenericBuilder<T> with(Consumer3<T, P1, P2> consumer, P1 p1, P2 p2) {
modifiers.add(instant ->{consumer.accept(instant, p1, p2);});
return this;
}

View File

@ -1,37 +0,0 @@
package cn.hutool.core.lang.func;
import java.util.function.Consumer;
/**
* 2参数Consumer
*
* @param <T> 目标类型
* @param <P1> 参数一类型
* @param <P2> 参数二类型
* @author TomXin
* @since 5.7.22
*/
@FunctionalInterface
public interface Consumer2<T, P1, P2> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数一
* @param p2 参数二
*/
void accept(T t, P1 p1, P2 p2);
/**
* 将带有参数的Consumer转换为无参{@link Consumer}
*
* @param p1 参数1
* @param p2 参数2
* @return {@link Consumer}
*/
default Consumer<T> toConsumer(P1 p1, P2 p2) {
return instant -> accept(instant, p1, p2);
}
}

View File

@ -0,0 +1,23 @@
package cn.hutool.core.lang.func;
/**
* 3参数Consumer
*
* @param <P1> 参数一类型
* @param <P2> 参数二类型
* @param <P3> 参数三类型
* @author TomXin
* @since 5.7.22
*/
@FunctionalInterface
public interface Consumer3<P1, P2, P3> {
/**
* 接收参数方法
*
* @param p1 参数一
* @param p1 参数二
* @param p2 参数三
*/
void accept(P1 p1, P2 p2, P3 p3);
}

View File

@ -62,7 +62,10 @@ public class GenericBuilderTest {
Assert.assertEquals(333, box1.getWidth().intValue());
Assert.assertEquals(444, box1.getHeight().intValue());
Assert.assertEquals("TomXin:\"Hello Partner!\"", box1.getTitleAlias());
}
@Test
public void buildMapTest(){
//Map创建
HashMap<String, String> colorMap = GenericBuilder
.of(HashMap<String,String>::new)

View File

@ -459,4 +459,11 @@ public class ArrayUtilTest {
Assert.assertEquals(3, arrayAfterSplit.length);
Assert.assertEquals(24, arrayAfterSplit[2].length);
}
@Test
public void getTest(){
String[] a = {"a", "b", "c"};
final Object o = ArrayUtil.get(a, -1);
Assert.assertEquals("c", o);
}
}

View File

@ -134,6 +134,17 @@ public class Ftp extends AbstractFtp {
this.init();
}
/**
* 构造
*
* @param client 自定义实例化好的{@link FTPClient}
* @since 5.7.22
*/
public Ftp(FTPClient client) {
super(FtpConfig.create());
this.client = client;
}
/**
* 初始化连接
*