fix(bug): change ObjectUtil.defaultIfXXX param type Supplier to Function

This commit is contained in:
youtiaoguagua 2022-09-11 00:16:46 +08:00
parent 4696576598
commit df98df70bb
12 changed files with 140 additions and 45 deletions

View File

@ -164,7 +164,7 @@ public abstract class AbstractTypeAnnotationScanner<T extends AbstractTypeAnnota
*/ */
@Override @Override
public void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) { public void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) {
filter = ObjectUtil.defaultIfNull(filter, annotation -> true); filter = ObjectUtil.defaultIfNull(filter, a -> annotation -> true);
final Class<?> sourceClass = getClassFormAnnotatedElement(annotatedEle); final Class<?> sourceClass = getClassFormAnnotatedElement(annotatedEle);
final Deque<List<Class<?>>> classDeque = CollUtil.newLinkedList(CollUtil.newArrayList(sourceClass)); final Deque<List<Class<?>>> classDeque = CollUtil.newLinkedList(CollUtil.newArrayList(sourceClass));
final Set<Class<?>> accessedTypes = new LinkedHashSet<>(); final Set<Class<?>> accessedTypes = new LinkedHashSet<>();

View File

@ -174,7 +174,7 @@ public interface AnnotationScanner {
* @param filter 注解过滤器无法通过过滤器的注解不会被处理该参数允许为空 * @param filter 注解过滤器无法通过过滤器的注解不会被处理该参数允许为空
*/ */
default void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) { default void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) {
filter = ObjectUtil.defaultIfNull(filter, annotation -> true); filter = ObjectUtil.defaultIfNull(filter, (a)->annotation -> true);
for (final Annotation annotation : annotatedEle.getAnnotations()) { for (final Annotation annotation : annotatedEle.getAnnotations()) {
if (AnnotationUtil.isNotJdkMateAnnotation(annotation.annotationType()) && filter.test(annotation)) { if (AnnotationUtil.isNotJdkMateAnnotation(annotation.annotationType()) && filter.test(annotation)) {
consumer.accept(0, annotation); consumer.accept(0, annotation);

View File

@ -35,7 +35,7 @@ public class ElementAnnotationScanner implements AnnotationScanner {
*/ */
@Override @Override
public void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) { public void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) {
filter = ObjectUtil.defaultIfNull(filter, t -> true); filter = ObjectUtil.defaultIfNull(filter,a-> t -> true);
Stream.of(annotatedEle.getAnnotations()) Stream.of(annotatedEle.getAnnotations())
.filter(filter) .filter(filter)
.forEach(annotation -> consumer.accept(0, annotation)); .forEach(annotation -> consumer.accept(0, annotation));

View File

@ -36,7 +36,7 @@ public class FieldAnnotationScanner implements AnnotationScanner {
*/ */
@Override @Override
public void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) { public void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) {
filter = ObjectUtil.defaultIfNull(filter, annotation -> true); filter = ObjectUtil.defaultIfNull(filter, a -> annotation -> true);
for (final Annotation annotation : annotatedEle.getAnnotations()) { for (final Annotation annotation : annotatedEle.getAnnotations()) {
if (AnnotationUtil.isNotJdkMateAnnotation(annotation.annotationType()) && filter.test(annotation)) { if (AnnotationUtil.isNotJdkMateAnnotation(annotation.annotationType()) && filter.test(annotation)) {
consumer.accept(0, annotation); consumer.accept(0, annotation);

View File

@ -75,16 +75,16 @@ public class GenericAnnotationScanner implements AnnotationScanner {
* @param enableScanSupperInterface 是否扫描父接口 * @param enableScanSupperInterface 是否扫描父接口
*/ */
public GenericAnnotationScanner( public GenericAnnotationScanner(
boolean enableScanMetaAnnotation, boolean enableScanMetaAnnotation,
boolean enableScanSupperClass, boolean enableScanSupperClass,
boolean enableScanSupperInterface) { boolean enableScanSupperInterface) {
this.metaScanner = enableScanMetaAnnotation ? new MetaAnnotationScanner() : new EmptyAnnotationScanner(); this.metaScanner = enableScanMetaAnnotation ? new MetaAnnotationScanner() : new EmptyAnnotationScanner();
this.typeScanner = new TypeAnnotationScanner( this.typeScanner = new TypeAnnotationScanner(
enableScanSupperClass, enableScanSupperInterface, a -> true, Collections.emptySet() enableScanSupperClass, enableScanSupperInterface, a -> true, Collections.emptySet()
); );
this.methodScanner = new MethodAnnotationScanner( this.methodScanner = new MethodAnnotationScanner(
enableScanSupperClass, enableScanSupperInterface, a -> true, Collections.emptySet() enableScanSupperClass, enableScanSupperInterface, a -> true, Collections.emptySet()
); );
this.elementScanner = new ElementAnnotationScanner(); this.elementScanner = new ElementAnnotationScanner();
} }
@ -98,7 +98,7 @@ public class GenericAnnotationScanner implements AnnotationScanner {
*/ */
@Override @Override
public void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) { public void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) {
filter = ObjectUtil.defaultIfNull(filter, t -> true); filter = ObjectUtil.defaultIfNull(filter, a -> t -> true);
if (ObjectUtil.isNull(annotatedEle)) { if (ObjectUtil.isNull(annotatedEle)) {
return; return;
} }
@ -125,10 +125,10 @@ public class GenericAnnotationScanner implements AnnotationScanner {
* @param filter 注解过滤器无法通过过滤器的注解不会被处理该参数允许为空 * @param filter 注解过滤器无法通过过滤器的注解不会被处理该参数允许为空
*/ */
private void scanElements( private void scanElements(
AnnotationScanner scanner, AnnotationScanner scanner,
BiConsumer<Integer, Annotation> consumer, BiConsumer<Integer, Annotation> consumer,
AnnotatedElement annotatedEle, AnnotatedElement annotatedEle,
Predicate<Annotation> filter) { Predicate<Annotation> filter) {
// 扫描类上注解 // 扫描类上注解
final ListValueMap<Integer, Annotation> classAnnotations = new ListValueMap<>(new LinkedHashMap<>()); final ListValueMap<Integer, Annotation> classAnnotations = new ListValueMap<>(new LinkedHashMap<>());
scanner.scan((index, annotation) -> { scanner.scan((index, annotation) -> {
@ -139,10 +139,10 @@ public class GenericAnnotationScanner implements AnnotationScanner {
// 扫描元注解 // 扫描元注解
classAnnotations.forEach((index, annotations) -> classAnnotations.forEach((index, annotations) ->
annotations.forEach(annotation -> { annotations.forEach(annotation -> {
consumer.accept(index, annotation); consumer.accept(index, annotation);
metaScanner.scan(consumer, annotation.annotationType(), filter); metaScanner.scan(consumer, annotation.annotationType(), filter);
}) })
); );
} }

View File

@ -51,7 +51,7 @@ public class MetaAnnotationScanner implements AnnotationScanner {
*/ */
@Override @Override
public boolean support(AnnotatedElement annotatedEle) { public boolean support(AnnotatedElement annotatedEle) {
return (annotatedEle instanceof Class && ClassUtil.isAssignable(Annotation.class, (Class<?>)annotatedEle)); return (annotatedEle instanceof Class && ClassUtil.isAssignable(Annotation.class, (Class<?>) annotatedEle));
} }
/** /**
@ -80,25 +80,25 @@ public class MetaAnnotationScanner implements AnnotationScanner {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) { public void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) {
filter = ObjectUtil.defaultIfNull(filter, t -> true); filter = ObjectUtil.defaultIfNull(filter, a -> t -> true);
Set<Class<? extends Annotation>> accessed = new HashSet<>(); Set<Class<? extends Annotation>> accessed = new HashSet<>();
final Deque<List<Class<? extends Annotation>>> deque = CollUtil.newLinkedList(CollUtil.newArrayList((Class<? extends Annotation>)annotatedEle)); final Deque<List<Class<? extends Annotation>>> deque = CollUtil.newLinkedList(CollUtil.newArrayList((Class<? extends Annotation>) annotatedEle));
int distance = 0; int distance = 0;
do { do {
final List<Class<? extends Annotation>> annotationTypes = deque.removeFirst(); final List<Class<? extends Annotation>> annotationTypes = deque.removeFirst();
for (final Class<? extends Annotation> type : annotationTypes) { for (final Class<? extends Annotation> type : annotationTypes) {
final List<Annotation> metaAnnotations = Stream.of(type.getAnnotations()) final List<Annotation> metaAnnotations = Stream.of(type.getAnnotations())
.filter(a -> !AnnotationUtil.isJdkMetaAnnotation(a.annotationType())) .filter(a -> !AnnotationUtil.isJdkMetaAnnotation(a.annotationType()))
.filter(filter) .filter(filter)
.collect(Collectors.toList()); .collect(Collectors.toList());
for (final Annotation metaAnnotation : metaAnnotations) { for (final Annotation metaAnnotation : metaAnnotations) {
consumer.accept(distance, metaAnnotation); consumer.accept(distance, metaAnnotation);
} }
accessed.add(type); accessed.add(type);
List<Class<? extends Annotation>> next = metaAnnotations.stream() List<Class<? extends Annotation>> next = metaAnnotations.stream()
.map(Annotation::annotationType) .map(Annotation::annotationType)
.filter(t -> !accessed.contains(t)) .filter(t -> !accessed.contains(t))
.collect(Collectors.toList()); .collect(Collectors.toList());
if (CollUtil.isNotEmpty(next)) { if (CollUtil.isNotEmpty(next)) {
deque.addLast(next); deque.addLast(next);
} }

View File

@ -160,7 +160,7 @@ public class DateTime extends Date {
* @since 4.1.2 * @since 4.1.2
*/ */
public DateTime(Date date, TimeZone timeZone) { public DateTime(Date date, TimeZone timeZone) {
this(ObjectUtil.defaultIfNull(date, Date::new).getTime(), timeZone); this(ObjectUtil.defaultIfNull(date, new Date()).getTime(), timeZone);
} }
/** /**

View File

@ -117,7 +117,7 @@ public class FastByteArrayOutputStream extends OutputStream {
*/ */
public String toString(Charset charset) { public String toString(Charset charset) {
return new String(toByteArray(), return new String(toByteArray(),
ObjectUtil.defaultIfNull(charset, CharsetUtil::defaultCharset)); ObjectUtil.defaultIfNull(charset, CharsetUtil.defaultCharset()));
} }
} }

View File

@ -31,7 +31,7 @@ public class ResourceClassLoader<T extends Resource> extends SecureClassLoader {
*/ */
public ResourceClassLoader(ClassLoader parentClassLoader, Map<String, T> resourceMap) { public ResourceClassLoader(ClassLoader parentClassLoader, Map<String, T> resourceMap) {
super(ObjectUtil.defaultIfNull(parentClassLoader, ClassLoaderUtil::getClassLoader)); super(ObjectUtil.defaultIfNull(parentClassLoader, ClassLoaderUtil::getClassLoader));
this.resourceMap = ObjectUtil.defaultIfNull(resourceMap, HashMap::new); this.resourceMap = ObjectUtil.defaultIfNull(resourceMap, new HashMap<>());
this.cacheClassMap = new HashMap<>(); this.cacheClassMap = new HashMap<>();
} }

View File

@ -442,7 +442,7 @@ public class LinkedForestMap<K, V> implements ForestMap<K, V> {
*/ */
TreeEntryNode<K, V> traverseParentNodes( TreeEntryNode<K, V> traverseParentNodes(
boolean includeCurrent, Consumer<TreeEntryNode<K, V>> consumer, Predicate<TreeEntryNode<K, V>> breakTraverse) { boolean includeCurrent, Consumer<TreeEntryNode<K, V>> consumer, Predicate<TreeEntryNode<K, V>> breakTraverse) {
breakTraverse = ObjectUtil.defaultIfNull(breakTraverse, n -> false); breakTraverse = ObjectUtil.defaultIfNull(breakTraverse, a -> n -> false);
TreeEntryNode<K, V> curr = includeCurrent ? this : this.parent; TreeEntryNode<K, V> curr = includeCurrent ? this : this.parent;
while (ObjectUtil.isNotNull(curr)) { while (ObjectUtil.isNotNull(curr)) {
consumer.accept(curr); consumer.accept(curr);

View File

@ -8,11 +8,8 @@ import cn.hutool.core.map.MapUtil;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Collection; import java.util.*;
import java.util.Enumeration; import java.util.function.Function;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
@ -310,6 +307,7 @@ public class ObjectUtil {
* @throws NullPointerException {@code defaultValueSupplier == null} 抛出 * @throws NullPointerException {@code defaultValueSupplier == null} 抛出
* @since 5.7.20 * @since 5.7.20
*/ */
@Deprecated
public static <T> T defaultIfNull(T source, Supplier<? extends T> defaultValueSupplier) { public static <T> T defaultIfNull(T source, Supplier<? extends T> defaultValueSupplier) {
if (isNull(source)) { if (isNull(source)) {
return defaultValueSupplier.get(); return defaultValueSupplier.get();
@ -317,6 +315,23 @@ public class ObjectUtil {
return source; return source;
} }
/**
* 如果被检查对象为 {@code null} 返回默认值 defaultValueSupplier 提供否则直接返回
*
* @param source 被检查对象
* @param defaultValueSupplier 默认值提供者
* @param <T> 对象类型
* @return 被检查对象为{@code null}返回默认值否则返回自定义handle处理后的返回值
* @throws NullPointerException {@code defaultValueSupplier == null} 抛出
* @since 5.7.20
*/
public static <T> T defaultIfNull(T source, Function<T , ? extends T> defaultValueSupplier) {
if (isNull(source)) {
return defaultValueSupplier.apply(null);
}
return source;
}
/** /**
* 如果给定对象为{@code null} 返回默认值, 如果不为null 返回自定义handle处理后的返回值 * 如果给定对象为{@code null} 返回默认值, 如果不为null 返回自定义handle处理后的返回值
* *
@ -327,6 +342,7 @@ public class ObjectUtil {
* @return 处理后的返回值 * @return 处理后的返回值
* @since 5.4.6 * @since 5.4.6
*/ */
@Deprecated
public static <T> T defaultIfNull(Object source, Supplier<? extends T> handle, final T defaultValue) { public static <T> T defaultIfNull(Object source, Supplier<? extends T> handle, final T defaultValue) {
if (isNotNull(source)) { if (isNotNull(source)) {
return handle.get(); return handle.get();
@ -334,6 +350,41 @@ public class ObjectUtil {
return defaultValue; return defaultValue;
} }
/**
* 如果给定对象为{@code null} 返回默认值, 如果不为null 返回自定义handle处理后的返回值
*
* @param source Object 类型对象
* @param handle 非空时自定义的处理方法
* @param defaultValue 默认为空的返回值
* @param <T> 被检查对象为{@code null}返回默认值否则返回自定义handle处理后的返回值
* @return 处理后的返回值
* @since 5.4.6
*/
public static <T,R> T defaultIfNull(R source, Function<R, ? extends T> handle, final T defaultValue) {
if (isNotNull(source)) {
return handle.apply(source);
}
return defaultValue;
}
/**
* 如果给定对象为{@code null}或者""返回默认值, 否则返回自定义handle处理后的返回值
*
* @param str String 类型
* @param handle 自定义的处理方法
* @param defaultValue 默认为空的返回值
* @param <T> 被检查对象为{@code null}或者 ""返回默认值否则返回自定义handle处理后的返回值
* @return 处理后的返回值
* @since 5.4.6
*/
@Deprecated
public static <T> T defaultIfEmpty(String str, Supplier<? extends T> handle, final T defaultValue) {
if (StrUtil.isNotEmpty(str)) {
return handle.get();
}
return defaultValue;
}
/** /**
* 如果给定对象为{@code null}或者""返回默认值, 否则返回自定义handle处理后的返回值 * 如果给定对象为{@code null}或者""返回默认值, 否则返回自定义handle处理后的返回值
* *
@ -344,9 +395,9 @@ public class ObjectUtil {
* @return 处理后的返回值 * @return 处理后的返回值
* @since 5.4.6 * @since 5.4.6
*/ */
public static <T> T defaultIfEmpty(String str, Supplier<? extends T> handle, final T defaultValue) { public static <T> T defaultIfEmpty(String str, Function<CharSequence, ? extends T> handle, final T defaultValue) {
if (StrUtil.isNotEmpty(str)) { if (StrUtil.isNotEmpty(str)) {
return handle.get(); return handle.apply(str);
} }
return defaultValue; return defaultValue;
} }
@ -382,6 +433,7 @@ public class ObjectUtil {
* @throws NullPointerException {@code defaultValueSupplier == null} 抛出 * @throws NullPointerException {@code defaultValueSupplier == null} 抛出
* @since 5.7.20 * @since 5.7.20
*/ */
@Deprecated
public static <T extends CharSequence> T defaultIfEmpty(T str, Supplier<? extends T> defaultValueSupplier) { public static <T extends CharSequence> T defaultIfEmpty(T str, Supplier<? extends T> defaultValueSupplier) {
if (StrUtil.isEmpty(str)) { if (StrUtil.isEmpty(str)) {
return defaultValueSupplier.get(); return defaultValueSupplier.get();
@ -389,6 +441,23 @@ public class ObjectUtil {
return str; return str;
} }
/**
* 如果被检查对象为 {@code null} "" 返回默认值 defaultValueSupplier 提供否则直接返回
*
* @param str 被检查对象
* @param defaultValueSupplier 默认值提供者
* @param <T> 对象类型必须实现CharSequence接口
* @return 被检查对象为{@code null}返回默认值否则返回自定义handle处理后的返回值
* @throws NullPointerException {@code defaultValueSupplier == null} 抛出
* @since 5.7.20
*/
public static <T extends CharSequence> T defaultIfEmpty(T str, Function<T, ? extends T> defaultValueSupplier) {
if (StrUtil.isEmpty(str)) {
return defaultValueSupplier.apply(null);
}
return str;
}
/** /**
* 如果给定对象为{@code null}或者""或者空白符返回默认值 * 如果给定对象为{@code null}或者""或者空白符返回默认值
* *
@ -420,6 +489,7 @@ public class ObjectUtil {
* @throws NullPointerException {@code defaultValueSupplier == null} 抛出 * @throws NullPointerException {@code defaultValueSupplier == null} 抛出
* @since 5.7.20 * @since 5.7.20
*/ */
@Deprecated
public static <T extends CharSequence> T defaultIfBlank(T str, Supplier<? extends T> defaultValueSupplier) { public static <T extends CharSequence> T defaultIfBlank(T str, Supplier<? extends T> defaultValueSupplier) {
if (StrUtil.isBlank(str)) { if (StrUtil.isBlank(str)) {
return defaultValueSupplier.get(); return defaultValueSupplier.get();
@ -427,6 +497,23 @@ public class ObjectUtil {
return str; return str;
} }
/**
* 如果被检查对象为 {@code null} "" 空白字符串时返回默认值 defaultValueSupplier 提供否则直接返回
*
* @param str 被检查对象
* @param defaultValueSupplier 默认值提供者
* @param <T> 对象类型必须实现CharSequence接口
* @return 被检查对象为{@code null}返回默认值否则返回自定义handle处理后的返回值
* @throws NullPointerException {@code defaultValueSupplier == null} 抛出
* @since 5.7.20
*/
public static <T extends CharSequence> T defaultIfBlank(T str, Function<T, ? extends T> defaultValueSupplier) {
if (StrUtil.isBlank(str)) {
return defaultValueSupplier.apply(null);
}
return str;
}
/** /**
* 克隆对象<br> * 克隆对象<br>
* 如果对象实现Cloneable接口调用其clone方法<br> * 如果对象实现Cloneable接口调用其clone方法<br>

View File

@ -15,15 +15,15 @@ import java.util.Map;
public class ObjectUtilTest { public class ObjectUtilTest {
@Test @Test
public void equalsTest(){ public void equalsTest() {
Object a = null; Object a = null;
Object b = null; Object b = null;
Assert.assertTrue(ObjectUtil.equals(a, b)); Assert.assertTrue(ObjectUtil.equals(a, b));
} }
@Test @Test
public void lengthTest(){ public void lengthTest() {
int[] array = new int[]{1,2,3,4,5}; int[] array = new int[]{1, 2, 3, 4, 5};
int length = ObjectUtil.length(array); int length = ObjectUtil.length(array);
Assert.assertEquals(5, length); Assert.assertEquals(5, length);
@ -36,8 +36,8 @@ public class ObjectUtilTest {
} }
@Test @Test
public void containsTest(){ public void containsTest() {
int[] array = new int[]{1,2,3,4,5}; int[] array = new int[]{1, 2, 3, 4, 5};
final boolean contains = ObjectUtil.contains(array, 1); final boolean contains = ObjectUtil.contains(array, 1);
Assert.assertTrue(contains); Assert.assertTrue(contains);
@ -73,6 +73,14 @@ public class ObjectUtilTest {
Instant result2 = ObjectUtil.defaultIfNull(nullValue, Instant result2 = ObjectUtil.defaultIfNull(nullValue,
() -> DateUtil.parse(nullValue, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant.now()); () -> DateUtil.parse(nullValue, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant.now());
Assert.assertNotNull(result2); Assert.assertNotNull(result2);
Obj obj = new Obj();
Obj objNull = null;
String result3 = ObjectUtil.defaultIfNull(obj, (a) -> obj.doSomeThing(), "fail");
Assert.assertNotNull(result3);
String result4 = ObjectUtil.defaultIfNull(objNull, Obj::doSomeThing, "fail");
Assert.assertNotNull(result4);
} }
@Test @Test
@ -88,14 +96,14 @@ public class ObjectUtilTest {
} }
@Test @Test
public void isBasicTypeTest(){ public void isBasicTypeTest() {
int a = 1; int a = 1;
final boolean basicType = ObjectUtil.isBasicType(a); final boolean basicType = ObjectUtil.isBasicType(a);
Assert.assertTrue(basicType); Assert.assertTrue(basicType);
} }
@Test @Test
public void isNotNullTest(){ public void isNotNullTest() {
String a = null; String a = null;
Assert.assertFalse(ObjectUtil.isNotNull(a)); Assert.assertFalse(ObjectUtil.isNotNull(a));
} }