This commit is contained in:
Looly 2019-11-20 00:15:52 +08:00
parent eec404f875
commit 106ea5a759
12 changed files with 399 additions and 378 deletions

View File

@ -6,8 +6,11 @@
## 5.0.7 ## 5.0.7
### 新特性 ### 新特性
* 【core 】 解决NumberUtil导致的ambiguous问题issue#630@Github
* 【core 】 BeanUtil.isEmpty()忽略字段支持增加isNotEmptyissue#629@Github
### Bug修复 ### Bug修复
* 【extra】 修复SFTP.upload上传失败的问题
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -676,17 +676,31 @@ public class BeanUtil {
return bean; return bean;
} }
/**
* 判断Bean是否为非空对象非空对象表示本身不为<code>null</code>或者含有非<code>null</code>属性的对象
*
* @param bean Bean对象
* @param ignoreFiledNames 忽略检查的字段名
* @return 是否为空<code>true</code> - / <code>false</code> - 非空
* @since 5.0.7
*/
public static boolean isNotEmpty(Object bean, String... ignoreFiledNames) {
return false == isEmpty(bean, ignoreFiledNames);
}
/** /**
* 判断Bean是否为空对象空对象表示本身为<code>null</code>或者所有属性都为<code>null</code> * 判断Bean是否为空对象空对象表示本身为<code>null</code>或者所有属性都为<code>null</code>
* *
* @param bean Bean对象 * @param bean Bean对象
* @param ignoreFiledNames 忽略检查的字段名
* @return 是否为空<code>true</code> - / <code>false</code> - 非空 * @return 是否为空<code>true</code> - / <code>false</code> - 非空
* @since 4.1.10 * @since 4.1.10
*/ */
public static boolean isEmpty(Object bean) { public static boolean isEmpty(Object bean, String... ignoreFiledNames) {
if (null != bean) { if (null != bean) {
for (Field field : ReflectUtil.getFields(bean.getClass())) { for (Field field : ReflectUtil.getFields(bean.getClass())) {
if (null != ReflectUtil.getFieldValue(bean, field)) { if ((false == ArrayUtil.contains(ignoreFiledNames, field.getName()))
&& null != ReflectUtil.getFieldValue(bean, field)) {
return false; return false;
} }
} }
@ -699,15 +713,17 @@ public class BeanUtil {
* 对象本身为<code>null</code>也返回true * 对象本身为<code>null</code>也返回true
* *
* @param bean Bean对象 * @param bean Bean对象
* @param ignoreFiledNames 忽略检查的字段名
* @return 是否包含值为<code>null</code>的属性<code>true</code> - 包含 / <code>false</code> - 不包含 * @return 是否包含值为<code>null</code>的属性<code>true</code> - 包含 / <code>false</code> - 不包含
* @since 4.1.10 * @since 4.1.10
*/ */
public static boolean hasNullField(Object bean) { public static boolean hasNullField(Object bean, String... ignoreFiledNames) {
if (null == bean) { if (null == bean) {
return true; return true;
} }
for (Field field : ReflectUtil.getFields(bean.getClass())) { for (Field field : ReflectUtil.getFields(bean.getClass())) {
if (null == ReflectUtil.getFieldValue(bean, field)) { if ((false == ArrayUtil.contains(ignoreFiledNames, field.getName()))//
&& null == ReflectUtil.getFieldValue(bean, field)) {
return true; return true;
} }
} }

View File

@ -477,7 +477,7 @@ public class ArrayUtil {
* @return 新数组 * @return 新数组
* @since 4.0.8 * @since 4.0.8
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings({"unchecked", "SuspiciousSystemArraycopy"})
public static <T> Object insert(Object array, int index, T... newElements) { public static <T> Object insert(Object array, int index, T... newElements) {
if (isEmpty(newElements)) { if (isEmpty(newElements)) {
return array; return array;
@ -541,6 +541,7 @@ public class ArrayUtil {
final int length = length(array); final int length = length(array);
final Object newArray = Array.newInstance(array.getClass().getComponentType(), newSize); final Object newArray = Array.newInstance(array.getClass().getComponentType(), newSize);
if (newSize > 0 && isNotEmpty(array)) { if (newSize > 0 && isNotEmpty(array)) {
//noinspection SuspiciousSystemArraycopy
System.arraycopy(array, 0, newArray, 0, Math.min(length, newSize)); System.arraycopy(array, 0, newArray, 0, Math.min(length, newSize));
} }
return newArray; return newArray;
@ -656,6 +657,7 @@ public class ArrayUtil {
* @since 3.0.6 * @since 3.0.6
*/ */
public static Object copy(Object src, int srcPos, Object dest, int destPos, int length) { public static Object copy(Object src, int srcPos, Object dest, int destPos, int length) {
//noinspection SuspiciousSystemArraycopy
System.arraycopy(src, srcPos, dest, destPos, length); System.arraycopy(src, srcPos, dest, destPos, length);
return dest; return dest;
} }
@ -671,6 +673,7 @@ public class ArrayUtil {
* @since 3.0.6 * @since 3.0.6
*/ */
public static Object copy(Object src, Object dest, int length) { public static Object copy(Object src, Object dest, int length) {
//noinspection SuspiciousSystemArraycopy
System.arraycopy(src, 0, dest, 0, length); System.arraycopy(src, 0, dest, 0, length);
return dest; return dest;
} }
@ -1477,7 +1480,7 @@ public class ArrayUtil {
final Integer[] array = new Integer[length]; final Integer[] array = new Integer[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = Integer.valueOf(values[i]); array[i] = values[i];
} }
return array; return array;
} }
@ -1499,7 +1502,7 @@ public class ArrayUtil {
final int[] array = new int[length]; final int[] array = new int[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = values[i].intValue(); array[i] = values[i];
} }
return array; return array;
} }
@ -1521,7 +1524,7 @@ public class ArrayUtil {
final Long[] array = new Long[length]; final Long[] array = new Long[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = Long.valueOf(values[i]); array[i] = values[i];
} }
return array; return array;
} }
@ -1543,7 +1546,7 @@ public class ArrayUtil {
final long[] array = new long[length]; final long[] array = new long[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = values[i].longValue(); array[i] = values[i];
} }
return array; return array;
} }
@ -1565,7 +1568,7 @@ public class ArrayUtil {
final Character[] array = new Character[length]; final Character[] array = new Character[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = Character.valueOf(values[i]); array[i] = values[i];
} }
return array; return array;
} }
@ -1587,7 +1590,7 @@ public class ArrayUtil {
char[] array = new char[length]; char[] array = new char[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = values[i].charValue(); array[i] = values[i];
} }
return array; return array;
} }
@ -1609,7 +1612,7 @@ public class ArrayUtil {
final Byte[] array = new Byte[length]; final Byte[] array = new Byte[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = Byte.valueOf(values[i]); array[i] = values[i];
} }
return array; return array;
} }
@ -1631,7 +1634,7 @@ public class ArrayUtil {
final byte[] array = new byte[length]; final byte[] array = new byte[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = values[i].byteValue(); array[i] = ObjectUtil.defaultIfNull(values[i], (byte)0);
} }
return array; return array;
} }
@ -1653,7 +1656,7 @@ public class ArrayUtil {
final Short[] array = new Short[length]; final Short[] array = new Short[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = Short.valueOf(values[i]); array[i] = values[i];
} }
return array; return array;
} }
@ -1675,7 +1678,7 @@ public class ArrayUtil {
final short[] array = new short[length]; final short[] array = new short[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = values[i].shortValue(); array[i] = ObjectUtil.defaultIfNull(values[i], (short)0);
} }
return array; return array;
} }
@ -1697,7 +1700,7 @@ public class ArrayUtil {
final Float[] array = new Float[length]; final Float[] array = new Float[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = Float.valueOf(values[i]); array[i] = values[i];
} }
return array; return array;
} }
@ -1719,7 +1722,7 @@ public class ArrayUtil {
final float[] array = new float[length]; final float[] array = new float[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = values[i].floatValue(); array[i] = ObjectUtil.defaultIfNull(values[i], 0F);
} }
return array; return array;
} }
@ -1741,7 +1744,7 @@ public class ArrayUtil {
final Double[] array = new Double[length]; final Double[] array = new Double[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = Double.valueOf(values[i]); array[i] = values[i];
} }
return array; return array;
} }
@ -1763,7 +1766,7 @@ public class ArrayUtil {
final double[] array = new double[length]; final double[] array = new double[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = values[i].doubleValue(); array[i] = ObjectUtil.defaultIfNull(values[i], 0D);
} }
return array; return array;
} }
@ -1785,7 +1788,7 @@ public class ArrayUtil {
final Boolean[] array = new Boolean[length]; final Boolean[] array = new Boolean[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = Boolean.valueOf(values[i]); array[i] = values[i];
} }
return array; return array;
} }
@ -1807,7 +1810,7 @@ public class ArrayUtil {
final boolean[] array = new boolean[length]; final boolean[] array = new boolean[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = values[i].booleanValue(); array[i] = ObjectUtil.defaultIfNull(values[i], false);
} }
return array; return array;
} }
@ -2841,6 +2844,7 @@ public class ArrayUtil {
* @throws IllegalArgumentException 参数对象不为数组对象 * @throws IllegalArgumentException 参数对象不为数组对象
* @since 3.0.8 * @since 3.0.8
*/ */
@SuppressWarnings("SuspiciousSystemArraycopy")
public static Object remove(Object array, int index) throws IllegalArgumentException { public static Object remove(Object array, int index) throws IllegalArgumentException {
if (null == array) { if (null == array) {
return null; return null;
@ -3339,9 +3343,9 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
T min = numberArray[0]; T min = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (T t : numberArray) {
if (ObjectUtil.compare(min, numberArray[i]) > 0) { if (ObjectUtil.compare(min, t) > 0) {
min = numberArray[i]; min = t;
} }
} }
return min; return min;
@ -3359,7 +3363,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
long min = numberArray[0]; long min = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (min > numberArray[i]) { if (min > numberArray[i]) {
min = numberArray[i]; min = numberArray[i];
} }
@ -3379,7 +3383,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
int min = numberArray[0]; int min = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (min > numberArray[i]) { if (min > numberArray[i]) {
min = numberArray[i]; min = numberArray[i];
} }
@ -3399,7 +3403,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
short min = numberArray[0]; short min = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (min > numberArray[i]) { if (min > numberArray[i]) {
min = numberArray[i]; min = numberArray[i];
} }
@ -3419,7 +3423,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
char min = numberArray[0]; char min = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (min > numberArray[i]) { if (min > numberArray[i]) {
min = numberArray[i]; min = numberArray[i];
} }
@ -3439,7 +3443,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
byte min = numberArray[0]; byte min = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (min > numberArray[i]) { if (min > numberArray[i]) {
min = numberArray[i]; min = numberArray[i];
} }
@ -3459,7 +3463,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
double min = numberArray[0]; double min = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (min > numberArray[i]) { if (min > numberArray[i]) {
min = numberArray[i]; min = numberArray[i];
} }
@ -3479,7 +3483,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
float min = numberArray[0]; float min = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (min > numberArray[i]) { if (min > numberArray[i]) {
min = numberArray[i]; min = numberArray[i];
} }
@ -3500,7 +3504,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
T max = numberArray[0]; T max = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (ObjectUtil.compare(max, numberArray[i]) < 0) { if (ObjectUtil.compare(max, numberArray[i]) < 0) {
max = numberArray[i]; max = numberArray[i];
} }
@ -3520,7 +3524,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
long max = numberArray[0]; long max = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (max < numberArray[i]) { if (max < numberArray[i]) {
max = numberArray[i]; max = numberArray[i];
} }
@ -3540,7 +3544,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
int max = numberArray[0]; int max = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (max < numberArray[i]) { if (max < numberArray[i]) {
max = numberArray[i]; max = numberArray[i];
} }
@ -3560,7 +3564,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
short max = numberArray[0]; short max = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (max < numberArray[i]) { if (max < numberArray[i]) {
max = numberArray[i]; max = numberArray[i];
} }
@ -3580,7 +3584,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
char max = numberArray[0]; char max = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (max < numberArray[i]) { if (max < numberArray[i]) {
max = numberArray[i]; max = numberArray[i];
} }
@ -3600,7 +3604,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
byte max = numberArray[0]; byte max = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (max < numberArray[i]) { if (max < numberArray[i]) {
max = numberArray[i]; max = numberArray[i];
} }
@ -3620,7 +3624,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
double max = numberArray[0]; double max = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (max < numberArray[i]) { if (max < numberArray[i]) {
max = numberArray[i]; max = numberArray[i];
} }
@ -3640,7 +3644,7 @@ public class ArrayUtil {
throw new IllegalArgumentException("Number array must not empty !"); throw new IllegalArgumentException("Number array must not empty !");
} }
float max = numberArray[0]; float max = numberArray[0];
for (int i = 0; i < numberArray.length; i++) { for (int i = 1; i < numberArray.length; i++) {
if (max < numberArray[i]) { if (max < numberArray[i]) {
max = numberArray[i]; max = numberArray[i];
} }

View File

@ -10,6 +10,7 @@ import cn.hutool.core.lang.Validator;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
/** /**
* 身份证相关工具类<br> * 身份证相关工具类<br>
@ -323,10 +324,10 @@ public class IdcardUtil {
final char[] chars = mid.toCharArray(); final char[] chars = mid.toCharArray();
int iflag = 8; int iflag = 8;
for (char c : chars) { for (char c : chars) {
sum += Integer.valueOf(String.valueOf(c)) * iflag; sum += Integer.parseInt(String.valueOf(c)) * iflag;
iflag--; iflag--;
} }
return (sum % 10 == 0 ? 0 : (10 - sum % 10)) == Integer.valueOf(end); return (sum % 10 == 0 ? 0 : (10 - sum % 10)) == Integer.parseInt(end);
} }
/** /**
@ -360,7 +361,7 @@ public class IdcardUtil {
char[] chars = mid.toCharArray(); char[] chars = mid.toCharArray();
int iflag = 7; int iflag = 7;
for (char c : chars) { for (char c : chars) {
sum = sum + Integer.valueOf(String.valueOf(c)) * iflag; sum = sum + Integer.parseInt(String.valueOf(c)) * iflag;
iflag--; iflag--;
} }
if ("A".equals(end.toUpperCase())) { if ("A".equals(end.toUpperCase())) {
@ -389,13 +390,15 @@ public class IdcardUtil {
* @return 生日(yyyyMMdd) * @return 生日(yyyyMMdd)
*/ */
public static String getBirth(String idCard) { public static String getBirth(String idCard) {
Assert.notBlank(idCard, "id card must be not blank!");
final int len = idCard.length(); final int len = idCard.length();
if (len < CHINA_ID_MIN_LENGTH) { if (len < CHINA_ID_MIN_LENGTH) {
return null; return null;
} else if (len == CHINA_ID_MIN_LENGTH) { } else if (len == CHINA_ID_MIN_LENGTH) {
idCard = convert15To18(idCard); idCard = convert15To18(idCard);
} }
return idCard.substring(6, 14);
return Objects.requireNonNull(idCard).substring(6, 14);
} }
/** /**
@ -444,7 +447,7 @@ public class IdcardUtil {
} else if (len == CHINA_ID_MIN_LENGTH) { } else if (len == CHINA_ID_MIN_LENGTH) {
idCard = convert15To18(idCard); idCard = convert15To18(idCard);
} }
return Short.valueOf(idCard.substring(6, 10)); return Short.valueOf(Objects.requireNonNull(idCard).substring(6, 10));
} }
/** /**
@ -460,7 +463,7 @@ public class IdcardUtil {
} else if (len == CHINA_ID_MIN_LENGTH) { } else if (len == CHINA_ID_MIN_LENGTH) {
idCard = convert15To18(idCard); idCard = convert15To18(idCard);
} }
return Short.valueOf(idCard.substring(10, 12)); return Short.valueOf(Objects.requireNonNull(idCard).substring(10, 12));
} }
/** /**
@ -476,7 +479,7 @@ public class IdcardUtil {
} else if (len == CHINA_ID_MIN_LENGTH) { } else if (len == CHINA_ID_MIN_LENGTH) {
idCard = convert15To18(idCard); idCard = convert15To18(idCard);
} }
return Short.valueOf(idCard.substring(12, 14)); return Short.valueOf(Objects.requireNonNull(idCard).substring(12, 14));
} }
/** /**
@ -495,7 +498,7 @@ public class IdcardUtil {
if (len == CHINA_ID_MIN_LENGTH) { if (len == CHINA_ID_MIN_LENGTH) {
idCard = convert15To18(idCard); idCard = convert15To18(idCard);
} }
char sCardChar = idCard.charAt(16); char sCardChar = Objects.requireNonNull(idCard).charAt(16);
return (sCardChar % 2 != 0) ? 1 : 0; return (sCardChar % 2 != 0) ? 1 : 0;
} }
@ -586,7 +589,7 @@ public class IdcardUtil {
int iSum = 0; int iSum = 0;
if (power.length == iArr.length) { if (power.length == iArr.length) {
for (int i = 0; i < iArr.length; i++) { for (int i = 0; i < iArr.length; i++) {
iSum += Integer.valueOf(String.valueOf(iArr[i])) * power[i]; iSum += Integer.parseInt(String.valueOf(iArr[i])) * power[i];
} }
} }
return iSum; return iSum;

View File

@ -22,7 +22,7 @@ import java.util.Set;
* <pre> * <pre>
* new BigDecimal(0.1) * new BigDecimal(0.1)
* </pre> * </pre>
* * <p>
* 表示的不是<strong>0.1</strong>而是<strong>0.1000000000000000055511151231257827021181583404541015625</strong> * 表示的不是<strong>0.1</strong>而是<strong>0.1000000000000000055511151231257827021181583404541015625</strong>
* *
* <p> * <p>
@ -35,11 +35,12 @@ import java.util.Set;
* </ul> * </ul>
* *
* @author Looly * @author Looly
*
*/ */
public class NumberUtil { public class NumberUtil {
/** 默认除法运算精度 */ /**
* 默认除法运算精度
*/
private static final int DEFAUT_DIV_SCALE = 10; private static final int DEFAUT_DIV_SCALE = 10;
/** /**
@ -95,6 +96,7 @@ public class NumberUtil {
* @since 3.1.1 * @since 3.1.1
*/ */
public static double add(Double v1, Double v2) { public static double add(Double v1, Double v2) {
//noinspection RedundantCast
return add((Number) v1, (Number) v2).doubleValue(); return add((Number) v1, (Number) v2).doubleValue();
} }
@ -234,6 +236,7 @@ public class NumberUtil {
* @return * @return
*/ */
public static double sub(Double v1, Double v2) { public static double sub(Double v1, Double v2) {
//noinspection RedundantCast
return sub((Number) v1, (Number) v2).doubleValue(); return sub((Number) v1, (Number) v2).doubleValue();
} }
@ -374,6 +377,7 @@ public class NumberUtil {
* @return * @return
*/ */
public static double mul(Double v1, Double v2) { public static double mul(Double v1, Double v2) {
//noinspection RedundantCast
return mul((Number) v1, (Number) v2).doubleValue(); return mul((Number) v1, (Number) v2).doubleValue();
} }
@ -698,6 +702,7 @@ public class NumberUtil {
* @return 两个参数的商 * @return 两个参数的商
*/ */
public static double div(Double v1, Double v2, int scale, RoundingMode roundingMode) { public static double div(Double v1, Double v2, int scale, RoundingMode roundingMode) {
//noinspection RedundantCast
return div((Number) v1, (Number) v2, scale, roundingMode).doubleValue(); return div((Number) v1, (Number) v2, scale, roundingMode).doubleValue();
} }
@ -750,6 +755,7 @@ public class NumberUtil {
} }
// ------------------------------------------------------------------------------------------- round // ------------------------------------------------------------------------------------------- round
/** /**
* 保留固定位数小数<br> * 保留固定位数小数<br>
* 采用四舍五入策略 {@link RoundingMode#HALF_UP}<br> * 采用四舍五入策略 {@link RoundingMode#HALF_UP}<br>
@ -971,6 +977,7 @@ public class NumberUtil {
} }
// ------------------------------------------------------------------------------------------- decimalFormat // ------------------------------------------------------------------------------------------- decimalFormat
/** /**
* 格式化double<br> * 格式化double<br>
* {@link DecimalFormat} 做封装<br> * {@link DecimalFormat} 做封装<br>
@ -1042,6 +1049,7 @@ public class NumberUtil {
} }
// ------------------------------------------------------------------------------------------- isXXX // ------------------------------------------------------------------------------------------- isXXX
/** /**
* 是否为数字支持包括 * 是否为数字支持包括
* *
@ -1288,6 +1296,7 @@ public class NumberUtil {
} }
// ------------------------------------------------------------------------------------------- range // ------------------------------------------------------------------------------------------- range
/** /**
* 从0开始给定范围内的整数列表步进为1 * 从0开始给定范围内的整数列表步进为1
* *
@ -1375,6 +1384,7 @@ public class NumberUtil {
} }
// ------------------------------------------------------------------------------------------- others // ------------------------------------------------------------------------------------------- others
/** /**
* 计算阶乘 * 计算阶乘
* <p> * <p>
@ -1514,11 +1524,10 @@ public class NumberUtil {
/** /**
* 比较两个值的大小 * 比较两个值的大小
* *
* @see Character#compare(char, char)
*
* @param x 第一个值 * @param x 第一个值
* @param y 第二个值 * @param y 第二个值
* @return x==y返回0x&lt;y返回-1x&gt;y返回1 * @return x==y返回0x&lt;y返回-1x&gt;y返回1
* @see Character#compare(char, char)
* @since 3.0.1 * @since 3.0.1
*/ */
public static int compare(char x, char y) { public static int compare(char x, char y) {
@ -1528,11 +1537,10 @@ public class NumberUtil {
/** /**
* 比较两个值的大小 * 比较两个值的大小
* *
* @see Double#compare(double, double)
*
* @param x 第一个值 * @param x 第一个值
* @param y 第二个值 * @param y 第二个值
* @return x==y返回0x&lt;y返回-1x&gt;y返回1 * @return x==y返回0x&lt;y返回-1x&gt;y返回1
* @see Double#compare(double, double)
* @since 3.0.1 * @since 3.0.1
*/ */
public static int compare(double x, double y) { public static int compare(double x, double y) {
@ -1542,11 +1550,10 @@ public class NumberUtil {
/** /**
* 比较两个值的大小 * 比较两个值的大小
* *
* @see Integer#compare(int, int)
*
* @param x 第一个值 * @param x 第一个值
* @param y 第二个值 * @param y 第二个值
* @return x==y返回0x&lt;y返回-1x&gt;y返回1 * @return x==y返回0x&lt;y返回-1x&gt;y返回1
* @see Integer#compare(int, int)
* @since 3.0.1 * @since 3.0.1
*/ */
public static int compare(int x, int y) { public static int compare(int x, int y) {
@ -1556,11 +1563,10 @@ public class NumberUtil {
/** /**
* 比较两个值的大小 * 比较两个值的大小
* *
* @see Long#compare(long, long)
*
* @param x 第一个值 * @param x 第一个值
* @param y 第二个值 * @param y 第二个值
* @return x==y返回0x&lt;y返回-1x&gt;y返回1 * @return x==y返回0x&lt;y返回-1x&gt;y返回1
* @see Long#compare(long, long)
* @since 3.0.1 * @since 3.0.1
*/ */
public static int compare(long x, long y) { public static int compare(long x, long y) {
@ -1570,11 +1576,10 @@ public class NumberUtil {
/** /**
* 比较两个值的大小 * 比较两个值的大小
* *
* @see Short#compare(short, short)
*
* @param x 第一个值 * @param x 第一个值
* @param y 第二个值 * @param y 第二个值
* @return x==y返回0x&lt;y返回-1x&gt;y返回1 * @return x==y返回0x&lt;y返回-1x&gt;y返回1
* @see Short#compare(short, short)
* @since 3.0.1 * @since 3.0.1
*/ */
public static int compare(short x, short y) { public static int compare(short x, short y) {
@ -1584,11 +1589,10 @@ public class NumberUtil {
/** /**
* 比较两个值的大小 * 比较两个值的大小
* *
* @see Byte#compare(byte, byte)
*
* @param x 第一个值 * @param x 第一个值
* @param y 第二个值 * @param y 第二个值
* @return x==y返回0x&lt;y返回-1x&gt;y返回1 * @return x==y返回0x&lt;y返回-1x&gt;y返回1
* @see Byte#compare(byte, byte)
* @since 3.0.1 * @since 3.0.1
*/ */
public static int compare(byte x, byte y) { public static int compare(byte x, byte y) {
@ -1673,8 +1677,8 @@ public class NumberUtil {
* @param c2 字符2 * @param c2 字符2
* @param ignoreCase 是否忽略大小写 * @param ignoreCase 是否忽略大小写
* @return 是否相同 * @return 是否相同
* @since 3.2.1
* @see CharUtil#equals(char, char, boolean) * @see CharUtil#equals(char, char, boolean)
* @since 3.2.1
*/ */
public static boolean equals(char c1, char c2, boolean ignoreCase) { public static boolean equals(char c1, char c2, boolean ignoreCase) {
return CharUtil.equals(c1, c2, ignoreCase); return CharUtil.equals(c1, c2, ignoreCase);
@ -1686,11 +1690,10 @@ public class NumberUtil {
* @param <T> 元素类型 * @param <T> 元素类型
* @param numberArray 数字数组 * @param numberArray 数字数组
* @return 最小值 * @return 最小值
* @since 4.0.7
* @see ArrayUtil#min(Comparable[]) * @see ArrayUtil#min(Comparable[])
* @since 4.0.7
*/ */
@SuppressWarnings("unchecked") public static <T extends Comparable<? super T>> T min(T[] numberArray) {
public static <T extends Comparable<? super T>> T min(T... numberArray) {
return ArrayUtil.min(numberArray); return ArrayUtil.min(numberArray);
} }
@ -1699,8 +1702,8 @@ public class NumberUtil {
* *
* @param numberArray 数字数组 * @param numberArray 数字数组
* @return 最小值 * @return 最小值
* @since 4.0.7
* @see ArrayUtil#min(long...) * @see ArrayUtil#min(long...)
* @since 4.0.7
*/ */
public static long min(long... numberArray) { public static long min(long... numberArray) {
return ArrayUtil.min(numberArray); return ArrayUtil.min(numberArray);
@ -1711,8 +1714,8 @@ public class NumberUtil {
* *
* @param numberArray 数字数组 * @param numberArray 数字数组
* @return 最小值 * @return 最小值
* @since 4.0.7
* @see ArrayUtil#min(int...) * @see ArrayUtil#min(int...)
* @since 4.0.7
*/ */
public static int min(int... numberArray) { public static int min(int... numberArray) {
return ArrayUtil.min(numberArray); return ArrayUtil.min(numberArray);
@ -1723,8 +1726,8 @@ public class NumberUtil {
* *
* @param numberArray 数字数组 * @param numberArray 数字数组
* @return 最小值 * @return 最小值
* @since 4.0.7
* @see ArrayUtil#min(short...) * @see ArrayUtil#min(short...)
* @since 4.0.7
*/ */
public static short min(short... numberArray) { public static short min(short... numberArray) {
return ArrayUtil.min(numberArray); return ArrayUtil.min(numberArray);
@ -1735,8 +1738,8 @@ public class NumberUtil {
* *
* @param numberArray 数字数组 * @param numberArray 数字数组
* @return 最小值 * @return 最小值
* @since 4.0.7
* @see ArrayUtil#min(double...) * @see ArrayUtil#min(double...)
* @since 4.0.7
*/ */
public static double min(double... numberArray) { public static double min(double... numberArray) {
return ArrayUtil.min(numberArray); return ArrayUtil.min(numberArray);
@ -1747,8 +1750,8 @@ public class NumberUtil {
* *
* @param numberArray 数字数组 * @param numberArray 数字数组
* @return 最小值 * @return 最小值
* @since 4.0.7
* @see ArrayUtil#min(float...) * @see ArrayUtil#min(float...)
* @since 4.0.7
*/ */
public static float min(float... numberArray) { public static float min(float... numberArray) {
return ArrayUtil.min(numberArray); return ArrayUtil.min(numberArray);
@ -1760,11 +1763,10 @@ public class NumberUtil {
* @param <T> 元素类型 * @param <T> 元素类型
* @param numberArray 数字数组 * @param numberArray 数字数组
* @return 最大值 * @return 最大值
* @since 4.0.7
* @see ArrayUtil#max(Comparable[]) * @see ArrayUtil#max(Comparable[])
* @since 4.0.7
*/ */
@SuppressWarnings("unchecked") public static <T extends Comparable<? super T>> T max(T[] numberArray) {
public static <T extends Comparable<? super T>> T max(T... numberArray) {
return ArrayUtil.max(numberArray); return ArrayUtil.max(numberArray);
} }
@ -1773,8 +1775,8 @@ public class NumberUtil {
* *
* @param numberArray 数字数组 * @param numberArray 数字数组
* @return 最大值 * @return 最大值
* @since 4.0.7
* @see ArrayUtil#max(long...) * @see ArrayUtil#max(long...)
* @since 4.0.7
*/ */
public static long max(long... numberArray) { public static long max(long... numberArray) {
return ArrayUtil.max(numberArray); return ArrayUtil.max(numberArray);
@ -1785,8 +1787,8 @@ public class NumberUtil {
* *
* @param numberArray 数字数组 * @param numberArray 数字数组
* @return 最大值 * @return 最大值
* @since 4.0.7
* @see ArrayUtil#max(int...) * @see ArrayUtil#max(int...)
* @since 4.0.7
*/ */
public static int max(int... numberArray) { public static int max(int... numberArray) {
return ArrayUtil.max(numberArray); return ArrayUtil.max(numberArray);
@ -1797,8 +1799,8 @@ public class NumberUtil {
* *
* @param numberArray 数字数组 * @param numberArray 数字数组
* @return 最大值 * @return 最大值
* @since 4.0.7
* @see ArrayUtil#max(short...) * @see ArrayUtil#max(short...)
* @since 4.0.7
*/ */
public static short max(short... numberArray) { public static short max(short... numberArray) {
return ArrayUtil.max(numberArray); return ArrayUtil.max(numberArray);
@ -1809,8 +1811,8 @@ public class NumberUtil {
* *
* @param numberArray 数字数组 * @param numberArray 数字数组
* @return 最大值 * @return 最大值
* @since 4.0.7
* @see ArrayUtil#max(double...) * @see ArrayUtil#max(double...)
* @since 4.0.7
*/ */
public static double max(double... numberArray) { public static double max(double... numberArray) {
return ArrayUtil.max(numberArray); return ArrayUtil.max(numberArray);
@ -1821,8 +1823,8 @@ public class NumberUtil {
* *
* @param numberArray 数字数组 * @param numberArray 数字数组
* @return 最大值 * @return 最大值
* @since 4.0.7
* @see ArrayUtil#max(float...) * @see ArrayUtil#max(float...)
* @since 4.0.7
*/ */
public static float max(float... numberArray) { public static float max(float... numberArray) {
return ArrayUtil.max(numberArray); return ArrayUtil.max(numberArray);
@ -1915,10 +1917,10 @@ public class NumberUtil {
* 是否空白符<br> * 是否空白符<br>
* 空白符包括空格制表符全角空格和不间断空格<br> * 空白符包括空格制表符全角空格和不间断空格<br>
* *
* @see Character#isWhitespace(int)
* @see Character#isSpaceChar(int)
* @param c 字符 * @param c 字符
* @return 是否空白符 * @return 是否空白符
* @see Character#isWhitespace(int)
* @see Character#isSpaceChar(int)
* @since 3.0.6 * @since 3.0.6
* @deprecated 请使用{@link CharUtil#isBlankChar(int)} * @deprecated 请使用{@link CharUtil#isBlankChar(int)}
*/ */
@ -2054,14 +2056,9 @@ public class NumberUtil {
* @since 4.0.7 * @since 4.0.7
*/ */
public static int partValue(int total, int partCount, boolean isPlusOneWhenHasRem) { public static int partValue(int total, int partCount, boolean isPlusOneWhenHasRem) {
int partValue; int partValue = total / partCount;
if (total % partCount == 0) { if (isPlusOneWhenHasRem && total % partCount == 0) {
partValue = total / partCount; partValue++;
} else {
partValue = (int) Math.floor(total / partCount);
if (isPlusOneWhenHasRem) {
partValue += 1;
}
} }
return partValue; return partValue;
} }

View File

@ -184,6 +184,7 @@ public class ObjectUtil {
* @return 是否为null * @return 是否为null
*/ */
public static boolean isNull(Object obj) { public static boolean isNull(Object obj) {
//noinspection ConstantConditions
return null == obj || obj.equals(null); return null == obj || obj.equals(null);
} }
@ -194,6 +195,7 @@ public class ObjectUtil {
* @return 是否为null * @return 是否为null
*/ */
public static boolean isNotNull(Object obj) { public static boolean isNotNull(Object obj) {
//noinspection ConstantConditions
return null != obj && false == obj.equals(null); return null != obj && false == obj.equals(null);
} }

View File

@ -191,13 +191,13 @@ public class NumberUtilTest {
@Test @Test
public void maxTest() { public void maxTest() {
int max = NumberUtil.max(new int[]{5,4,3,6,1}); int max = NumberUtil.max(5,4,3,6,1);
Assert.assertEquals(6, max); Assert.assertEquals(6, max);
} }
@Test @Test
public void minTest() { public void minTest() {
int min = NumberUtil.min(new int[]{5,4,3,6,1}); int min = NumberUtil.min(5,4,3,6,1);
Assert.assertEquals(1, min); Assert.assertEquals(1, min);
} }

View File

@ -133,13 +133,14 @@ public abstract class AbstractFtp implements Closeable {
} }
/** /**
* 将本地文件上传到目标服务器目标文件名为destPath若destPath为目录则目标文件名将与srcFilePath文件名相同覆盖模式 * 将本地文件上传到目标服务器目标文件名为destPath若destPath为目录则目标文件名将与file文件名相同
* 覆盖模式
* *
* @param srcFilePath 本地文件路径 * @param destPath 服务端路径可以为{@code null} 或者相对路径或绝对路径
* @param destFile 目标文件 * @param file 需要上传的文件
* @return 是否成功 * @return 是否成功
*/ */
public abstract boolean upload(String srcFilePath, File destFile); public abstract boolean upload(String destPath, File file);
/** /**
* 下载文件 * 下载文件

View File

@ -364,14 +364,14 @@ public class Ftp extends AbstractFtp {
* 3. path为绝对路径则上传到此路径 * 3. path为绝对路径则上传到此路径
* </pre> * </pre>
* *
* @param path 服务端路径可以为{@code null} 或者相对路径或绝对路径 * @param destPath 服务端路径可以为{@code null} 或者相对路径或绝对路径
* @param file 文件 * @param file 文件
* @return 是否上传成功 * @return 是否上传成功
*/ */
@Override @Override
public boolean upload(String path, File file) { public boolean upload(String destPath, File file) {
Assert.notNull(file, "file to upload is null !"); Assert.notNull(file, "file to upload is null !");
return upload(path, file.getName(), file); return upload(destPath, file.getName(), file);
} }
/** /**

View File

@ -238,9 +238,6 @@ public class JschUtil {
*/ */
public static int openAndBindPortToLocal(Connector sshConn, String remoteHost, int remotePort) throws JschRuntimeException { public static int openAndBindPortToLocal(Connector sshConn, String remoteHost, int remotePort) throws JschRuntimeException {
final Session session = openSession(sshConn.getHost(), sshConn.getPort(), sshConn.getUser(), sshConn.getPassword()); final Session session = openSession(sshConn.getHost(), sshConn.getPort(), sshConn.getUser(), sshConn.getPassword());
if (session == null) {
throw new JschRuntimeException("Error to create SSH Session");
}
final int localPort = generateLocalPort(); final int localPort = generateLocalPort();
bindPort(session, remoteHost, remotePort, localPort); bindPort(session, remoteHost, remotePort, localPort);
return localPort; return localPort;
@ -359,7 +356,7 @@ public class JschUtil {
if (null == charset) { if (null == charset) {
charset = CharsetUtil.CHARSET_UTF_8; charset = CharsetUtil.CHARSET_UTF_8;
} }
ChannelExec channel = (ChannelExec) openChannel(session, ChannelType.EXEC); final ChannelExec channel = (ChannelExec) openChannel(session, ChannelType.EXEC);
channel.setCommand(StrUtil.bytes(cmd, charset)); channel.setCommand(StrUtil.bytes(cmd, charset));
channel.setInputStream(null); channel.setInputStream(null);
channel.setErrStream(errStream); channel.setErrStream(errStream);

View File

@ -1,12 +1,9 @@
package cn.hutool.extra.ssh; package cn.hutool.extra.ssh;
import java.io.File; import cn.hutool.core.io.FileUtil;
import java.io.IOException; import cn.hutool.core.lang.Filter;
import java.nio.charset.Charset; import cn.hutool.core.util.StrUtil;
import java.util.ArrayList; import cn.hutool.extra.ftp.AbstractFtp;
import java.util.List;
import java.util.Vector;
import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry; import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.ChannelSftp.LsEntrySelector; import com.jcraft.jsch.ChannelSftp.LsEntrySelector;
@ -14,10 +11,11 @@ import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException; import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.SftpProgressMonitor; import com.jcraft.jsch.SftpProgressMonitor;
import cn.hutool.core.io.FileUtil; import java.io.File;
import cn.hutool.core.lang.Filter; import java.nio.charset.Charset;
import cn.hutool.core.util.StrUtil; import java.util.ArrayList;
import cn.hutool.extra.ftp.AbstractFtp; import java.util.List;
import java.util.Vector;
/** /**
* SFTP是Secure File Transfer Protocol的缩写安全文件传送协议可以为传输文件提供一种安全的加密方法<br> * SFTP是Secure File Transfer Protocol的缩写安全文件传送协议可以为传输文件提供一种安全的加密方法<br>
@ -336,8 +334,8 @@ public class Sftp extends AbstractFtp {
} }
@Override @Override
public boolean upload(String srcFilePath, File destFile) { public boolean upload(String destPath, File file) {
put(srcFilePath, FileUtil.getAbsolutePath(destFile)); put(FileUtil.getAbsolutePath(file), destPath);
return true; return true;
} }

View File

@ -23,8 +23,8 @@ public class RuntimeInfo implements Serializable{
} }
/** /**
* 获得JVM最大可用内存 * 获得JVM最大内存
* @return 最大可用内存 * @return 最大内存
*/ */
public final long getMaxMemory(){ public final long getMaxMemory(){
return currentRuntime.maxMemory(); return currentRuntime.maxMemory();