mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
Merge pull request #2094 from wangliang181230/feature/ObjectUtil-defaultIfXxx_T_defaultValueSupplier
feature: `ObjectUtil` 添加三个 `defaultIfXxxx` 方法,用于节省CPU及内存损耗。
This commit is contained in:
commit
0a4c208777
@ -10,6 +10,7 @@
|
||||
* 【core 】 增加KetamaHash(issue#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)
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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)) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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<>();
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
|
||||
|
@ -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, //
|
||||
|
@ -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>
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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());
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user