Merge pull request #2094 from wangliang181230/feature/ObjectUtil-defaultIfXxx_T_defaultValueSupplier

feature: `ObjectUtil` 添加三个 `defaultIfXxxx` 方法,用于节省CPU及内存损耗。
This commit is contained in:
Golden Looly 2022-01-14 09:39:12 +08:00 committed by GitHub
commit 0a4c208777
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 75 additions and 24 deletions

View File

@ -10,6 +10,7 @@
* 【core 】 增加KetamaHashissue#2084@Github
* 【crypto 】 增加SignUtil
* 【json 】 JSONGetter增加getBeanList方法
* 【core 】 `ObjectUtil` 添加三个 `defaultIfXxxx`方法用于节省CPU及内存损耗。 [pr#2094@Github](https://github.com/dromara/hutool/pull/2094)
*
### 🐞Bug修复
* 【core 】 修复setter重载导致匹配错误issue#2082@Github

View File

@ -738,7 +738,7 @@ public class BeanUtil {
* @param copyOptions 拷贝选项 {@link CopyOptions}
*/
public static void copyProperties(Object source, Object target, CopyOptions copyOptions) {
BeanCopier.create(source, target, ObjectUtil.defaultIfNull(copyOptions, CopyOptions.create())).copy();
BeanCopier.create(source, target, ObjectUtil.defaultIfNull(copyOptions, CopyOptions::create)).copy();
}
/**

View File

@ -44,7 +44,7 @@ class JavaClassFileManager extends ForwardingJavaFileManager<JavaFileManager> {
*/
protected JavaClassFileManager(ClassLoader parent, JavaFileManager fileManager) {
super(fileManager);
this.parent = ObjectUtil.defaultIfNull(parent, ClassLoaderUtil.getClassLoader());
this.parent = ObjectUtil.defaultIfNull(parent, ClassLoaderUtil::getClassLoader);
}
/**

View File

@ -89,7 +89,7 @@ public class JavaSourceCompiler {
* @param parent 父类加载器null则使用默认类加载器
*/
private JavaSourceCompiler(ClassLoader parent) {
this.parentClassLoader = ObjectUtil.defaultIfNull(parent, ClassLoaderUtil.getClassLoader());
this.parentClassLoader = ObjectUtil.defaultIfNull(parent, ClassLoaderUtil::getClassLoader);
}
/**

View File

@ -227,7 +227,7 @@ public class TemporalAccessorConverter extends AbstractConverter<TemporalAccesso
return instant;
}
zoneId = ObjectUtil.defaultIfNull(zoneId, ZoneId.systemDefault());
zoneId = ObjectUtil.defaultIfNull(zoneId, ZoneId::systemDefault);
TemporalAccessor result = null;
if (LocalDateTime.class.equals(this.targetType)) {

View File

@ -143,7 +143,7 @@ public class DateTime extends Date {
* @since 4.1.2
*/
public DateTime(Date date, TimeZone timeZone) {
this(ObjectUtil.defaultIfNull(date, new Date()).getTime(), timeZone);
this(ObjectUtil.defaultIfNull(date, Date::new).getTime(), timeZone);
}
/**
@ -216,7 +216,7 @@ public class DateTime extends Date {
*/
public DateTime(long timeMillis, TimeZone timeZone) {
super(timeMillis);
this.timeZone = ObjectUtil.defaultIfNull(timeZone, TimeZone.getDefault());
this.timeZone = ObjectUtil.defaultIfNull(timeZone, TimeZone::getDefault);
}
/**
@ -913,7 +913,7 @@ public class DateTime extends Date {
* @since 4.1.2
*/
public DateTime setTimeZone(TimeZone timeZone) {
this.timeZone = ObjectUtil.defaultIfNull(timeZone, TimeZone.getDefault());
this.timeZone = ObjectUtil.defaultIfNull(timeZone, TimeZone::getDefault);
return this;
}

View File

@ -88,7 +88,7 @@ public class LocalDateTimeUtil {
return null;
}
return LocalDateTime.ofInstant(instant, ObjectUtil.defaultIfNull(zoneId, ZoneId.systemDefault()));
return LocalDateTime.ofInstant(instant, ObjectUtil.defaultIfNull(zoneId, ZoneId::systemDefault));
}
/**
@ -103,7 +103,7 @@ public class LocalDateTimeUtil {
return null;
}
return of(instant, ObjectUtil.defaultIfNull(timeZone, TimeZone.getDefault()).toZoneId());
return of(instant, ObjectUtil.defaultIfNull(timeZone, TimeZone::getDefault).toZoneId());
}
/**

View File

@ -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));
}
}

View File

@ -69,7 +69,7 @@ public class ClassPathResource extends UrlResource {
this.path = path;
this.name = StrUtil.isBlank(path) ? null : FileUtil.getName(path);
this.classLoader = ObjectUtil.defaultIfNull(classLoader, ClassUtil.getClassLoader());
this.classLoader = ObjectUtil.defaultIfNull(classLoader, ClassUtil::getClassLoader);
this.clazz = clazz;
initUrl();
}

View File

@ -60,7 +60,7 @@ public class FileResource implements Resource, Serializable {
public FileResource(File file, String fileName) {
Assert.notNull(file, "File must be not null !");
this.file = file;
this.name = ObjectUtil.defaultIfNull(fileName, file.getName());
this.name = ObjectUtil.defaultIfNull(fileName, file::getName);
}
// ----------------------------------------------------------------------- Constructor end

View File

@ -36,7 +36,7 @@ public class UrlResource implements Resource, Serializable{
*/
public UrlResource(URL url, String name) {
this.url = url;
this.name = ObjectUtil.defaultIfNull(name, (null != url) ? FileUtil.getName(url.getPath()) : null);
this.name = ObjectUtil.defaultIfNull(name, () -> (null != url ? FileUtil.getName(url.getPath()) : null));
}
/**

View File

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

View File

@ -49,7 +49,7 @@ public class CsvBaseReader implements Serializable {
* @param config 配置项
*/
public CsvBaseReader(CsvReadConfig config) {
this.config = ObjectUtil.defaultIfNull(config, CsvReadConfig.defaultConfig());
this.config = ObjectUtil.defaultIfNull(config, CsvReadConfig::defaultConfig);
}
//--------------------------------------------------------------------------------------------- Constructor end

View File

@ -80,7 +80,7 @@ public final class CsvParser extends ComputeIter<CsvRow> implements Closeable, S
*/
public CsvParser(final Reader reader, CsvReadConfig config) {
this.reader = Objects.requireNonNull(reader, "reader must not be null");
this.config = ObjectUtil.defaultIfNull(config, CsvReadConfig.defaultConfig());
this.config = ObjectUtil.defaultIfNull(config, CsvReadConfig::defaultConfig);
}
/**

View File

@ -149,7 +149,7 @@ public final class CsvWriter implements Closeable, Flushable, Serializable {
*/
public CsvWriter(Writer writer, CsvWriteConfig config) {
this.writer = (writer instanceof BufferedWriter) ? writer : new BufferedWriter(writer);
this.config = ObjectUtil.defaultIfNull(config, CsvWriteConfig.defaultConfig());
this.config = ObjectUtil.defaultIfNull(config, CsvWriteConfig::defaultConfig);
}
// --------------------------------------------------------------------------------------------------- Constructor end

View File

@ -242,7 +242,7 @@ public class ExecutorBuilder implements Builder<ThreadPoolExecutor> {
workQueue = (corePoolSize <= 0) ? new SynchronousQueue<>() : new LinkedBlockingQueue<>(DEFAULT_QUEUE_CAPACITY);
}
final ThreadFactory threadFactory = (null != builder.threadFactory) ? builder.threadFactory : Executors.defaultThreadFactory();
RejectedExecutionHandler handler = ObjectUtil.defaultIfNull(builder.handler, new ThreadPoolExecutor.AbortPolicy());
RejectedExecutionHandler handler = ObjectUtil.defaultIfNull(builder.handler, ThreadPoolExecutor.AbortPolicy::new);
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(//
corePoolSize, //

View File

@ -295,6 +295,22 @@ public class ObjectUtil {
return isNull(object) ? defaultValue : object;
}
/**
* 如果被检查对象为 {@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, Supplier<? extends T> defaultValueSupplier) {
if (isNull(source)) {
return defaultValueSupplier.get();
}
return source;
}
/**
* 如果给定对象为{@code null} 返回默认值, 如果不为null 返回自定义handle处理后的返回值
@ -351,6 +367,23 @@ public class ObjectUtil {
return StrUtil.isEmpty(str) ? defaultValue : 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, Supplier<? extends T> defaultValueSupplier) {
if (StrUtil.isEmpty(str)) {
return defaultValueSupplier.get();
}
return str;
}
/**
* 如果给定对象为{@code null}或者""或者空白符返回默认值
*
@ -372,6 +405,23 @@ public class ObjectUtil {
return StrUtil.isBlank(str) ? defaultValue : 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, Supplier<? extends T> defaultValueSupplier) {
if (StrUtil.isBlank(str)) {
return defaultValueSupplier.get();
}
return str;
}
/**
* 克隆对象<br>
* 如果对象实现Cloneable接口调用其clone方法<br>

View File

@ -76,7 +76,7 @@ public class ServiceLoaderUtil {
* @return 服务接口实现列表
*/
public static <T> ServiceLoader<T> load(Class<T> clazz, ClassLoader loader) {
return ServiceLoader.load(clazz, ObjectUtil.defaultIfNull(loader, ClassLoaderUtil.getClassLoader()));
return ServiceLoader.load(clazz, ObjectUtil.defaultIfNull(loader, ClassLoaderUtil::getClassLoader));
}
/**

View File

@ -169,7 +169,7 @@ public class UserAgentUtilTest {
Assert.assertEquals("63.0.3239.132", ua.getVersion());
Assert.assertEquals("Webkit", ua.getEngine().toString());
Assert.assertEquals("537.36", ua.getEngineVersion());
Assert.assertEquals("Windows 8.1 or Winsows Server 2012R2", ua.getOs().toString());
Assert.assertEquals("Windows 8.1 or Windows Server 2012R2", ua.getOs().toString());
Assert.assertEquals("6.3", ua.getOsVersion());
Assert.assertEquals("Windows", ua.getPlatform().toString());
Assert.assertFalse(ua.isMobile());

View File

@ -95,7 +95,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
*/
public JSONArray(int initialCapacity, JSONConfig config) {
this.rawList = new ArrayList<>(initialCapacity);
this.config = ObjectUtil.defaultIfNull(config, JSONConfig.create());
this.config = ObjectUtil.defaultIfNull(config, JSONConfig::create);
}
/**

View File

@ -376,7 +376,7 @@ public class CellUtil {
public static Cell getMergedRegionCell(Sheet sheet, int x, int y) {
return ObjectUtil.defaultIfNull(
getCellIfMergedRegion(sheet, x, y),
SheetUtil.getCell(sheet, y, x));
() -> SheetUtil.getCell(sheet, y, x));
}
/**

View File

@ -294,7 +294,7 @@ public class SheetDataSaxHandler extends DefaultHandler {
final int numFmtIndex = xssfCellStyle.getDataFormat();
this.numFmtString = ObjectUtil.defaultIfNull(
xssfCellStyle.getDataFormatString(),
BuiltinFormats.getBuiltinFormat(numFmtIndex));
() -> BuiltinFormats.getBuiltinFormat(numFmtIndex));
if (CellDataType.NUMBER == this.cellDataType && ExcelSaxUtil.isDateFormat(numFmtIndex, numFmtString)) {
cellDataType = CellDataType.DATE;
}