com.penglecode.codeforce.common.support.ObjectLambdaBuilder Maven / Gradle / Ivy
The newest version!
package com.penglecode.codeforce.common.support;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* 基于JAVA8方法引用(lambda的一种特殊形式)方式来快速构造普通JavaBean的通用Builder
* 使用示例:
* Account account = ObjectLambdaBuilder.of(Account::new)
* .with(Account::setAcctNo, "27182821221122219")
* .with(Account::setCustNo, "71283828182823291929")
* .with(Account::setIdCode, null)
* .with(Account::setCustName, "阿三")
* .with(Account::setMobile, "13812345678")
* .build();
* System.out.println(account);
*
* @author pengpeng
* @version 1.0
*/
public class ObjectLambdaBuilder {
private final Supplier instantiator;
private final List> modifiers = new ArrayList<>();
private ObjectLambdaBuilder(Supplier instantiator) {
Assert.notNull(instantiator, "Parameter 'instantiator' can not be null!");
this.instantiator = instantiator;
}
public static ObjectLambdaBuilder of(Supplier instantiator) {
return new ObjectLambdaBuilder<>(instantiator);
}
public ObjectLambdaBuilder with(Consumer2 consumer2, P p) {
Consumer consumer = instance -> consumer2.accept(instance, p);
modifiers.add(consumer);
return this;
}
public T build() {
T instance = instantiator.get();
modifiers.forEach(modifier -> modifier.accept(instance));
modifiers.clear();
return instance;
}
/**
* 具有两个参数的Consumer
* @param
* @param
*/
public interface Consumer2 {
void accept(T t, P p);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy