Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
839128 committed Sep 12, 2022
1 parent 32ae351 commit 5b62daa
Show file tree
Hide file tree
Showing 22 changed files with 242 additions and 37 deletions.
Empty file added CHANGELOG.md
Empty file.
Expand Up @@ -181,7 +181,7 @@ public ZipWriter add(Resource... resources) throws InternalException {
* @throws InternalException IO异常
*/
public ZipWriter add(String path, InputStream in) throws InternalException {
path = StringKit.nullToEmpty(path);
path = StringKit.emptyIfNull(path);
if (null == in) {
// 空目录需要检查路径规范性,目录以"/"结尾
path = StringKit.addSuffixIfNot(path, Symbol.SLASH);
Expand Down
Expand Up @@ -51,6 +51,7 @@ public T convert(Object value, T defaultValue) {
throw new NullPointerException(StringKit.format("[type] and [defaultValue] are both null for Converter [{}], we can not know what type to convert !", this.getClass().getName()));
}
if (null == targetType) {
// 目标类型不确定时使用默认值的类型
targetType = (Class<T>) defaultValue.getClass();
}
if (null == value) {
Expand All @@ -62,17 +63,11 @@ public T convert(Object value, T defaultValue) {
// 除Map外,已经是目标类型,不需要转换(Map类型涉及参数类型,需要单独转换)
return targetType.cast(value);
}
T result;
try {
result = convertInternal(value);
} catch (RuntimeException e) {
return defaultValue;
}
final T result = convertInternal(value);
return ((null == result) ? defaultValue : result);
} else {
throw new IllegalArgumentException(
StringKit.format("Default value [{}]({}) is not the instance of [{}]", defaultValue, defaultValue.getClass(), targetType));

}
}

Expand Down
Expand Up @@ -26,6 +26,7 @@
package org.aoju.bus.core.convert;

import org.aoju.bus.core.date.DateTime;
import org.aoju.bus.core.exception.ConvertException;
import org.aoju.bus.core.toolkit.DateKit;
import org.aoju.bus.core.toolkit.ObjectKit;
import org.aoju.bus.core.toolkit.StringKit;
Expand All @@ -37,6 +38,7 @@
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
Expand Down Expand Up @@ -126,6 +128,17 @@ protected TemporalAccessor convertInternal(Object value) {
} else if (value instanceof Calendar) {
final Calendar calendar = (Calendar) value;
return parseFromInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId());
} else if (value instanceof Map) {
final Map<?, ?> map = (Map<?, ?>) value;
if (LocalDate.class.equals(this.targetType)) {
return LocalDate.of(Convert.toInt(map.get("year")), Convert.toInt(map.get("month")), Convert.toInt(map.get("day")));
} else if (LocalDateTime.class.equals(this.targetType)) {
return LocalDateTime.of(Convert.toInt(map.get("year")), Convert.toInt(map.get("month")), Convert.toInt(map.get("day")),
Convert.toInt(map.get("hour")), Convert.toInt(map.get("minute")), Convert.toInt(map.get("second")), Convert.toInt(map.get("second")));
} else if (LocalTime.class.equals(this.targetType)) {
return LocalTime.of(Convert.toInt(map.get("hour")), Convert.toInt(map.get("minute")), Convert.toInt(map.get("second")), Convert.toInt(map.get("nano")));
}
throw new ConvertException("Unsupported type: [{}] from map: [{}]", this.targetType, map);
} else {
return parseFromCharSequence(convertString(value));
}
Expand Down
66 changes: 63 additions & 3 deletions bus-core/src/main/java/org/aoju/bus/core/date/Almanac.java
Expand Up @@ -4929,9 +4929,20 @@ public static boolean isPM(Calendar calendar) {
return Calendar.PM == calendar.get(Calendar.AM_PM);
}

/**
* 判断当前时间(默认时区)是否在指定范围内
* 起始日期和结束日期可以互换
*
* @param beginDate 起始日期
* @param endDate 结束日期
* @return 是否在范围内
*/
public static boolean isIn(Date beginDate, Date endDate) {
return isIn(new Date(), beginDate, endDate);
}

/**
* 判定在指定检查时间是否过期
* 当前日期是否在日期指定范围内
* 起始日期和结束日期可以互换
*
* @param date 被检查的日期
Expand All @@ -4948,7 +4959,19 @@ public static boolean isIn(Date date, Date beginDate, Date endDate) {
}

/**
* 当前日期是否在日期指定范围内
* 判断当前时间(默认时区)是否在指定范围内
* 起始时间和结束时间可以互换
*
* @param beginDate 起始时间(包含)
* @param endDate 结束时间(包含)
* @return 是否在范围内
*/
public static boolean isIn(ChronoLocalDateTime<?> beginDate, ChronoLocalDateTime<?> endDate) {
return isIn(LocalDateTime.now(), beginDate, endDate);
}

/**
* 判断指定时间是否在指定范围内
* 起始日期和结束日期可以互换
*
* @param date 被检查的日期
Expand All @@ -4957,11 +4980,48 @@ public static boolean isIn(Date date, Date beginDate, Date endDate) {
* @return 是否在范围内
*/
public static boolean isIn(TemporalAccessor date, TemporalAccessor beginDate, TemporalAccessor endDate) {
return isIn(date, beginDate, endDate, true, true);
}

/**
* 判断指定时间是否在指定范围内
* 起始日期和结束日期可以互换
* 通过includeBegin, includeEnd参数控制日期范围区间是否为开区间,
* 例如:传入参数:includeBegin=true, includeEnd=false,
* 则本方法会判断 date (beginDate, endDate) 是否成立
*
* @param date 被检查的日期
* @param beginDate 起始日期
* @param endDate 结束日期
* @param includeBegin 时间范围是否包含起始日期
* @param includeEnd 时间范围是否包含结束日期
* @return 是否在范围内
*/
public static boolean isIn(TemporalAccessor date, TemporalAccessor beginDate, TemporalAccessor endDate,
boolean includeBegin, boolean includeEnd) {
if (date == null || beginDate == null || endDate == null) {
throw new IllegalArgumentException("参数不可为null");
}

final long thisMills = toEpochMilli(date);
final long beginMills = toEpochMilli(beginDate);
final long endMills = toEpochMilli(endDate);
final long rangeMin = Math.min(beginMills, endMills);
final long rangeMax = Math.max(beginMills, endMills);

// 先判断是否满足 date (beginDate, endDate)
boolean isIn = rangeMin < thisMills && thisMills < rangeMax;

// 若不满足,则再判断是否在时间范围的边界上
if (!isIn && includeBegin) {
isIn = thisMills == rangeMin;
}

if (!isIn && includeEnd) {
isIn = thisMills == rangeMax;
}

return thisMills >= Math.min(beginMills, endMills) && thisMills <= Math.max(beginMills, endMills);
return isIn;
}

/**
Expand Down
Expand Up @@ -116,7 +116,7 @@ public ClassScaner(String packageName, Predicate<Class<?>> predicate) {
* @param charset 编码
*/
public ClassScaner(String packageName, Predicate<Class<?>> predicate, java.nio.charset.Charset charset) {
packageName = StringKit.nullToEmpty(packageName);
packageName = StringKit.emptyIfNull(packageName);
this.packageName = packageName;
this.packageNameWithDot = StringKit.addSuffixIfNot(packageName, Symbol.DOT);
this.packageDirName = packageName.replace(Symbol.C_DOT, File.separatorChar);
Expand Down
Expand Up @@ -678,7 +678,7 @@ public static boolean isAllCharMatch(CharSequence value, org.aoju.bus.core.lang.
* @see #nullToEmpty(CharSequence)
*/
public static String emptyIfNull(CharSequence text) {
return nullToEmpty(text);
return ObjectKit.defaultIfNull(text, Normal.EMPTY).toString();
}

/**
Expand Down
Expand Up @@ -4057,7 +4057,7 @@ public static EnumerationIterator<URL> getResourceIter(String resource) {
* @return {@link URL}
*/
public static URL getResource(String resource, Class<?> baseClass) {
resource = StringKit.nullToEmpty(resource);
resource = StringKit.emptyIfNull(resource);
URL url = (null != baseClass) ? baseClass.getResource(resource) : ClassKit.getClassLoader().getResource(resource);
return null != url ? url : baseClass.getClassLoader().getResource(resource);
}
Expand Down
22 changes: 18 additions & 4 deletions bus-core/src/main/java/org/aoju/bus/core/toolkit/ImageKit.java
Expand Up @@ -402,16 +402,29 @@ public static void slice(java.awt.Image srcImage, File descDir, int destWidth, i
}

/**
* 图像切割(指定切片的行数和列数)
* 图像切割指定切片的行数和列数
*
* @param srcImageFile 源图像文件
* @param destDir 切片目标文件夹
* @param rows 目标切片行数 默认2,必须是范围 [1, 20] 之内
* @param cols 目标切片列数 默认2,必须是范围 [1, 20] 之内
*/
public static void sliceByRowsAndCols(File srcImageFile, File destDir, int rows, int cols) {
sliceByRowsAndCols(srcImageFile, destDir, FileType.TYPE_JPEG, rows, cols);
}

/**
* 图像切割(指定切片的行数和列数)
*
* @param srcImageFile 源图像文件
* @param destDir 切片目标文件夹
* @param format 目标文件格式
* @param rows 目标切片行数 默认2,必须是范围 [1, 20] 之内
* @param cols 目标切片列数 默认2,必须是范围 [1, 20] 之内
*/
public static void sliceByRowsAndCols(File srcImageFile, File destDir, String format, int rows, int cols) {
try {
sliceByRowsAndCols(ImageIO.read(srcImageFile), destDir, rows, cols);
sliceByRowsAndCols(ImageIO.read(srcImageFile), destDir, format, rows, cols);
} catch (IOException e) {
throw new InternalException(e);
}
Expand All @@ -422,10 +435,11 @@ public static void sliceByRowsAndCols(File srcImageFile, File destDir, int rows,
*
* @param srcImage 源图像
* @param destDir 切片目标文件夹
* @param format 目标文件格式
* @param rows 目标切片行数 默认2,必须是范围 [1, 20] 之内
* @param cols 目标切片列数 默认2,必须是范围 [1, 20] 之内
*/
public static void sliceByRowsAndCols(java.awt.Image srcImage, File destDir, int rows, int cols) {
public static void sliceByRowsAndCols(java.awt.Image srcImage, File destDir, String format, int rows, int cols) {
if (false == destDir.exists()) {
FileKit.mkdir(destDir);
} else if (false == destDir.isDirectory()) {
Expand Down Expand Up @@ -453,7 +467,7 @@ public static void sliceByRowsAndCols(java.awt.Image srcImage, File destDir, int
for (int j = 0; j < cols; j++) {
tag = cut(bi, new Rectangle(j * destWidth, i * destHeight, destWidth, destHeight));
// 输出为文件
ImageIO.write(toRenderedImage(tag), FileType.TYPE_JPEG, new File(destDir, "_r" + i + "_c" + j + ".jpg"));
ImageIO.write(toRenderedImage(tag), format, new File(destDir, "_r" + i + "_c" + j + ".jpg"));
}
}
} catch (IOException e) {
Expand Down
20 changes: 17 additions & 3 deletions bus-core/src/main/java/org/aoju/bus/core/toolkit/IterKit.java
Expand Up @@ -269,21 +269,35 @@ public static <K, V> Map<K, V> fieldValueAsMap(Iterator<?> iterator, String fiel
return result;
}

/**
* 获取指定Bean列表中某个字段,生成新的列表
*
* @param <R> 返回元素类型
* @param <V> 对象类型
* @param iterable 对象列表
* @param fieldName 字段名(会通过反射获取其值)
* @return 某个字段值与对象对应Map
*/
public static <V, R> List<R> fieldValueList(final Iterable<V> iterable, final String fieldName) {
return fieldValueList(get(iterable), fieldName);
}

/**
* 获取指定Bean列表中某个字段,生成新的列表
*
* @param <R> 返回元素类型
* @param <V> 对象类型
* @param iterator 对象列表
* @param fieldName 字段名(会通过反射获取其值)
* @return 某个字段值与对象对应Map
*/
public static <V> List<Object> fieldValueList(Iterator<V> iterator, String fieldName) {
final List<Object> result = new ArrayList<>();
public static <V, R> List<R> fieldValueList(final Iterator<V> iterator, final String fieldName) {
final List<R> result = new ArrayList<>();
if (null != iterator) {
V value;
while (iterator.hasNext()) {
value = iterator.next();
result.add(ReflectKit.getFieldValue(value, fieldName));
result.add((R) ReflectKit.getFieldValue(value, fieldName));
}
}
return result;
Expand Down
22 changes: 21 additions & 1 deletion bus-core/src/main/java/org/aoju/bus/core/toolkit/MapKit.java
Expand Up @@ -132,7 +132,7 @@ public static <K, V> TreeMap<K, V> newTreeMap(Comparator<? super K> comparator)
* @return HashMap对象
*/
public static <K, V> HashMap<K, V> newHashMap(int size, boolean isLinked) {
int initialCapacity = (int) (size / DEFAULT_LOAD_FACTOR) + 1;
final int initialCapacity = (int) (size / DEFAULT_LOAD_FACTOR) + 1;
return isLinked ? new LinkedHashMap<>(initialCapacity) : new HashMap<>(initialCapacity);
}

Expand Down Expand Up @@ -1340,4 +1340,24 @@ public static <K, V> Map.Entry<K, V> entry(K key, V value, boolean isImmutable)
new AbstractMap.SimpleEntry<>(key, value);
}

/**
* 根据给定的entry列表,根据entry的key进行分组
*
* @param <K> 键类型
* @param <V> 值类型
* @param entries entry列表
* @return this map
*/
public static <K, V> Map<K, List<V>> grouping(Iterable<Map.Entry<K, V>> entries) {
final Map<K, List<V>> map = new HashMap<>();
if (CollKit.isEmpty(entries)) {
return map;
}
for (final Map.Entry<K, V> pair : entries) {
final List<V> values = map.computeIfAbsent(pair.getKey(), k -> new ArrayList<>());
values.add(pair.getValue());
}
return map;
}

}
55 changes: 55 additions & 0 deletions bus-core/src/main/java/org/aoju/bus/core/toolkit/RuntimeKit.java
Expand Up @@ -42,6 +42,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
* 系统运行时工具类
Expand Down Expand Up @@ -271,6 +272,60 @@ public static void addShutdownHook(Runnable hook) {
Runtime.getRuntime().addShutdownHook((hook instanceof Thread) ? (Thread) hook : new Thread(hook));
}

/**
* 获得JVM可用的处理器数量(一般为CPU核心数)
* <p>
* 这里做一个特殊的处理,在特殊的CPU上面,会有获取不到CPU数量的情况,所以这里做一个保护;
* 默认给一个7,真实的CPU基本都是偶数,方便区分。
* 如果不做处理,会出现创建线程池时{@link ThreadPoolExecutor},抛出异常:{@link IllegalArgumentException}
*
* @return 可用的处理器数量
*/
public static int getProcessorCount() {
int cpu = Runtime.getRuntime().availableProcessors();
if (cpu <= 0) {
cpu = 7;
}
return cpu;
}

/**
* 获得JVM中剩余的内存数,单位byte
*
* @return JVM中剩余的内存数,单位byte
*/
public static long getFreeMemory() {
return Runtime.getRuntime().freeMemory();
}

/**
* 获得JVM已经从系统中获取到的总共的内存数,单位byte
*
* @return JVM中剩余的内存数,单位byte
*/
public static long getTotalMemory() {
return Runtime.getRuntime().totalMemory();
}

/**
* 获得JVM中可以从系统中获取的最大的内存数,单位byte,以-Xmx参数为准
*
* @return JVM中剩余的内存数,单位byte
*/
public static long getMaxMemory() {
return Runtime.getRuntime().maxMemory();
}

/**
* 获得JVM最大可用内存,计算方法为:<br>
* 最大内存-总内存+剩余内存
*
* @return 最大可用内存
*/
public static long getUsableMemory() {
return getMaxMemory() - getTotalMemory() + getFreeMemory();
}

/**
* 获得当前进程的PID
* 当失败时返回-1
Expand Down
Expand Up @@ -144,7 +144,7 @@ public static ThreadPoolExecutor newExecutorByBlockingCoefficient(float blocking
}

// 最佳的线程数 = CPU可用核心数 / (1 - 阻塞系数)
int poolSize = (int) (Runtime.getRuntime().availableProcessors() / (1 - blockingCoefficient));
int poolSize = (int) (RuntimeKit.getProcessorCount() / (1 - blockingCoefficient));
return ExecutorBuilder.create().setCorePoolSize(poolSize).setMaxPoolSize(poolSize).setKeepAliveTime(0L).build();
}

Expand Down

0 comments on commit 5b62daa

Please sign in to comment.