mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
Merge pull request #2603 from youtiaoguagua/v5-dev
fix(bug): change ObjectUtil.defaultIfXXX param type Supplier to Function
This commit is contained in:
commit
a4a0633ed9
@ -164,7 +164,7 @@ public abstract class AbstractTypeAnnotationScanner<T extends AbstractTypeAnnota
|
||||
*/
|
||||
@Override
|
||||
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 Deque<List<Class<?>>> classDeque = CollUtil.newLinkedList(CollUtil.newArrayList(sourceClass));
|
||||
final Set<Class<?>> accessedTypes = new LinkedHashSet<>();
|
||||
|
@ -174,7 +174,7 @@ public interface AnnotationScanner {
|
||||
* @param 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()) {
|
||||
if (AnnotationUtil.isNotJdkMateAnnotation(annotation.annotationType()) && filter.test(annotation)) {
|
||||
consumer.accept(0, annotation);
|
||||
|
@ -35,7 +35,7 @@ public class ElementAnnotationScanner implements AnnotationScanner {
|
||||
*/
|
||||
@Override
|
||||
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())
|
||||
.filter(filter)
|
||||
.forEach(annotation -> consumer.accept(0, annotation));
|
||||
|
@ -36,7 +36,7 @@ public class FieldAnnotationScanner implements AnnotationScanner {
|
||||
*/
|
||||
@Override
|
||||
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()) {
|
||||
if (AnnotationUtil.isNotJdkMateAnnotation(annotation.annotationType()) && filter.test(annotation)) {
|
||||
consumer.accept(0, annotation);
|
||||
|
@ -98,7 +98,7 @@ public class GenericAnnotationScanner implements AnnotationScanner {
|
||||
*/
|
||||
@Override
|
||||
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)) {
|
||||
return;
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ public class MetaAnnotationScanner implements AnnotationScanner {
|
||||
*/
|
||||
@Override
|
||||
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,9 +80,9 @@ public class MetaAnnotationScanner implements AnnotationScanner {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
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<>();
|
||||
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;
|
||||
do {
|
||||
final List<Class<? extends Annotation>> annotationTypes = deque.removeFirst();
|
||||
|
@ -160,7 +160,7 @@ public class DateTime extends Date {
|
||||
* @since 4.1.2
|
||||
*/
|
||||
public DateTime(Date date, TimeZone timeZone) {
|
||||
this(ObjectUtil.defaultIfNull(date, Date::new).getTime(), timeZone);
|
||||
this(ObjectUtil.defaultIfNull(date, new Date()).getTime(), timeZone);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -117,7 +117,7 @@ public class FastByteArrayOutputStream extends OutputStream {
|
||||
*/
|
||||
public String toString(Charset charset) {
|
||||
return new String(toByteArray(),
|
||||
ObjectUtil.defaultIfNull(charset, CharsetUtil::defaultCharset));
|
||||
ObjectUtil.defaultIfNull(charset, CharsetUtil.defaultCharset()));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ public class ResourceClassLoader<T extends Resource> extends SecureClassLoader {
|
||||
*/
|
||||
public ResourceClassLoader(ClassLoader parentClassLoader, Map<String, T> resourceMap) {
|
||||
super(ObjectUtil.defaultIfNull(parentClassLoader, ClassLoaderUtil::getClassLoader));
|
||||
this.resourceMap = ObjectUtil.defaultIfNull(resourceMap, HashMap::new);
|
||||
this.resourceMap = ObjectUtil.defaultIfNull(resourceMap, new HashMap<>());
|
||||
this.cacheClassMap = new HashMap<>();
|
||||
}
|
||||
|
||||
|
@ -442,7 +442,7 @@ public class LinkedForestMap<K, V> implements ForestMap<K, V> {
|
||||
*/
|
||||
TreeEntryNode<K, V> traverseParentNodes(
|
||||
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;
|
||||
while (ObjectUtil.isNotNull(curr)) {
|
||||
consumer.accept(curr);
|
||||
|
@ -8,11 +8,8 @@ import cn.hutool.core.map.MapUtil;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
@ -310,6 +307,7 @@ public class ObjectUtil {
|
||||
* @throws NullPointerException {@code defaultValueSupplier == null} 时,抛出
|
||||
* @since 5.7.20
|
||||
*/
|
||||
@Deprecated
|
||||
public static <T> T defaultIfNull(T source, Supplier<? extends T> defaultValueSupplier) {
|
||||
if (isNull(source)) {
|
||||
return defaultValueSupplier.get();
|
||||
@ -317,6 +315,23 @@ public class ObjectUtil {
|
||||
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处理后的返回值
|
||||
*
|
||||
@ -327,6 +342,7 @@ public class ObjectUtil {
|
||||
* @return 处理后的返回值
|
||||
* @since 5.4.6
|
||||
*/
|
||||
@Deprecated
|
||||
public static <T> T defaultIfNull(Object source, Supplier<? extends T> handle, final T defaultValue) {
|
||||
if (isNotNull(source)) {
|
||||
return handle.get();
|
||||
@ -334,6 +350,41 @@ public class ObjectUtil {
|
||||
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处理后的返回值
|
||||
*
|
||||
@ -344,9 +395,9 @@ public class ObjectUtil {
|
||||
* @return 处理后的返回值
|
||||
* @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)) {
|
||||
return handle.get();
|
||||
return handle.apply(str);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
@ -382,6 +433,7 @@ public class ObjectUtil {
|
||||
* @throws NullPointerException {@code defaultValueSupplier == null} 时,抛出
|
||||
* @since 5.7.20
|
||||
*/
|
||||
@Deprecated
|
||||
public static <T extends CharSequence> T defaultIfEmpty(T str, Supplier<? extends T> defaultValueSupplier) {
|
||||
if (StrUtil.isEmpty(str)) {
|
||||
return defaultValueSupplier.get();
|
||||
@ -389,6 +441,23 @@ public class ObjectUtil {
|
||||
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}或者""或者空白符返回默认值
|
||||
*
|
||||
@ -420,6 +489,7 @@ public class ObjectUtil {
|
||||
* @throws NullPointerException {@code defaultValueSupplier == null} 时,抛出
|
||||
* @since 5.7.20
|
||||
*/
|
||||
@Deprecated
|
||||
public static <T extends CharSequence> T defaultIfBlank(T str, Supplier<? extends T> defaultValueSupplier) {
|
||||
if (StrUtil.isBlank(str)) {
|
||||
return defaultValueSupplier.get();
|
||||
@ -427,6 +497,23 @@ public class ObjectUtil {
|
||||
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>
|
||||
* 如果对象实现Cloneable接口,调用其clone方法<br>
|
||||
|
@ -15,15 +15,15 @@ import java.util.Map;
|
||||
public class ObjectUtilTest {
|
||||
|
||||
@Test
|
||||
public void equalsTest(){
|
||||
public void equalsTest() {
|
||||
Object a = null;
|
||||
Object b = null;
|
||||
Assert.assertTrue(ObjectUtil.equals(a, b));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lengthTest(){
|
||||
int[] array = new int[]{1,2,3,4,5};
|
||||
public void lengthTest() {
|
||||
int[] array = new int[]{1, 2, 3, 4, 5};
|
||||
int length = ObjectUtil.length(array);
|
||||
Assert.assertEquals(5, length);
|
||||
|
||||
@ -36,8 +36,8 @@ public class ObjectUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void containsTest(){
|
||||
int[] array = new int[]{1,2,3,4,5};
|
||||
public void containsTest() {
|
||||
int[] array = new int[]{1, 2, 3, 4, 5};
|
||||
|
||||
final boolean contains = ObjectUtil.contains(array, 1);
|
||||
Assert.assertTrue(contains);
|
||||
@ -73,6 +73,14 @@ public class ObjectUtilTest {
|
||||
Instant result2 = ObjectUtil.defaultIfNull(nullValue,
|
||||
() -> DateUtil.parse(nullValue, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant.now());
|
||||
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
|
||||
@ -88,14 +96,14 @@ public class ObjectUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isBasicTypeTest(){
|
||||
public void isBasicTypeTest() {
|
||||
int a = 1;
|
||||
final boolean basicType = ObjectUtil.isBasicType(a);
|
||||
Assert.assertTrue(basicType);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotNullTest(){
|
||||
public void isNotNullTest() {
|
||||
String a = null;
|
||||
Assert.assertFalse(ObjectUtil.isNotNull(a));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user