添加ifAbsent

This commit is contained in:
yulichang 2023-12-24 23:37:14 +08:00
parent a2756a9dde
commit a2bdce7614
4 changed files with 22 additions and 22 deletions

View File

@ -73,7 +73,8 @@ public class MybatisPlusJoinAutoConfiguration {
ConfigProperties.logicDelType = this.properties.getLogicDelType();
ConfigProperties.mappingMaxCount = this.properties.getMappingMaxCount();
ConfigProperties.ifAbsent = Optional.ofNullable(ifAbsentConsumers.getIfAvailable())
.map(m -> (BiPredicate<Object, IfAbsentSqlKeyWordEnum>) m).orElse(this.properties.getIfAbsent());
.map(m -> (BiPredicate<Object, IfAbsentSqlKeyWordEnum>) m)
.orElse((val, key) -> this.properties.getIfAbsent().test(val));
info("mybatis plus join properties config complete");
}

View File

@ -59,5 +59,5 @@ public class ConfigProperties {
* <p>
* NOT_BLANK 非空白字符串 "" -> false, " " -> false, "\r" -> false, "abc" -> true ...
*/
public static BiPredicate<Object, IfAbsentSqlKeyWordEnum> ifAbsent = IfAbsentEnum.NOT_EMPTY;
public static BiPredicate<Object, IfAbsentSqlKeyWordEnum> ifAbsent = (val, key) -> IfAbsentEnum.NOT_EMPTY.test(val);
}

View File

@ -1,10 +1,9 @@
package com.github.yulichang.config.enums;
import com.github.yulichang.toolkit.MPJStringUtils;
import com.github.yulichang.wrapper.enums.IfAbsentSqlKeyWordEnum;
import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
/**
* 条件判断策略
@ -12,29 +11,29 @@ import java.util.function.BiPredicate;
* @author yulichang
* @since 1.4.9
*/
public enum IfAbsentEnum implements BiPredicate<Object, IfAbsentSqlKeyWordEnum> {
public enum IfAbsentEnum implements Predicate<Object> {
/**
* 非null
*/
NOT_NULL((val, key) -> Objects.nonNull(val)),
NOT_NULL(Objects::nonNull),
/**
* 非空字符串 "" -> false, " " -> true ...
*/
NOT_EMPTY((val, key) -> NOT_NULL.test(val, key) && (!(val instanceof CharSequence) || MPJStringUtils.isNotEmpty((CharSequence) val))),
NOT_EMPTY(val -> NOT_NULL.and(v -> !(v instanceof CharSequence) || MPJStringUtils.isNotEmpty((CharSequence) v)).test(val)),
/**
* NOT_BLANK 非空白字符串 "" -> false, " " -> false, "\r" -> false, "abc" -> true ...
*/
NOT_BLANK((val, key) -> NOT_NULL.test(val, key) && (!(val instanceof CharSequence) || MPJStringUtils.isNotBlank((CharSequence) val)));
NOT_BLANK(val -> NOT_NULL.and(v -> !(v instanceof CharSequence) || MPJStringUtils.isNotBlank((CharSequence) v)).test(val));
private final BiPredicate<Object, IfAbsentSqlKeyWordEnum> predicate;
private final Predicate<Object> predicate;
IfAbsentEnum(BiPredicate<Object, IfAbsentSqlKeyWordEnum> predicate) {
IfAbsentEnum(Predicate<Object> predicate) {
this.predicate = predicate;
}
@Override
public boolean test(Object obj, IfAbsentSqlKeyWordEnum keyword) {
return this.predicate.test(obj, keyword);
public boolean test(Object obj) {
return this.predicate.test(obj);
}
}

View File

@ -22,17 +22,17 @@ public class IfAbsentTest {
@Test
void ifAbsent() {
assert IfAbsentEnum.NOT_EMPTY.test("\t", null);
assert !IfAbsentEnum.NOT_EMPTY.test("", null);
assert IfAbsentEnum.NOT_EMPTY.test(" ", null);
assert IfAbsentEnum.NOT_EMPTY.test("\r", null);
assert IfAbsentEnum.NOT_EMPTY.test("a", null);
assert IfAbsentEnum.NOT_EMPTY.test("\t");
assert !IfAbsentEnum.NOT_EMPTY.test("");
assert IfAbsentEnum.NOT_EMPTY.test(" ");
assert IfAbsentEnum.NOT_EMPTY.test("\r");
assert IfAbsentEnum.NOT_EMPTY.test("a");
assert !IfAbsentEnum.NOT_BLANK.test("\t", null);
assert !IfAbsentEnum.NOT_BLANK.test("", null);
assert !IfAbsentEnum.NOT_BLANK.test(" ", null);
assert !IfAbsentEnum.NOT_BLANK.test("\r", null);
assert IfAbsentEnum.NOT_BLANK.test("a", null);
assert !IfAbsentEnum.NOT_BLANK.test("\t");
assert !IfAbsentEnum.NOT_BLANK.test("");
assert !IfAbsentEnum.NOT_BLANK.test(" ");
assert !IfAbsentEnum.NOT_BLANK.test("\r");
assert IfAbsentEnum.NOT_BLANK.test("a");
ThreadLocalUtils.set("SELECT t.id, t.pid, t.`name`, t.`json`, t.sex, t.head_img, t.create_time, t.address_id, " +
"t.address_id2, t.del, t.create_by, t.update_by FROM `user` t " +