GenericBuilder新增2个参数的with方法,以支持Map构建

This commit is contained in:
TomXin 2022-02-14 10:29:07 +08:00
parent f8acf05f7c
commit 5239d4b697
3 changed files with 80 additions and 8 deletions

View File

@ -1,10 +1,6 @@
package cn.hutool.core.builder;
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 cn.hutool.core.lang.func.*;
import java.util.ArrayList;
import java.util.List;
@ -45,7 +41,16 @@ import java.util.function.Supplier;
* .with(Box::alis)
* .build();
* </pre>
* <p>注意本工具类支持调用的方法的参数数量不超过1个更多的参数不利于阅读和维护</p>
* <p> 还可能这样构建Map对象</p>
* <pre>
* HashMap<String, String> colorMap = GenericBuilder
* .of(HashMap<String,String>::new)
* .with(Map::put, "red", "#FF0000")
* .with(Map::put, "yellow", "#FFFF00")
* .with(Map::put, "blue", "#0000FF")
* .build();
* </pre>
* <p>注意本工具类支持调用的构造方法的参数数量不超过5个一般方法的参数数量不超过2个更多的参数不利于阅读和维护</p>
*
* @author TomXin
* @since 5.7.21
@ -183,9 +188,9 @@ public class GenericBuilder<T> implements Builder<T> {
/**
* 调用1参数方法
*
* @param <P1> 参数一类型
* @param consumer 1参数Consumer一般为Setter方法引用
* @param consumer 1参数Consumer
* @param p1 参数一
* @param <P1> 参数一类型
* @return GenericBuilder对象
*/
public <P1> GenericBuilder<T> with(BiConsumer<T, P1> consumer, P1 p1) {
@ -193,6 +198,21 @@ public class GenericBuilder<T> implements Builder<T> {
return this;
}
/**
* 调用2参数方法
*
* @param consumer 2参数Consumer
* @param p1 参数一
* @param p2 参数二
* @param <P1> 参数一类型
* @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));
return this;
}
/**
* 构建
*

View File

@ -0,0 +1,37 @@
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

@ -8,6 +8,9 @@ import lombok.experimental.Accessors;
import org.junit.Assert;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
/**
* {@link GenericBuilder} 单元测试类
*
@ -58,6 +61,18 @@ public class GenericBuilderTest {
Assert.assertEquals(222, box1.getLength().intValue());
Assert.assertEquals(333, box1.getWidth().intValue());
Assert.assertEquals(444, box1.getHeight().intValue());
Assert.assertEquals("TomXin:\"Hello Partner!\"", box1.getTitleAlias());
//Map创建
HashMap<String, String> colorMap = GenericBuilder
.of(HashMap<String,String>::new)
.with(Map::put, "red", "#FF0000")
.with(Map::put, "yellow", "#FFFF00")
.with(Map::put, "blue", "#0000FF")
.build();
Assert.assertEquals("#FF0000", colorMap.get("red"));
Assert.assertEquals("#FFFF00", colorMap.get("yellow"));
Assert.assertEquals("#0000FF", colorMap.get("blue"));
}
@Getter