mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-08 06:37:58 +08:00
!540 GenericBuilder新增2个参数的with方法,以支持Map构建
Merge pull request !540 from meiMingle/v5-dev
This commit is contained in:
commit
9ecca7a3c8
@ -1,11 +1,13 @@
|
|||||||
package cn.hutool.core.builder;
|
package cn.hutool.core.builder;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.func.Consumer2;
|
||||||
import cn.hutool.core.lang.func.Supplier1;
|
import cn.hutool.core.lang.func.Supplier1;
|
||||||
import cn.hutool.core.lang.func.Supplier2;
|
import cn.hutool.core.lang.func.Supplier2;
|
||||||
import cn.hutool.core.lang.func.Supplier3;
|
import cn.hutool.core.lang.func.Supplier3;
|
||||||
import cn.hutool.core.lang.func.Supplier4;
|
import cn.hutool.core.lang.func.Supplier4;
|
||||||
import cn.hutool.core.lang.func.Supplier5;
|
import cn.hutool.core.lang.func.Supplier5;
|
||||||
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
@ -45,7 +47,16 @@ import java.util.function.Supplier;
|
|||||||
* .with(Box::alis)
|
* .with(Box::alis)
|
||||||
* .build();
|
* .build();
|
||||||
* </pre>
|
* </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
|
* @author TomXin
|
||||||
* @since 5.7.21
|
* @since 5.7.21
|
||||||
@ -183,9 +194,9 @@ public class GenericBuilder<T> implements Builder<T> {
|
|||||||
/**
|
/**
|
||||||
* 调用1参数方法
|
* 调用1参数方法
|
||||||
*
|
*
|
||||||
* @param <P1> 参数一类型
|
* @param consumer 1参数Consumer
|
||||||
* @param consumer 1参数Consumer,一般为Setter方法引用
|
|
||||||
* @param p1 参数一
|
* @param p1 参数一
|
||||||
|
* @param <P1> 参数一类型
|
||||||
* @return GenericBuilder对象
|
* @return GenericBuilder对象
|
||||||
*/
|
*/
|
||||||
public <P1> GenericBuilder<T> with(BiConsumer<T, P1> consumer, P1 p1) {
|
public <P1> GenericBuilder<T> with(BiConsumer<T, P1> consumer, P1 p1) {
|
||||||
@ -193,6 +204,21 @@ public class GenericBuilder<T> implements Builder<T> {
|
|||||||
return this;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建
|
* 构建
|
||||||
*
|
*
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -8,6 +8,9 @@ import lombok.experimental.Accessors;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link GenericBuilder} 单元测试类
|
* {@link GenericBuilder} 单元测试类
|
||||||
*
|
*
|
||||||
@ -58,6 +61,18 @@ public class GenericBuilderTest {
|
|||||||
Assert.assertEquals(222, box1.getLength().intValue());
|
Assert.assertEquals(222, box1.getLength().intValue());
|
||||||
Assert.assertEquals(333, box1.getWidth().intValue());
|
Assert.assertEquals(333, box1.getWidth().intValue());
|
||||||
Assert.assertEquals(444, box1.getHeight().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
|
@Getter
|
||||||
|
Loading…
Reference in New Issue
Block a user