mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
bd74cdb078
commit
1a0d7b51a7
@ -514,7 +514,7 @@ public class BeanUtil {
|
||||
return bean;
|
||||
}
|
||||
|
||||
return BeanCopier.create(valueProvider, bean, copyOptions).copy();
|
||||
return BeanCopier.of(valueProvider, bean, copyOptions).copy();
|
||||
}
|
||||
// --------------------------------------------------------------------------------------------- beanToMap
|
||||
|
||||
@ -604,7 +604,7 @@ public class BeanUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
return BeanCopier.create(bean, targetMap,
|
||||
return BeanCopier.of(bean, targetMap,
|
||||
CopyOptions.of()
|
||||
.setIgnoreNullValue(ignoreNullValue)
|
||||
.setFieldEditor(keyEditor)
|
||||
@ -634,7 +634,7 @@ public class BeanUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
return BeanCopier.create(bean, targetMap, copyOptions).copy();
|
||||
return BeanCopier.of(bean, targetMap, copyOptions).copy();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------- copyProperties
|
||||
@ -677,7 +677,7 @@ public class BeanUtil {
|
||||
* @param ignoreCase 是否忽略大小写
|
||||
*/
|
||||
public static void copyProperties(final Object source, final Object target, final boolean ignoreCase) {
|
||||
BeanCopier.create(source, target, CopyOptions.of().setIgnoreCase(ignoreCase)).copy();
|
||||
BeanCopier.of(source, target, CopyOptions.of().setIgnoreCase(ignoreCase)).copy();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -692,7 +692,7 @@ public class BeanUtil {
|
||||
if (null == source || null == target) {
|
||||
return;
|
||||
}
|
||||
BeanCopier.create(source, target, ObjUtil.defaultIfNull(copyOptions, CopyOptions::of)).copy();
|
||||
BeanCopier.of(source, target, ObjUtil.defaultIfNull(copyOptions, CopyOptions::of)).copy();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -28,7 +28,7 @@ public class DynaBean implements Cloneable, Serializable {
|
||||
* @param bean 普通Bean
|
||||
* @return DynaBean
|
||||
*/
|
||||
public static DynaBean create(final Object bean) {
|
||||
public static DynaBean of(final Object bean) {
|
||||
return new DynaBean(bean);
|
||||
}
|
||||
|
||||
@ -38,7 +38,7 @@ public class DynaBean implements Cloneable, Serializable {
|
||||
* @param beanClass Bean类
|
||||
* @return DynaBean
|
||||
*/
|
||||
public static DynaBean create(final Class<?> beanClass) {
|
||||
public static DynaBean of(final Class<?> beanClass) {
|
||||
return new DynaBean(beanClass);
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ public class DynaBean implements Cloneable, Serializable {
|
||||
* @param params 构造Bean所需要的参数
|
||||
* @return DynaBean
|
||||
*/
|
||||
public static DynaBean create(final Class<?> beanClass, final Object... params) {
|
||||
public static DynaBean of(final Class<?> beanClass, final Object... params) {
|
||||
return new DynaBean(beanClass, params);
|
||||
}
|
||||
|
||||
|
@ -35,8 +35,8 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
|
||||
* @param copyOptions 拷贝属性选项
|
||||
* @return BeanCopier
|
||||
*/
|
||||
public static <T> BeanCopier<T> create(final Object source, final T target, final CopyOptions copyOptions) {
|
||||
return create(source, target, target.getClass(), copyOptions);
|
||||
public static <T> BeanCopier<T> of(final Object source, final T target, final CopyOptions copyOptions) {
|
||||
return of(source, target, target.getClass(), copyOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -49,7 +49,7 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
|
||||
* @param copyOptions 拷贝属性选项
|
||||
* @return BeanCopier
|
||||
*/
|
||||
public static <T> BeanCopier<T> create(final Object source, final T target, final Type destType, final CopyOptions copyOptions) {
|
||||
public static <T> BeanCopier<T> of(final Object source, final T target, final Type destType, final CopyOptions copyOptions) {
|
||||
return new BeanCopier<>(source, target, destType, copyOptions);
|
||||
}
|
||||
|
||||
|
@ -74,8 +74,8 @@ public class Hashids implements Encoder<long[], String>, Decoder<String, long[]>
|
||||
* @param salt 加盐值
|
||||
* @return {@code Hashids}
|
||||
*/
|
||||
public static Hashids create(final char[] salt) {
|
||||
return create(salt, DEFAULT_ALPHABET, -1);
|
||||
public static Hashids of(final char[] salt) {
|
||||
return of(salt, DEFAULT_ALPHABET, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -85,8 +85,8 @@ public class Hashids implements Encoder<long[], String>, Decoder<String, long[]>
|
||||
* @param minLength 限制最小长度,-1表示不限制
|
||||
* @return {@code Hashids}
|
||||
*/
|
||||
public static Hashids create(final char[] salt, final int minLength) {
|
||||
return create(salt, DEFAULT_ALPHABET, minLength);
|
||||
public static Hashids of(final char[] salt, final int minLength) {
|
||||
return of(salt, DEFAULT_ALPHABET, minLength);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -97,7 +97,7 @@ public class Hashids implements Encoder<long[], String>, Decoder<String, long[]>
|
||||
* @param minLength 限制最小长度,-1表示不限制
|
||||
* @return {@code Hashids}
|
||||
*/
|
||||
public static Hashids create(final char[] salt, final char[] alphabet, final int minLength) {
|
||||
public static Hashids of(final char[] salt, final char[] alphabet, final int minLength) {
|
||||
return new Hashids(salt, alphabet, minLength);
|
||||
}
|
||||
// endregion
|
||||
|
@ -73,7 +73,7 @@ public class BeanConverter implements Converter, Serializable {
|
||||
}
|
||||
|
||||
//限定被转换对象类型
|
||||
return BeanCopier.create(value, ConstructorUtil.newInstanceIfPossible(targetClass), targetType, this.copyOptions).copy();
|
||||
return BeanCopier.of(value, ConstructorUtil.newInstanceIfPossible(targetClass), targetType, this.copyOptions).copy();
|
||||
} else if (value instanceof byte[]) {
|
||||
// 尝试反序列化
|
||||
return ObjUtil.deserialize((byte[]) value);
|
||||
|
@ -48,7 +48,7 @@ public class ChineseDate {
|
||||
* @param date 公历日期
|
||||
*/
|
||||
public ChineseDate(final Date date) {
|
||||
this(LocalDateTimeUtil.ofDate(date.toInstant()));
|
||||
this(TimeUtil.ofDate(date.toInstant()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -32,7 +32,7 @@ public class DateBetween implements Serializable {
|
||||
* @return DateBetween
|
||||
* @since 3.2.3
|
||||
*/
|
||||
public static DateBetween create(final Date begin, final Date end) {
|
||||
public static DateBetween of(final Date begin, final Date end) {
|
||||
return new DateBetween(begin, end);
|
||||
}
|
||||
|
||||
@ -46,7 +46,7 @@ public class DateBetween implements Serializable {
|
||||
* @return DateBetween
|
||||
* @since 3.2.3
|
||||
*/
|
||||
public static DateBetween create(final Date begin, final Date end, final boolean isAbs) {
|
||||
public static DateBetween of(final Date begin, final Date end, final boolean isAbs) {
|
||||
return new DateBetween(begin, end, isAbs);
|
||||
}
|
||||
|
||||
|
@ -736,7 +736,7 @@ public class DateTime extends Date {
|
||||
* @since 5.7.16
|
||||
*/
|
||||
public LocalDateTime toLocalDateTime() {
|
||||
return LocalDateTimeUtil.of(this);
|
||||
return TimeUtil.of(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -37,7 +37,7 @@ import java.util.stream.Collectors;
|
||||
* 时间工具类
|
||||
*
|
||||
* @author xiaoleilu
|
||||
* @see LocalDateTimeUtil java8日志工具类
|
||||
* @see TimeUtil java8日志工具类
|
||||
* @see DatePattern 日期常用格式工具类
|
||||
*/
|
||||
public class DateUtil extends CalendarUtil {
|
||||
@ -492,7 +492,7 @@ public class DateUtil extends CalendarUtil {
|
||||
* @return 格式化后的字符串
|
||||
*/
|
||||
public static String formatLocalDateTime(final LocalDateTime localDateTime) {
|
||||
return LocalDateTimeUtil.formatNormal(localDateTime);
|
||||
return TimeUtil.formatNormal(localDateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -503,7 +503,7 @@ public class DateUtil extends CalendarUtil {
|
||||
* @return 格式化后的字符串
|
||||
*/
|
||||
public static String format(final LocalDateTime localDateTime, final String format) {
|
||||
return LocalDateTimeUtil.format(localDateTime, format);
|
||||
return TimeUtil.format(localDateTime, format);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -675,7 +675,7 @@ public class DateUtil extends CalendarUtil {
|
||||
* @return LocalDateTime对象
|
||||
*/
|
||||
public static LocalDateTime parseLocalDateTime(final CharSequence dateStr, final String format) {
|
||||
return LocalDateTimeUtil.parse(dateStr, format);
|
||||
return TimeUtil.parse(dateStr, format);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2050,11 +2050,11 @@ public class DateUtil extends CalendarUtil {
|
||||
*
|
||||
* @param instant {@link Instant}
|
||||
* @return {@link LocalDateTime}
|
||||
* @see LocalDateTimeUtil#of(Instant)
|
||||
* @see TimeUtil#of(Instant)
|
||||
* @since 5.0.5
|
||||
*/
|
||||
public static LocalDateTime toLocalDateTime(final Instant instant) {
|
||||
return LocalDateTimeUtil.of(instant);
|
||||
return TimeUtil.of(instant);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2062,11 +2062,11 @@ public class DateUtil extends CalendarUtil {
|
||||
*
|
||||
* @param date {@link Date}
|
||||
* @return {@link LocalDateTime}
|
||||
* @see LocalDateTimeUtil#of(Date)
|
||||
* @see TimeUtil#of(Date)
|
||||
* @since 5.0.5
|
||||
*/
|
||||
public static LocalDateTime toLocalDateTime(final Date date) {
|
||||
return LocalDateTimeUtil.of(date);
|
||||
return TimeUtil.of(date);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -49,7 +49,7 @@ public class StopWatch {
|
||||
* @return StopWatch
|
||||
* @since 5.5.2
|
||||
*/
|
||||
public static StopWatch create(final String id) {
|
||||
public static StopWatch of(final String id) {
|
||||
return new StopWatch(id);
|
||||
}
|
||||
|
||||
|
@ -157,7 +157,7 @@ public class TemporalAccessorUtil extends TemporalUtil{
|
||||
// issue#1891@Github
|
||||
// Instant.from不能完成日期转换
|
||||
//result = Instant.from(temporalAccessor);
|
||||
result = toInstant(LocalDateTimeUtil.of(temporalAccessor));
|
||||
result = toInstant(TimeUtil.of(temporalAccessor));
|
||||
}
|
||||
|
||||
return result;
|
||||
|
17
hutool-core/src/main/java/cn/hutool/core/date/LocalDateTimeUtil.java → hutool-core/src/main/java/cn/hutool/core/date/TimeUtil.java
Executable file → Normal file
17
hutool-core/src/main/java/cn/hutool/core/date/LocalDateTimeUtil.java → hutool-core/src/main/java/cn/hutool/core/date/TimeUtil.java
Executable file → Normal file
@ -27,14 +27,14 @@ import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* JDK8+中的{@link LocalDateTime} 工具类封装
|
||||
* JDK8+中的{@link java.time} 工具类封装
|
||||
*
|
||||
* @author looly
|
||||
* @see DateUtil java7及其以下版本,使用Date工具类
|
||||
* @see DatePattern 常用格式工具类
|
||||
* @since 5.3.9
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class LocalDateTimeUtil {
|
||||
public class TimeUtil {
|
||||
|
||||
/**
|
||||
* 当前时间,默认时区
|
||||
@ -191,7 +191,7 @@ public class LocalDateTimeUtil {
|
||||
|
||||
if (temporalAccessor instanceof LocalDate) {
|
||||
return ((LocalDate) temporalAccessor).atStartOfDay();
|
||||
} else if(temporalAccessor instanceof Instant){
|
||||
} else if (temporalAccessor instanceof Instant) {
|
||||
return LocalDateTime.ofInstant((Instant) temporalAccessor, ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
@ -220,7 +220,7 @@ public class LocalDateTimeUtil {
|
||||
|
||||
if (temporalAccessor instanceof LocalDateTime) {
|
||||
return ((LocalDateTime) temporalAccessor).toLocalDate();
|
||||
} else if(temporalAccessor instanceof Instant){
|
||||
} else if (temporalAccessor instanceof Instant) {
|
||||
return of(temporalAccessor).toLocalDate();
|
||||
}
|
||||
|
||||
@ -251,7 +251,7 @@ public class LocalDateTimeUtil {
|
||||
* @return {@link LocalDateTime}
|
||||
*/
|
||||
public static LocalDateTime parse(final CharSequence text, final DateTimeFormatter formatter) {
|
||||
if (null == text) {
|
||||
if (StrUtil.isBlank(text)) {
|
||||
return null;
|
||||
}
|
||||
if (null == formatter) {
|
||||
@ -269,7 +269,7 @@ public class LocalDateTimeUtil {
|
||||
* @return {@link LocalDateTime}
|
||||
*/
|
||||
public static LocalDateTime parse(CharSequence text, final String format) {
|
||||
if (null == text) {
|
||||
if (StrUtil.isBlank(text)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -585,12 +585,11 @@ public class LocalDateTimeUtil {
|
||||
* <li>如果一年中的第5天是星期一,则第二周从第5周开始,第1至第4周在第1周</li>
|
||||
* </ul>
|
||||
*
|
||||
*
|
||||
* @param date 日期({@link LocalDate} 或者 {@link LocalDateTime}等)
|
||||
* @return 所在年的第几周
|
||||
* @since 5.7.21
|
||||
*/
|
||||
public static int weekOfYear(final TemporalAccessor date){
|
||||
public static int weekOfYear(final TemporalAccessor date) {
|
||||
return TemporalAccessorUtil.get(date, WeekFields.ISO.weekOfYear());
|
||||
}
|
||||
}
|
@ -1089,7 +1089,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static File copy(final File src, final File dest, final boolean isOverride) throws IORuntimeException {
|
||||
return FileCopier.create(src, dest).setOverride(isOverride).copy();
|
||||
return FileCopier.of(src, dest).setOverride(isOverride).copy();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1109,7 +1109,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static File copyContent(final File src, final File dest, final boolean isOverride) throws IORuntimeException {
|
||||
return FileCopier.create(src, dest).setCopyContentIfDir(true).setOverride(isOverride).copy();
|
||||
return FileCopier.of(src, dest).setCopyContentIfDir(true).setOverride(isOverride).copy();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1130,7 +1130,7 @@ public class FileUtil extends PathUtil {
|
||||
* @since 4.1.5
|
||||
*/
|
||||
public static File copyFilesFromDir(final File src, final File dest, final boolean isOverride) throws IORuntimeException {
|
||||
return FileCopier.create(src, dest).setCopyContentIfDir(true).setOnlyCopyFile(true).setOverride(isOverride).copy();
|
||||
return FileCopier.of(src, dest).setCopyContentIfDir(true).setOnlyCopyFile(true).setOverride(isOverride).copy();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1976,7 +1976,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static byte[] readBytes(final File file) throws IORuntimeException {
|
||||
return FileReader.create(file).readBytes();
|
||||
return FileReader.of(file).readBytes();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2023,7 +2023,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static String readString(final File file, final Charset charset) throws IORuntimeException {
|
||||
return FileReader.create(file, charset).readString();
|
||||
return FileReader.of(file, charset).readString();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2130,7 +2130,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static <T extends Collection<String>> T readLines(final File file, final String charset, final T collection) throws IORuntimeException {
|
||||
return FileReader.create(file, CharsetUtil.charset(charset)).readLines(collection);
|
||||
return FileReader.of(file, CharsetUtil.charset(charset)).readLines(collection);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2144,7 +2144,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static <T extends Collection<String>> T readLines(final File file, final Charset charset, final T collection) throws IORuntimeException {
|
||||
return FileReader.create(file, charset).readLines(collection);
|
||||
return FileReader.of(file, charset).readLines(collection);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2299,7 +2299,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static void readLines(final File file, final Charset charset, final LineHandler lineHandler) throws IORuntimeException {
|
||||
FileReader.create(file, charset).readLines(lineHandler);
|
||||
FileReader.of(file, charset).readLines(lineHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2387,7 +2387,7 @@ public class FileUtil extends PathUtil {
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public static <T> T load(final String path, final String charset, final ReaderHandler<T> readerHandler) throws IORuntimeException {
|
||||
return FileReader.create(file(path), CharsetUtil.charset(charset)).read(readerHandler);
|
||||
return FileReader.of(file(path), CharsetUtil.charset(charset)).read(readerHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2402,7 +2402,7 @@ public class FileUtil extends PathUtil {
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public static <T> T load(final String path, final Charset charset, final ReaderHandler<T> readerHandler) throws IORuntimeException {
|
||||
return FileReader.create(file(path), charset).read(readerHandler);
|
||||
return FileReader.of(file(path), charset).read(readerHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2431,7 +2431,7 @@ public class FileUtil extends PathUtil {
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public static <T> T load(final File file, final Charset charset, final ReaderHandler<T> readerHandler) throws IORuntimeException {
|
||||
return FileReader.create(file, charset).read(readerHandler);
|
||||
return FileReader.of(file, charset).read(readerHandler);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------------------- out start
|
||||
@ -2487,7 +2487,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static BufferedWriter getWriter(final File file, final Charset charset, final boolean isAppend) throws IORuntimeException {
|
||||
return FileWriter.create(file, charset).getWriter(isAppend);
|
||||
return FileWriter.of(file, charset).getWriter(isAppend);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2597,7 +2597,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static File writeString(final String content, final File file, final String charset) throws IORuntimeException {
|
||||
return FileWriter.create(file, CharsetUtil.charset(charset)).write(content);
|
||||
return FileWriter.of(file, CharsetUtil.charset(charset)).write(content);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2610,7 +2610,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static File writeString(final String content, final File file, final Charset charset) throws IORuntimeException {
|
||||
return FileWriter.create(file, charset).write(content);
|
||||
return FileWriter.of(file, charset).write(content);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2675,7 +2675,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static File appendString(final String content, final File file, final String charset) throws IORuntimeException {
|
||||
return FileWriter.create(file, CharsetUtil.charset(charset)).append(content);
|
||||
return FileWriter.of(file, CharsetUtil.charset(charset)).append(content);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2688,7 +2688,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static File appendString(final String content, final File file, final Charset charset) throws IORuntimeException {
|
||||
return FileWriter.create(file, charset).append(content);
|
||||
return FileWriter.of(file, charset).append(content);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2910,7 +2910,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static <T> File writeLines(final Collection<T> list, final File file, final String charset, final boolean isAppend) throws IORuntimeException {
|
||||
return FileWriter.create(file, CharsetUtil.charset(charset)).writeLines(list, isAppend);
|
||||
return FileWriter.of(file, CharsetUtil.charset(charset)).writeLines(list, isAppend);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2925,7 +2925,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static <T> File writeLines(final Collection<T> list, final File file, final Charset charset, final boolean isAppend) throws IORuntimeException {
|
||||
return FileWriter.create(file, charset).writeLines(list, isAppend);
|
||||
return FileWriter.of(file, charset).writeLines(list, isAppend);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2940,7 +2940,7 @@ public class FileUtil extends PathUtil {
|
||||
* @since 4.0.5
|
||||
*/
|
||||
public static File writeUtf8Map(final Map<?, ?> map, final File file, final String kvSeparator, final boolean isAppend) throws IORuntimeException {
|
||||
return FileWriter.create(file, CharsetUtil.UTF_8).writeMap(map, kvSeparator, isAppend);
|
||||
return FileWriter.of(file, CharsetUtil.UTF_8).writeMap(map, kvSeparator, isAppend);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2956,7 +2956,7 @@ public class FileUtil extends PathUtil {
|
||||
* @since 4.0.5
|
||||
*/
|
||||
public static File writeMap(final Map<?, ?> map, final File file, final Charset charset, final String kvSeparator, final boolean isAppend) throws IORuntimeException {
|
||||
return FileWriter.create(file, charset).writeMap(map, kvSeparator, isAppend);
|
||||
return FileWriter.of(file, charset).writeMap(map, kvSeparator, isAppend);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2996,7 +2996,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static File writeBytes(final byte[] data, final File dest, final int off, final int len, final boolean isAppend) throws IORuntimeException {
|
||||
return FileWriter.create(dest).write(data, off, len, isAppend);
|
||||
return FileWriter.of(dest).write(data, off, len, isAppend);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -3023,7 +3023,7 @@ public class FileUtil extends PathUtil {
|
||||
* @since 5.5.6
|
||||
*/
|
||||
public static File writeFromStream(final InputStream in, final File dest, final boolean isCloseIn) throws IORuntimeException {
|
||||
return FileWriter.create(dest).writeFromStream(in, isCloseIn);
|
||||
return FileWriter.of(dest).writeFromStream(in, isCloseIn);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -3048,7 +3048,7 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static long writeToStream(final File file, final OutputStream out) throws IORuntimeException {
|
||||
return FileReader.create(file).writeToStream(out);
|
||||
return FileReader.of(file).writeToStream(out);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -3112,7 +3112,7 @@ public class FileUtil extends PathUtil {
|
||||
*/
|
||||
public static File convertLineSeparator(final File file, final Charset charset, final LineSeparator lineSeparator) {
|
||||
final List<String> lines = readLines(file, charset);
|
||||
return FileWriter.create(file, charset).writeLines(lines, lineSeparator, false);
|
||||
return FileWriter.of(file, charset).writeLines(lines, lineSeparator, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,7 +78,7 @@ public class FileAppender implements Serializable {
|
||||
this.capacity = capacity;
|
||||
this.list = new ArrayList<>(capacity);
|
||||
this.isNewLineMode = isNewLineMode;
|
||||
this.writer = FileWriter.create(destFile, charset);
|
||||
this.writer = FileWriter.of(destFile, charset);
|
||||
this.lock = ObjUtil.defaultIfNull(lock, LockUtil::getNoLock);
|
||||
}
|
||||
|
||||
|
@ -46,7 +46,7 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
|
||||
* @param destPath 目标文件路径(相对ClassPath路径或绝对路径)
|
||||
* @return this
|
||||
*/
|
||||
public static FileCopier create(final String srcPath, final String destPath) {
|
||||
public static FileCopier of(final String srcPath, final String destPath) {
|
||||
return new FileCopier(FileUtil.file(srcPath), FileUtil.file(destPath));
|
||||
}
|
||||
|
||||
@ -56,7 +56,7 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
|
||||
* @param dest 目标文件
|
||||
* @return this
|
||||
*/
|
||||
public static FileCopier create(final File src, final File dest) {
|
||||
public static FileCopier of(final File src, final File dest) {
|
||||
return new FileCopier(src, dest);
|
||||
}
|
||||
//-------------------------------------------------------------------------------------------------------- static method end
|
||||
|
@ -33,7 +33,7 @@ public class FileReader extends FileWrapper {
|
||||
* @param charset 编码,使用 {@link CharsetUtil}
|
||||
* @return FileReader
|
||||
*/
|
||||
public static FileReader create(final File file, final Charset charset){
|
||||
public static FileReader of(final File file, final Charset charset){
|
||||
return new FileReader(file, charset);
|
||||
}
|
||||
|
||||
@ -42,7 +42,7 @@ public class FileReader extends FileWrapper {
|
||||
* @param file 文件
|
||||
* @return FileReader
|
||||
*/
|
||||
public static FileReader create(final File file){
|
||||
public static FileReader of(final File file){
|
||||
return new FileReader(file);
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,7 @@ public class FileWriter extends FileWrapper {
|
||||
* @param charset 编码,使用 {@link CharsetUtil}
|
||||
* @return FileWriter
|
||||
*/
|
||||
public static FileWriter create(final File file, final Charset charset) {
|
||||
public static FileWriter of(final File file, final Charset charset) {
|
||||
return new FileWriter(file, charset);
|
||||
}
|
||||
|
||||
@ -44,7 +44,7 @@ public class FileWriter extends FileWrapper {
|
||||
* @param file 文件
|
||||
* @return FileWriter
|
||||
*/
|
||||
public static FileWriter create(final File file) {
|
||||
public static FileWriter of(final File file) {
|
||||
return new FileWriter(file);
|
||||
}
|
||||
|
||||
|
@ -78,8 +78,8 @@ public class WatchMonitor extends WatchServer {
|
||||
* @param events 监听的事件列表
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final URL url, final WatchEvent.Kind<?>... events) {
|
||||
return create(url, 0, events);
|
||||
public static WatchMonitor of(final URL url, final WatchEvent.Kind<?>... events) {
|
||||
return of(url, 0, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -90,8 +90,8 @@ public class WatchMonitor extends WatchServer {
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final URL url, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return create(URLUtil.toURI(url), maxDepth, events);
|
||||
public static WatchMonitor of(final URL url, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return of(URLUtil.toURI(url), maxDepth, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -101,8 +101,8 @@ public class WatchMonitor extends WatchServer {
|
||||
* @param events 监听的事件列表
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final URI uri, final WatchEvent.Kind<?>... events) {
|
||||
return create(uri, 0, events);
|
||||
public static WatchMonitor of(final URI uri, final WatchEvent.Kind<?>... events) {
|
||||
return of(uri, 0, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -113,8 +113,8 @@ public class WatchMonitor extends WatchServer {
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final URI uri, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return create(Paths.get(uri), maxDepth, events);
|
||||
public static WatchMonitor of(final URI uri, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return of(Paths.get(uri), maxDepth, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -124,8 +124,8 @@ public class WatchMonitor extends WatchServer {
|
||||
* @param events 监听的事件列表
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final File file, final WatchEvent.Kind<?>... events) {
|
||||
return create(file, 0, events);
|
||||
public static WatchMonitor of(final File file, final WatchEvent.Kind<?>... events) {
|
||||
return of(file, 0, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -136,8 +136,8 @@ public class WatchMonitor extends WatchServer {
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final File file, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return create(file.toPath(), maxDepth, events);
|
||||
public static WatchMonitor of(final File file, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return of(file.toPath(), maxDepth, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -147,8 +147,8 @@ public class WatchMonitor extends WatchServer {
|
||||
* @param events 监听的事件列表
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final String path, final WatchEvent.Kind<?>... events) {
|
||||
return create(path, 0, events);
|
||||
public static WatchMonitor of(final String path, final WatchEvent.Kind<?>... events) {
|
||||
return of(path, 0, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -159,8 +159,8 @@ public class WatchMonitor extends WatchServer {
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final String path, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return create(Paths.get(path), maxDepth, events);
|
||||
public static WatchMonitor of(final String path, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return of(Paths.get(path), maxDepth, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -170,8 +170,8 @@ public class WatchMonitor extends WatchServer {
|
||||
* @param events 监听事件列表
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final Path path, final WatchEvent.Kind<?>... events) {
|
||||
return create(path, 0, events);
|
||||
public static WatchMonitor of(final Path path, final WatchEvent.Kind<?>... events) {
|
||||
return of(path, 0, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -182,7 +182,7 @@ public class WatchMonitor extends WatchServer {
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final Path path, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
public static WatchMonitor of(final Path path, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return new WatchMonitor(path, maxDepth, events);
|
||||
}
|
||||
|
||||
@ -244,7 +244,7 @@ public class WatchMonitor extends WatchServer {
|
||||
* @return WatchMonitor
|
||||
*/
|
||||
public static WatchMonitor createAll(final Path path, final Watcher watcher) {
|
||||
final WatchMonitor watchMonitor = create(path, EVENTS_ALL);
|
||||
final WatchMonitor watchMonitor = of(path, EVENTS_ALL);
|
||||
watchMonitor.setWatcher(watcher);
|
||||
return watchMonitor;
|
||||
}
|
||||
|
@ -1,13 +1,18 @@
|
||||
package cn.hutool.core.io.watch;
|
||||
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.net.URLUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.file.*;
|
||||
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.net.URLUtil;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.WatchEvent;
|
||||
import java.nio.file.WatchKey;
|
||||
import java.nio.file.WatchService;
|
||||
import java.nio.file.Watchable;
|
||||
|
||||
/**
|
||||
* 监听工具类<br>
|
||||
@ -24,8 +29,8 @@ public class WatchUtil {
|
||||
* @param events 监听的事件列表
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final URL url, final WatchEvent.Kind<?>... events) {
|
||||
return create(url, 0, events);
|
||||
public static WatchMonitor of(final URL url, final WatchEvent.Kind<?>... events) {
|
||||
return of(url, 0, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -36,8 +41,8 @@ public class WatchUtil {
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final URL url, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return create(URLUtil.toURI(url), maxDepth, events);
|
||||
public static WatchMonitor of(final URL url, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return of(URLUtil.toURI(url), maxDepth, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -47,8 +52,8 @@ public class WatchUtil {
|
||||
* @param events 监听的事件列表
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final URI uri, final WatchEvent.Kind<?>... events) {
|
||||
return create(uri, 0, events);
|
||||
public static WatchMonitor of(final URI uri, final WatchEvent.Kind<?>... events) {
|
||||
return of(uri, 0, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -59,8 +64,8 @@ public class WatchUtil {
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final URI uri, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return create(Paths.get(uri), maxDepth, events);
|
||||
public static WatchMonitor of(final URI uri, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return of(Paths.get(uri), maxDepth, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -70,8 +75,8 @@ public class WatchUtil {
|
||||
* @param events 监听的事件列表
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final File file, final WatchEvent.Kind<?>... events) {
|
||||
return create(file, 0, events);
|
||||
public static WatchMonitor of(final File file, final WatchEvent.Kind<?>... events) {
|
||||
return of(file, 0, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -82,8 +87,8 @@ public class WatchUtil {
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final File file, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return create(file.toPath(), maxDepth, events);
|
||||
public static WatchMonitor of(final File file, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return of(file.toPath(), maxDepth, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,8 +98,8 @@ public class WatchUtil {
|
||||
* @param events 监听的事件列表
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final String path, final WatchEvent.Kind<?>... events) {
|
||||
return create(path, 0, events);
|
||||
public static WatchMonitor of(final String path, final WatchEvent.Kind<?>... events) {
|
||||
return of(path, 0, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -105,8 +110,8 @@ public class WatchUtil {
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final String path, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return create(Paths.get(path), maxDepth, events);
|
||||
public static WatchMonitor of(final String path, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return of(Paths.get(path), maxDepth, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -116,8 +121,8 @@ public class WatchUtil {
|
||||
* @param events 监听事件列表
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final Path path, final WatchEvent.Kind<?>... events) {
|
||||
return create(path, 0, events);
|
||||
public static WatchMonitor of(final Path path, final WatchEvent.Kind<?>... events) {
|
||||
return of(path, 0, events);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -128,7 +133,7 @@ public class WatchUtil {
|
||||
* @param maxDepth 当监听目录时,监听目录的最大深度,当设置值为1(或小于1)时,表示不递归监听子目录
|
||||
* @return 监听对象
|
||||
*/
|
||||
public static WatchMonitor create(final Path path, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
public static WatchMonitor of(final Path path, final int maxDepth, final WatchEvent.Kind<?>... events) {
|
||||
return new WatchMonitor(path, maxDepth, events);
|
||||
}
|
||||
|
||||
@ -199,7 +204,7 @@ public class WatchUtil {
|
||||
* @return {@link WatchMonitor}
|
||||
*/
|
||||
public static WatchMonitor createAll(final File file, final int maxDepth, final Watcher watcher) {
|
||||
return createAll(file.toPath(), 0, watcher);
|
||||
return createAll(file.toPath(), maxDepth, watcher);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -245,7 +250,7 @@ public class WatchUtil {
|
||||
* @return {@link WatchMonitor}
|
||||
*/
|
||||
public static WatchMonitor createAll(final Path path, final int maxDepth, final Watcher watcher) {
|
||||
final WatchMonitor watchMonitor = create(path, maxDepth, WatchMonitor.EVENTS_ALL);
|
||||
final WatchMonitor watchMonitor = of(path, maxDepth, WatchMonitor.EVENTS_ALL);
|
||||
watchMonitor.setWatcher(watcher);
|
||||
return watchMonitor;
|
||||
}
|
||||
@ -323,7 +328,7 @@ public class WatchUtil {
|
||||
* @since 4.5.2
|
||||
*/
|
||||
public static WatchMonitor createModify(final File file, final int maxDepth, final Watcher watcher) {
|
||||
return createModify(file.toPath(), 0, watcher);
|
||||
return createModify(file.toPath(), maxDepth, watcher);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -373,7 +378,7 @@ public class WatchUtil {
|
||||
* @since 4.5.2
|
||||
*/
|
||||
public static WatchMonitor createModify(final Path path, final int maxDepth, final Watcher watcher) {
|
||||
final WatchMonitor watchMonitor = create(path, maxDepth, WatchMonitor.ENTRY_MODIFY);
|
||||
final WatchMonitor watchMonitor = of(path, maxDepth, WatchMonitor.ENTRY_MODIFY);
|
||||
watchMonitor.setWatcher(watcher);
|
||||
return watchMonitor;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package cn.hutool.core.thread;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
import java.util.concurrent.RejectedExecutionHandler;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* 当任务队列过长时处于阻塞状态,直到添加到队列中
|
||||
@ -14,15 +15,42 @@ import java.util.concurrent.ThreadPoolExecutor;
|
||||
*/
|
||||
public class BlockPolicy implements RejectedExecutionHandler {
|
||||
|
||||
/**
|
||||
* 线程池关闭时,为避免任务丢失,留下处理方法
|
||||
* 如果需要由调用方来运行,可以{@code new BlockPolicy(Runnable::run)}
|
||||
*/
|
||||
private final Consumer<Runnable> handlerwhenshutdown;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public BlockPolicy() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param handlerwhenshutdown 线程池关闭后的执行策略
|
||||
*/
|
||||
public BlockPolicy(final Consumer<Runnable> handlerwhenshutdown) {
|
||||
this.handlerwhenshutdown = handlerwhenshutdown;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rejectedExecution(final Runnable r, final ThreadPoolExecutor e) {
|
||||
try {
|
||||
e.getQueue().put(r);
|
||||
} catch (final InterruptedException ex) {
|
||||
throw new RejectedExecutionException("Task " + r + " rejected from " + e);
|
||||
// 线程池未关闭时,阻塞等待
|
||||
if (false == e.isShutdown()) {
|
||||
try {
|
||||
e.getQueue().put(r);
|
||||
} catch (InterruptedException ex) {
|
||||
throw new RejectedExecutionException("Task " + r + " rejected from " + e);
|
||||
}
|
||||
} else if (null != handlerwhenshutdown) {
|
||||
// 当设置了关闭时候的处理
|
||||
handlerwhenshutdown.accept(r);
|
||||
}
|
||||
|
||||
// 线程池关闭后,丢弃任务
|
||||
}
|
||||
}
|
||||
|
@ -202,7 +202,7 @@ public class ExecutorBuilder implements Builder<ThreadPoolExecutor> {
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public static ExecutorBuilder create() {
|
||||
public static ExecutorBuilder of() {
|
||||
return new ExecutorBuilder();
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,11 @@
|
||||
package cn.hutool.core.thread;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
|
||||
/**
|
||||
* 全局公共线程池<br>
|
||||
* 此线程池是一个无限线程池,即加入的线程不等待任何线程,直接执行
|
||||
@ -30,7 +30,7 @@ public class GlobalThreadPool {
|
||||
if (null != executor) {
|
||||
executor.shutdownNow();
|
||||
}
|
||||
executor = ExecutorBuilder.create().useSynchronousQueue().build();
|
||||
executor = ExecutorBuilder.of().useSynchronousQueue().build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -35,7 +35,7 @@ public class ThreadUtil {
|
||||
* @return ExecutorService
|
||||
*/
|
||||
public static ExecutorService newExecutor(final int corePoolSize) {
|
||||
final ExecutorBuilder builder = ExecutorBuilder.create();
|
||||
final ExecutorBuilder builder = ExecutorBuilder.of();
|
||||
if (corePoolSize > 0) {
|
||||
builder.setCorePoolSize(corePoolSize);
|
||||
}
|
||||
@ -54,7 +54,7 @@ public class ThreadUtil {
|
||||
* @return ExecutorService
|
||||
*/
|
||||
public static ExecutorService newExecutor() {
|
||||
return ExecutorBuilder.create().useSynchronousQueue().build();
|
||||
return ExecutorBuilder.of().useSynchronousQueue().build();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,7 +69,7 @@ public class ThreadUtil {
|
||||
* @return ExecutorService
|
||||
*/
|
||||
public static ExecutorService newSingleExecutor() {
|
||||
return ExecutorBuilder.create()//
|
||||
return ExecutorBuilder.of()//
|
||||
.setCorePoolSize(1)//
|
||||
.setMaxPoolSize(1)//
|
||||
.setKeepAliveTime(0)//
|
||||
@ -85,7 +85,7 @@ public class ThreadUtil {
|
||||
* @return {@link ThreadPoolExecutor}
|
||||
*/
|
||||
public static ThreadPoolExecutor newExecutor(final int corePoolSize, final int maximumPoolSize) {
|
||||
return ExecutorBuilder.create()
|
||||
return ExecutorBuilder.of()
|
||||
.setCorePoolSize(corePoolSize)
|
||||
.setMaxPoolSize(maximumPoolSize)
|
||||
.build();
|
||||
@ -102,7 +102,7 @@ public class ThreadUtil {
|
||||
* @since 5.4.1
|
||||
*/
|
||||
public static ExecutorService newExecutor(final int corePoolSize, final int maximumPoolSize, final int maximumQueueSize) {
|
||||
return ExecutorBuilder.create()
|
||||
return ExecutorBuilder.of()
|
||||
.setCorePoolSize(corePoolSize)
|
||||
.setMaxPoolSize(maximumPoolSize)
|
||||
.setWorkQueue(new LinkedBlockingQueue<>(maximumQueueSize))
|
||||
@ -128,7 +128,7 @@ public class ThreadUtil {
|
||||
|
||||
// 最佳的线程数 = CPU可用核心数 / (1 - 阻塞系数)
|
||||
final int poolSize = (int) (Runtime.getRuntime().availableProcessors() / (1 - blockingCoefficient));
|
||||
return ExecutorBuilder.create().setCorePoolSize(poolSize).setMaxPoolSize(poolSize).setKeepAliveTime(0L).build();
|
||||
return ExecutorBuilder.of().setCorePoolSize(poolSize).setMaxPoolSize(poolSize).setKeepAliveTime(0L).build();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -190,7 +190,7 @@ public class ThreadUtil {
|
||||
final int maximumQueueSize,
|
||||
final String threadNamePrefix,
|
||||
final RejectedExecutionHandler handler) {
|
||||
return ExecutorBuilder.create()
|
||||
return ExecutorBuilder.of()
|
||||
.setCorePoolSize(nThreads).setMaxPoolSize(nThreads)
|
||||
.setWorkQueue(new LinkedBlockingQueue<>(maximumQueueSize))
|
||||
.setThreadFactory(createThreadFactory(threadNamePrefix))
|
||||
|
@ -14,7 +14,7 @@ public class DynaBeanTest {
|
||||
@Test
|
||||
public void beanTest() {
|
||||
final User user = new User();
|
||||
final DynaBean bean = DynaBean.create(user);
|
||||
final DynaBean bean = DynaBean.of(user);
|
||||
bean.set("name", "李华");
|
||||
bean.set("age", 12);
|
||||
|
||||
@ -38,7 +38,7 @@ public class DynaBeanTest {
|
||||
public void beanByStaticClazzConstructorTest() {
|
||||
final String name_before = "李华";
|
||||
final int age_before = 12;
|
||||
final DynaBean bean = DynaBean.create(User.class);
|
||||
final DynaBean bean = DynaBean.of(User.class);
|
||||
bean.set("name", name_before);
|
||||
bean.set("age", age_before);
|
||||
|
||||
|
@ -12,13 +12,13 @@ public class BeanCopierTest {
|
||||
public void beanToMapIgnoreNullTest() {
|
||||
final A a = new A();
|
||||
|
||||
HashMap<Object, Object> map = BeanCopier.create(a, new HashMap<>(), CopyOptions.of()).copy();
|
||||
HashMap<Object, Object> map = BeanCopier.of(a, new HashMap<>(), CopyOptions.of()).copy();
|
||||
Assert.assertEquals(1, map.size());
|
||||
Assert.assertTrue(map.containsKey("value"));
|
||||
Assert.assertNull(map.get("value"));
|
||||
|
||||
// 忽略null的情况下,空字段不写入map
|
||||
map = BeanCopier.create(a, new HashMap<>(), CopyOptions.of().ignoreNullValue()).copy();
|
||||
map = BeanCopier.of(a, new HashMap<>(), CopyOptions.of().ignoreNullValue()).copy();
|
||||
Assert.assertFalse(map.containsKey("value"));
|
||||
Assert.assertEquals(0, map.size());
|
||||
}
|
||||
@ -33,7 +33,7 @@ public class BeanCopierTest {
|
||||
final B b = new B();
|
||||
b.setValue("abc");
|
||||
|
||||
final BeanCopier<B> copier = BeanCopier.create(a, b, CopyOptions.of().setOverride(false));
|
||||
final BeanCopier<B> copier = BeanCopier.of(a, b, CopyOptions.of().setOverride(false));
|
||||
copier.copy();
|
||||
|
||||
Assert.assertEquals("abc", b.getValue());
|
||||
@ -49,7 +49,7 @@ public class BeanCopierTest {
|
||||
final B b = new B();
|
||||
b.setValue("abc");
|
||||
|
||||
final BeanCopier<B> copier = BeanCopier.create(a, b, CopyOptions.of());
|
||||
final BeanCopier<B> copier = BeanCopier.of(a, b, CopyOptions.of());
|
||||
copier.copy();
|
||||
|
||||
Assert.assertEquals("123", b.getValue());
|
||||
|
@ -6,7 +6,7 @@ import org.junit.Test;
|
||||
public class HashidsTest {
|
||||
@Test
|
||||
public void hexEncodeDecode() {
|
||||
final Hashids hashids = Hashids.create("my awesome salt".toCharArray());
|
||||
final Hashids hashids = Hashids.of("my awesome salt".toCharArray());
|
||||
final String encoded1 = hashids.encodeFromHex("507f1f77bcf86cd799439011");
|
||||
final String encoded2 = hashids.encodeFromHex("0x507f1f77bcf86cd799439011");
|
||||
final String encoded3 = hashids.encodeFromHex("0X507f1f77bcf86cd799439011");
|
||||
|
@ -69,9 +69,9 @@ public class DateBetweenTest {
|
||||
DateUtil.parse("2020-11-21"),
|
||||
DateUtil.parse("2020-11-23"), false);
|
||||
|
||||
final long betweenWeek2 = LocalDateTimeUtil.between(
|
||||
LocalDateTimeUtil.parse("2020-11-21", "yyy-MM-dd"),
|
||||
LocalDateTimeUtil.parse("2020-11-23", "yyy-MM-dd"),
|
||||
final long betweenWeek2 = TimeUtil.between(
|
||||
TimeUtil.parse("2020-11-21", "yyy-MM-dd"),
|
||||
TimeUtil.parse("2020-11-23", "yyy-MM-dd"),
|
||||
ChronoUnit.WEEKS);
|
||||
Assert.assertEquals(betweenWeek, betweenWeek2);
|
||||
}
|
||||
|
@ -972,7 +972,7 @@ public class DateUtilTest {
|
||||
String format = DateUtil.format(DateUtil.parse("2021-07-14 10:05:38"), DatePattern.NORM_DATETIME_FORMATTER);
|
||||
Assert.assertEquals("2021-07-14 10:05:38", format);
|
||||
|
||||
format = DateUtil.format(LocalDateTimeUtil.parse("2021-07-14T10:05:38"),
|
||||
format = DateUtil.format(TimeUtil.parse("2021-07-14T10:05:38"),
|
||||
"yyyy-MM-dd HH:mm:ss");
|
||||
Assert.assertEquals("2021-07-14 10:05:38", format);
|
||||
}
|
||||
|
@ -1,239 +0,0 @@
|
||||
package cn.hutool.core.date;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
|
||||
public class LocalDateTimeUtilTest {
|
||||
|
||||
@Test
|
||||
public void nowTest() {
|
||||
Assert.assertNotNull(LocalDateTimeUtil.now());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofTest() {
|
||||
final String dateStr = "2020-01-23T12:23:56";
|
||||
final DateTime dt = DateUtil.parse(dateStr);
|
||||
|
||||
LocalDateTime of = LocalDateTimeUtil.of(dt);
|
||||
Assert.assertNotNull(of);
|
||||
Assert.assertEquals(dateStr, of.toString());
|
||||
|
||||
of = LocalDateTimeUtil.ofUTC(dt.getTime());
|
||||
Assert.assertEquals(dateStr, of.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseOffsetTest() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2021-07-30T16:27:27+08:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
||||
Assert.assertEquals("2021-07-30T16:27:27", localDateTime.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56", DateTimeFormatter.ISO_DATE_TIME);
|
||||
Assert.assertEquals("2020-01-23T12:23:56", localDateTime.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest2() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23", DatePattern.NORM_DATE_PATTERN);
|
||||
Assert.assertEquals("2020-01-23T00:00", localDateTime.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest3() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("12:23:56", DatePattern.NORM_TIME_PATTERN);
|
||||
Assert.assertEquals("12:23:56", localDateTime.toLocalTime().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest4() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56");
|
||||
Assert.assertEquals("2020-01-23T12:23:56", localDateTime.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest5() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("19940121183604", "yyyyMMddHHmmss");
|
||||
Assert.assertEquals("1994-01-21T18:36:04", localDateTime.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest6() {
|
||||
LocalDateTime localDateTime = LocalDateTimeUtil.parse("19940121183604682", "yyyyMMddHHmmssSSS");
|
||||
Assert.assertEquals("1994-01-21T18:36:04.682", localDateTime.toString());
|
||||
|
||||
localDateTime = LocalDateTimeUtil.parse("1994012118360468", "yyyyMMddHHmmssSS");
|
||||
Assert.assertEquals("1994-01-21T18:36:04.680", localDateTime.toString());
|
||||
|
||||
localDateTime = LocalDateTimeUtil.parse("199401211836046", "yyyyMMddHHmmssS");
|
||||
Assert.assertEquals("1994-01-21T18:36:04.600", localDateTime.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseDateTest() {
|
||||
LocalDate localDate = LocalDateTimeUtil.parseDate("2020-01-23");
|
||||
Assert.assertEquals("2020-01-23", localDate.toString());
|
||||
|
||||
localDate = LocalDateTimeUtil.parseDate("2020-01-23T12:23:56", DateTimeFormatter.ISO_DATE_TIME);
|
||||
Assert.assertEquals("2020-01-23", localDate.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseSingleMonthAndDayTest() {
|
||||
final LocalDate localDate = LocalDateTimeUtil.parseDate("2020-1-1", "yyyy-M-d");
|
||||
Assert.assertEquals("2020-01-01", localDate.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatTest() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56");
|
||||
String format = LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_DATETIME_PATTERN);
|
||||
Assert.assertEquals("2020-01-23 12:23:56", format);
|
||||
|
||||
format = LocalDateTimeUtil.formatNormal(localDateTime);
|
||||
Assert.assertEquals("2020-01-23 12:23:56", format);
|
||||
|
||||
format = LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_DATE_PATTERN);
|
||||
Assert.assertEquals("2020-01-23", format);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatLocalDateTest() {
|
||||
final LocalDate date = LocalDate.parse("2020-01-23");
|
||||
String format = LocalDateTimeUtil.format(date, DatePattern.NORM_DATE_PATTERN);
|
||||
Assert.assertEquals("2020-01-23", format);
|
||||
|
||||
format = LocalDateTimeUtil.formatNormal(date);
|
||||
Assert.assertEquals("2020-01-23", format);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void offset() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56");
|
||||
LocalDateTime offset = LocalDateTimeUtil.offset(localDateTime, 1, ChronoUnit.DAYS);
|
||||
// 非同一对象
|
||||
Assert.assertNotSame(localDateTime, offset);
|
||||
|
||||
Assert.assertEquals("2020-01-24T12:23:56", offset.toString());
|
||||
|
||||
offset = LocalDateTimeUtil.offset(localDateTime, -1, ChronoUnit.DAYS);
|
||||
Assert.assertEquals("2020-01-22T12:23:56", offset.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void between() {
|
||||
final Duration between = LocalDateTimeUtil.between(
|
||||
LocalDateTimeUtil.parse("2019-02-02T00:00:00"),
|
||||
LocalDateTimeUtil.parse("2020-02-02T00:00:00"));
|
||||
Assert.assertEquals(365, between.toDays());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beginOfDayTest() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56");
|
||||
final LocalDateTime beginOfDay = LocalDateTimeUtil.beginOfDay(localDateTime);
|
||||
Assert.assertEquals("2020-01-23T00:00", beginOfDay.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void endOfDayTest() {
|
||||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56");
|
||||
|
||||
LocalDateTime endOfDay = LocalDateTimeUtil.endOfDay(localDateTime);
|
||||
Assert.assertEquals("2020-01-23T23:59:59.999999999", endOfDay.toString());
|
||||
|
||||
endOfDay = LocalDateTimeUtil.endOfDay(localDateTime, true);
|
||||
Assert.assertEquals("2020-01-23T23:59:59", endOfDay.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dayOfWeekTest() {
|
||||
final Week one = LocalDateTimeUtil.dayOfWeek(LocalDate.of(2021, 9, 20));
|
||||
Assert.assertEquals(Week.MONDAY, one);
|
||||
|
||||
final Week two = LocalDateTimeUtil.dayOfWeek(LocalDate.of(2021, 9, 21));
|
||||
Assert.assertEquals(Week.TUESDAY, two);
|
||||
|
||||
final Week three = LocalDateTimeUtil.dayOfWeek(LocalDate.of(2021, 9, 22));
|
||||
Assert.assertEquals(Week.WEDNESDAY, three);
|
||||
|
||||
final Week four = LocalDateTimeUtil.dayOfWeek(LocalDate.of(2021, 9, 23));
|
||||
Assert.assertEquals(Week.THURSDAY, four);
|
||||
|
||||
final Week five = LocalDateTimeUtil.dayOfWeek(LocalDate.of(2021, 9, 24));
|
||||
Assert.assertEquals(Week.FRIDAY, five);
|
||||
|
||||
final Week six = LocalDateTimeUtil.dayOfWeek(LocalDate.of(2021, 9, 25));
|
||||
Assert.assertEquals(Week.SATURDAY, six);
|
||||
|
||||
final Week seven = LocalDateTimeUtil.dayOfWeek(LocalDate.of(2021, 9, 26));
|
||||
Assert.assertEquals(Week.SUNDAY, seven);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isOverlapTest(){
|
||||
final LocalDateTime oneStartTime = LocalDateTime.of(2022, 1, 1, 10, 10, 10);
|
||||
final LocalDateTime oneEndTime = LocalDateTime.of(2022, 1, 1, 11, 10, 10);
|
||||
|
||||
final LocalDateTime oneStartTime2 = LocalDateTime.of(2022, 1, 1, 11, 20, 10);
|
||||
final LocalDateTime oneEndTime2 = LocalDateTime.of(2022, 1, 1, 11, 30, 10);
|
||||
|
||||
final LocalDateTime oneStartTime3 = LocalDateTime.of(2022, 1, 1, 11, 40, 10);
|
||||
final LocalDateTime oneEndTime3 = LocalDateTime.of(2022, 1, 1, 11, 50, 10);
|
||||
|
||||
//真实请假数据
|
||||
final LocalDateTime realStartTime = LocalDateTime.of(2022, 1, 1, 11, 49, 10);
|
||||
final LocalDateTime realEndTime = LocalDateTime.of(2022, 1, 1, 12, 0, 10);
|
||||
|
||||
final LocalDateTime realStartTime1 = DateUtil.parseLocalDateTime("2022-03-01 08:00:00");
|
||||
final LocalDateTime realEndTime1 = DateUtil.parseLocalDateTime("2022-03-01 10:00:00");
|
||||
|
||||
final LocalDateTime startTime = DateUtil.parseLocalDateTime("2022-03-23 05:00:00");
|
||||
final LocalDateTime endTime = DateUtil.parseLocalDateTime("2022-03-23 13:00:00");
|
||||
|
||||
Assert.assertFalse(LocalDateTimeUtil.isOverlap(oneStartTime,oneEndTime,realStartTime,realEndTime));
|
||||
Assert.assertFalse(LocalDateTimeUtil.isOverlap(oneStartTime2,oneEndTime2,realStartTime,realEndTime));
|
||||
Assert.assertTrue(LocalDateTimeUtil.isOverlap(oneStartTime3,oneEndTime3,realStartTime,realEndTime));
|
||||
|
||||
Assert.assertFalse(LocalDateTimeUtil.isOverlap(realStartTime1,realEndTime1,startTime,endTime));
|
||||
Assert.assertFalse(LocalDateTimeUtil.isOverlap(startTime,endTime,realStartTime1,realEndTime1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void weekOfYearTest(){
|
||||
final LocalDate date1 = LocalDate.of(2021, 12, 31);
|
||||
final int weekOfYear1 = LocalDateTimeUtil.weekOfYear(date1);
|
||||
Assert.assertEquals(52, weekOfYear1);
|
||||
|
||||
final int weekOfYear2 = LocalDateTimeUtil.weekOfYear(date1.atStartOfDay());
|
||||
Assert.assertEquals(52, weekOfYear2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void weekOfYearTest2(){
|
||||
final LocalDate date1 = LocalDate.of(2022, 1, 31);
|
||||
final int weekOfYear1 = LocalDateTimeUtil.weekOfYear(date1);
|
||||
Assert.assertEquals(5, weekOfYear1);
|
||||
|
||||
final int weekOfYear2 = LocalDateTimeUtil.weekOfYear(date1.atStartOfDay());
|
||||
Assert.assertEquals(5, weekOfYear2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofTest2(){
|
||||
final Instant instant = DateUtil.parse("2022-02-22").toInstant();
|
||||
final LocalDateTime of = LocalDateTimeUtil.of((TemporalAccessor) instant);
|
||||
Console.log(of);
|
||||
}
|
||||
}
|
240
hutool-core/src/test/java/cn/hutool/core/date/TimeUtilTest.java
Normal file
240
hutool-core/src/test/java/cn/hutool/core/date/TimeUtilTest.java
Normal file
@ -0,0 +1,240 @@
|
||||
package cn.hutool.core.date;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Objects;
|
||||
|
||||
public class TimeUtilTest {
|
||||
|
||||
@Test
|
||||
public void nowTest() {
|
||||
Assert.assertNotNull(TimeUtil.now());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofTest() {
|
||||
final String dateStr = "2020-01-23T12:23:56";
|
||||
final DateTime dt = DateUtil.parse(dateStr);
|
||||
|
||||
LocalDateTime of = TimeUtil.of(dt);
|
||||
Assert.assertNotNull(of);
|
||||
Assert.assertEquals(dateStr, of.toString());
|
||||
|
||||
of = TimeUtil.ofUTC(dt.getTime());
|
||||
Assert.assertEquals(dateStr, of.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseOffsetTest() {
|
||||
final LocalDateTime localDateTime = TimeUtil.parse("2021-07-30T16:27:27+08:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
||||
Assert.assertEquals("2021-07-30T16:27:27", Objects.requireNonNull(localDateTime).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest() {
|
||||
final LocalDateTime localDateTime = TimeUtil.parse("2020-01-23T12:23:56", DateTimeFormatter.ISO_DATE_TIME);
|
||||
Assert.assertEquals("2020-01-23T12:23:56", Objects.requireNonNull(localDateTime).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest2() {
|
||||
final LocalDateTime localDateTime = TimeUtil.parse("2020-01-23", DatePattern.NORM_DATE_PATTERN);
|
||||
Assert.assertEquals("2020-01-23T00:00", Objects.requireNonNull(localDateTime).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest3() {
|
||||
final LocalDateTime localDateTime = TimeUtil.parse("12:23:56", DatePattern.NORM_TIME_PATTERN);
|
||||
Assert.assertEquals("12:23:56", Objects.requireNonNull(localDateTime).toLocalTime().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest4() {
|
||||
final LocalDateTime localDateTime = TimeUtil.parse("2020-01-23T12:23:56");
|
||||
Assert.assertEquals("2020-01-23T12:23:56", localDateTime.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest5() {
|
||||
final LocalDateTime localDateTime = TimeUtil.parse("19940121183604", "yyyyMMddHHmmss");
|
||||
Assert.assertEquals("1994-01-21T18:36:04", Objects.requireNonNull(localDateTime).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest6() {
|
||||
LocalDateTime localDateTime = TimeUtil.parse("19940121183604682", "yyyyMMddHHmmssSSS");
|
||||
Assert.assertEquals("1994-01-21T18:36:04.682", Objects.requireNonNull(localDateTime).toString());
|
||||
|
||||
localDateTime = TimeUtil.parse("1994012118360468", "yyyyMMddHHmmssSS");
|
||||
Assert.assertEquals("1994-01-21T18:36:04.680", Objects.requireNonNull(localDateTime).toString());
|
||||
|
||||
localDateTime = TimeUtil.parse("199401211836046", "yyyyMMddHHmmssS");
|
||||
Assert.assertEquals("1994-01-21T18:36:04.600", Objects.requireNonNull(localDateTime).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseDateTest() {
|
||||
LocalDate localDate = TimeUtil.parseDate("2020-01-23");
|
||||
Assert.assertEquals("2020-01-23", localDate.toString());
|
||||
|
||||
localDate = TimeUtil.parseDate("2020-01-23T12:23:56", DateTimeFormatter.ISO_DATE_TIME);
|
||||
Assert.assertEquals("2020-01-23", localDate.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseSingleMonthAndDayTest() {
|
||||
final LocalDate localDate = TimeUtil.parseDate("2020-1-1", "yyyy-M-d");
|
||||
Assert.assertEquals("2020-01-01", localDate.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatTest() {
|
||||
final LocalDateTime localDateTime = TimeUtil.parse("2020-01-23T12:23:56");
|
||||
String format = TimeUtil.format(localDateTime, DatePattern.NORM_DATETIME_PATTERN);
|
||||
Assert.assertEquals("2020-01-23 12:23:56", format);
|
||||
|
||||
format = TimeUtil.formatNormal(localDateTime);
|
||||
Assert.assertEquals("2020-01-23 12:23:56", format);
|
||||
|
||||
format = TimeUtil.format(localDateTime, DatePattern.NORM_DATE_PATTERN);
|
||||
Assert.assertEquals("2020-01-23", format);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void formatLocalDateTest() {
|
||||
final LocalDate date = LocalDate.parse("2020-01-23");
|
||||
String format = TimeUtil.format(date, DatePattern.NORM_DATE_PATTERN);
|
||||
Assert.assertEquals("2020-01-23", format);
|
||||
|
||||
format = TimeUtil.formatNormal(date);
|
||||
Assert.assertEquals("2020-01-23", format);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void offset() {
|
||||
final LocalDateTime localDateTime = TimeUtil.parse("2020-01-23T12:23:56");
|
||||
LocalDateTime offset = TimeUtil.offset(localDateTime, 1, ChronoUnit.DAYS);
|
||||
// 非同一对象
|
||||
Assert.assertNotSame(localDateTime, offset);
|
||||
|
||||
Assert.assertEquals("2020-01-24T12:23:56", offset.toString());
|
||||
|
||||
offset = TimeUtil.offset(localDateTime, -1, ChronoUnit.DAYS);
|
||||
Assert.assertEquals("2020-01-22T12:23:56", offset.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void between() {
|
||||
final Duration between = TimeUtil.between(
|
||||
TimeUtil.parse("2019-02-02T00:00:00"),
|
||||
TimeUtil.parse("2020-02-02T00:00:00"));
|
||||
Assert.assertEquals(365, between.toDays());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beginOfDayTest() {
|
||||
final LocalDateTime localDateTime = TimeUtil.parse("2020-01-23T12:23:56");
|
||||
final LocalDateTime beginOfDay = TimeUtil.beginOfDay(localDateTime);
|
||||
Assert.assertEquals("2020-01-23T00:00", beginOfDay.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void endOfDayTest() {
|
||||
final LocalDateTime localDateTime = TimeUtil.parse("2020-01-23T12:23:56");
|
||||
|
||||
LocalDateTime endOfDay = TimeUtil.endOfDay(localDateTime);
|
||||
Assert.assertEquals("2020-01-23T23:59:59.999999999", endOfDay.toString());
|
||||
|
||||
endOfDay = TimeUtil.endOfDay(localDateTime, true);
|
||||
Assert.assertEquals("2020-01-23T23:59:59", endOfDay.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dayOfWeekTest() {
|
||||
final Week one = TimeUtil.dayOfWeek(LocalDate.of(2021, 9, 20));
|
||||
Assert.assertEquals(Week.MONDAY, one);
|
||||
|
||||
final Week two = TimeUtil.dayOfWeek(LocalDate.of(2021, 9, 21));
|
||||
Assert.assertEquals(Week.TUESDAY, two);
|
||||
|
||||
final Week three = TimeUtil.dayOfWeek(LocalDate.of(2021, 9, 22));
|
||||
Assert.assertEquals(Week.WEDNESDAY, three);
|
||||
|
||||
final Week four = TimeUtil.dayOfWeek(LocalDate.of(2021, 9, 23));
|
||||
Assert.assertEquals(Week.THURSDAY, four);
|
||||
|
||||
final Week five = TimeUtil.dayOfWeek(LocalDate.of(2021, 9, 24));
|
||||
Assert.assertEquals(Week.FRIDAY, five);
|
||||
|
||||
final Week six = TimeUtil.dayOfWeek(LocalDate.of(2021, 9, 25));
|
||||
Assert.assertEquals(Week.SATURDAY, six);
|
||||
|
||||
final Week seven = TimeUtil.dayOfWeek(LocalDate.of(2021, 9, 26));
|
||||
Assert.assertEquals(Week.SUNDAY, seven);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isOverlapTest(){
|
||||
final LocalDateTime oneStartTime = LocalDateTime.of(2022, 1, 1, 10, 10, 10);
|
||||
final LocalDateTime oneEndTime = LocalDateTime.of(2022, 1, 1, 11, 10, 10);
|
||||
|
||||
final LocalDateTime oneStartTime2 = LocalDateTime.of(2022, 1, 1, 11, 20, 10);
|
||||
final LocalDateTime oneEndTime2 = LocalDateTime.of(2022, 1, 1, 11, 30, 10);
|
||||
|
||||
final LocalDateTime oneStartTime3 = LocalDateTime.of(2022, 1, 1, 11, 40, 10);
|
||||
final LocalDateTime oneEndTime3 = LocalDateTime.of(2022, 1, 1, 11, 50, 10);
|
||||
|
||||
//真实请假数据
|
||||
final LocalDateTime realStartTime = LocalDateTime.of(2022, 1, 1, 11, 49, 10);
|
||||
final LocalDateTime realEndTime = LocalDateTime.of(2022, 1, 1, 12, 0, 10);
|
||||
|
||||
final LocalDateTime realStartTime1 = DateUtil.parseLocalDateTime("2022-03-01 08:00:00");
|
||||
final LocalDateTime realEndTime1 = DateUtil.parseLocalDateTime("2022-03-01 10:00:00");
|
||||
|
||||
final LocalDateTime startTime = DateUtil.parseLocalDateTime("2022-03-23 05:00:00");
|
||||
final LocalDateTime endTime = DateUtil.parseLocalDateTime("2022-03-23 13:00:00");
|
||||
|
||||
Assert.assertFalse(TimeUtil.isOverlap(oneStartTime,oneEndTime,realStartTime,realEndTime));
|
||||
Assert.assertFalse(TimeUtil.isOverlap(oneStartTime2,oneEndTime2,realStartTime,realEndTime));
|
||||
Assert.assertTrue(TimeUtil.isOverlap(oneStartTime3,oneEndTime3,realStartTime,realEndTime));
|
||||
|
||||
Assert.assertFalse(TimeUtil.isOverlap(realStartTime1,realEndTime1,startTime,endTime));
|
||||
Assert.assertFalse(TimeUtil.isOverlap(startTime,endTime,realStartTime1,realEndTime1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void weekOfYearTest(){
|
||||
final LocalDate date1 = LocalDate.of(2021, 12, 31);
|
||||
final int weekOfYear1 = TimeUtil.weekOfYear(date1);
|
||||
Assert.assertEquals(52, weekOfYear1);
|
||||
|
||||
final int weekOfYear2 = TimeUtil.weekOfYear(date1.atStartOfDay());
|
||||
Assert.assertEquals(52, weekOfYear2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void weekOfYearTest2(){
|
||||
final LocalDate date1 = LocalDate.of(2022, 1, 31);
|
||||
final int weekOfYear1 = TimeUtil.weekOfYear(date1);
|
||||
Assert.assertEquals(5, weekOfYear1);
|
||||
|
||||
final int weekOfYear2 = TimeUtil.weekOfYear(date1.atStartOfDay());
|
||||
Assert.assertEquals(5, weekOfYear2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofTest2(){
|
||||
final Instant instant = Objects.requireNonNull(DateUtil.parse("2022-02-22")).toInstant();
|
||||
final LocalDateTime of = TimeUtil.of((TemporalAccessor) instant);
|
||||
Console.log(of);
|
||||
}
|
||||
}
|
@ -18,7 +18,7 @@ public class FileCopierTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void dirCopyTest() {
|
||||
final FileCopier copier = FileCopier.create("D:\\Java", "e:/eclipse/eclipse2.zip");
|
||||
final FileCopier copier = FileCopier.of("D:\\Java", "e:/eclipse/eclipse2.zip");
|
||||
copier.copy();
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@ public class FileCopierTest {
|
||||
@Ignore
|
||||
public void dirCopyTest2() {
|
||||
//测试带.的文件夹复制
|
||||
final FileCopier copier = FileCopier.create("D:\\workspace\\java\\.metadata", "D:\\workspace\\java\\.metadata\\temp");
|
||||
final FileCopier copier = FileCopier.of("D:\\workspace\\java\\.metadata", "D:\\workspace\\java\\.metadata\\temp");
|
||||
copier.copy();
|
||||
|
||||
FileUtil.copy("D:\\workspace\\java\\looly\\hutool\\.git", "D:\\workspace\\java\\temp", true);
|
||||
@ -35,14 +35,14 @@ public class FileCopierTest {
|
||||
@Test(expected = IORuntimeException.class)
|
||||
public void dirCopySubTest() {
|
||||
//测试父目录复制到子目录报错
|
||||
final FileCopier copier = FileCopier.create("D:\\workspace\\java\\.metadata", "D:\\workspace\\java\\.metadata\\temp");
|
||||
final FileCopier copier = FileCopier.of("D:\\workspace\\java\\.metadata", "D:\\workspace\\java\\.metadata\\temp");
|
||||
copier.copy();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void copyFileToDirTest() {
|
||||
final FileCopier copier = FileCopier.create("d:/GReen_Soft/XshellXftpPortable.zip", "c:/hp/");
|
||||
final FileCopier copier = FileCopier.of("d:/GReen_Soft/XshellXftpPortable.zip", "c:/hp/");
|
||||
copier.copy();
|
||||
}
|
||||
|
||||
@ -51,7 +51,7 @@ public class FileCopierTest {
|
||||
public void copyFileByRelativePath(){
|
||||
// https://github.com/dromara/hutool/pull/2188
|
||||
// 当复制的目标文件位置是相对路径的时候可以通过
|
||||
final FileCopier copier = FileCopier.create(new File("pom.xml"),new File("aaa.txt"));
|
||||
final FileCopier copier = FileCopier.of(new File("pom.xml"),new File("aaa.txt"));
|
||||
copier.copy();
|
||||
final boolean delete = new File("aaa.txt").delete();
|
||||
Assert.assertTrue(delete);
|
||||
|
@ -410,7 +410,7 @@ public class Scheduler implements Serializable {
|
||||
|
||||
if(null == this.threadExecutor){
|
||||
// 无界线程池,确保每一个需要执行的线程都可以及时运行,同时复用已有线程避免线程重复创建
|
||||
this.threadExecutor = ExecutorBuilder.create().useSynchronousQueue().setThreadFactory(//
|
||||
this.threadExecutor = ExecutorBuilder.of().useSynchronousQueue().setThreadFactory(//
|
||||
ThreadFactoryBuilder.create().setNamePrefix("hutool-cron-").setDaemon(this.daemon).build()//
|
||||
).build();
|
||||
}
|
||||
|
@ -225,7 +225,7 @@ public class JSONUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static JSON readJSON(final File file, final Charset charset) throws IORuntimeException {
|
||||
return parse(FileReader.create(file, charset).readString());
|
||||
return parse(FileReader.of(file, charset).readString());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -237,7 +237,7 @@ public class JSONUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static JSONObject readJSONObject(final File file, final Charset charset) throws IORuntimeException {
|
||||
return parseObj(FileReader.create(file, charset).readString());
|
||||
return parseObj(FileReader.of(file, charset).readString());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -249,7 +249,7 @@ public class JSONUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static JSONArray readJSONArray(final File file, final Charset charset) throws IORuntimeException {
|
||||
return parseArray(FileReader.create(file, charset).readString());
|
||||
return parseArray(FileReader.of(file, charset).readString());
|
||||
}
|
||||
// -------------------------------------------------------------------- Read end
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||
import cn.hutool.core.date.TimeUtil;
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@ -20,12 +20,12 @@ public class Issue644Test {
|
||||
final JSONObject jsonObject = JSONUtil.parseObj(beanWithDate);
|
||||
|
||||
BeanWithDate beanWithDate2 = JSONUtil.toBean(jsonObject, BeanWithDate.class);
|
||||
Assert.assertEquals(LocalDateTimeUtil.formatNormal(beanWithDate.getDate()),
|
||||
LocalDateTimeUtil.formatNormal(beanWithDate2.getDate()));
|
||||
Assert.assertEquals(TimeUtil.formatNormal(beanWithDate.getDate()),
|
||||
TimeUtil.formatNormal(beanWithDate2.getDate()));
|
||||
|
||||
beanWithDate2 = JSONUtil.toBean(jsonObject.toString(), BeanWithDate.class);
|
||||
Assert.assertEquals(LocalDateTimeUtil.formatNormal(beanWithDate.getDate()),
|
||||
LocalDateTimeUtil.formatNormal(beanWithDate2.getDate()));
|
||||
Assert.assertEquals(TimeUtil.formatNormal(beanWithDate.getDate()),
|
||||
TimeUtil.formatNormal(beanWithDate2.getDate()));
|
||||
}
|
||||
|
||||
@Data
|
||||
|
Loading…
x
Reference in New Issue
Block a user