mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
DataSizeUtil support decimal
This commit is contained in:
parent
8cc5b7ffa4
commit
3617d0a569
@ -14,6 +14,7 @@
|
|||||||
* 【core 】 优化StrUtil中部分参数校验以及逻辑处理(pr#1144@Github)
|
* 【core 】 优化StrUtil中部分参数校验以及逻辑处理(pr#1144@Github)
|
||||||
* 【core 】 简化CreditCode逻辑去除无用Character.toUpperCase(pr#1145@Github)
|
* 【core 】 简化CreditCode逻辑去除无用Character.toUpperCase(pr#1145@Github)
|
||||||
* 【core 】 NumberUtil增加generateRandomNumber重载,可自定义seed(issue#I1XTUT@Gitee)
|
* 【core 】 NumberUtil增加generateRandomNumber重载,可自定义seed(issue#I1XTUT@Gitee)
|
||||||
|
* 【core 】 DataSizeUtil支持小数(pr#1158@Github)
|
||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
* 【core 】 解决农历判断节日未判断大小月导致的问题(issue#I1XHSF@Gitee)
|
* 【core 】 解决农历判断节日未判断大小月导致的问题(issue#I1XHSF@Gitee)
|
||||||
|
@ -28,7 +28,7 @@ public final class DataSize implements Comparable<DataSize> {
|
|||||||
/**
|
/**
|
||||||
* The pattern for parsing.
|
* The pattern for parsing.
|
||||||
*/
|
*/
|
||||||
private static final Pattern PATTERN = Pattern.compile("^([+\\-]?\\d+||(\\d+\\.\\d+))([a-zA-Z]{0,2})$");
|
private static final Pattern PATTERN = Pattern.compile("^([+-]?\\d+(\\.\\d+)?)([a-zA-Z]{0,2})$");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bytes per Kilobyte(KB).
|
* Bytes per Kilobyte(KB).
|
||||||
@ -68,61 +68,61 @@ public final class DataSize implements Comparable<DataSize> {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得对应bytes的{@link DataSize}
|
* 获得对应bytes的DataSize
|
||||||
*
|
*
|
||||||
* @param bytes bytes大小,可正可负
|
* @param bytes bytes大小,可正可负
|
||||||
* @return a {@link DataSize}
|
* @return this
|
||||||
*/
|
*/
|
||||||
public static DataSize ofBytes(long bytes) {
|
public static DataSize ofBytes(long bytes) {
|
||||||
return new DataSize(bytes);
|
return new DataSize(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得对应kilobytes的{@link DataSize}
|
* 获得对应kilobytes的DataSize
|
||||||
*
|
*
|
||||||
* @param kilobytes kilobytes大小,可正可负
|
* @param kilobytes kilobytes大小,可正可负
|
||||||
* @return a {@link DataSize}
|
* @return a DataSize
|
||||||
*/
|
*/
|
||||||
public static DataSize ofKilobytes(long kilobytes) {
|
public static DataSize ofKilobytes(long kilobytes) {
|
||||||
return new DataSize(Math.multiplyExact(kilobytes, BYTES_PER_KB));
|
return new DataSize(Math.multiplyExact(kilobytes, BYTES_PER_KB));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得对应megabytes的{@link DataSize}
|
* 获得对应megabytes的DataSize
|
||||||
*
|
*
|
||||||
* @param megabytes megabytes大小,可正可负
|
* @param megabytes megabytes大小,可正可负
|
||||||
* @return a {@link DataSize}
|
* @return a DataSize
|
||||||
*/
|
*/
|
||||||
public static DataSize ofMegabytes(long megabytes) {
|
public static DataSize ofMegabytes(long megabytes) {
|
||||||
return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB));
|
return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得对应gigabytes的{@link DataSize}
|
* 获得对应gigabytes的DataSize
|
||||||
*
|
*
|
||||||
* @param gigabytes gigabytes大小,可正可负
|
* @param gigabytes gigabytes大小,可正可负
|
||||||
* @return a {@link DataSize}
|
* @return a DataSize
|
||||||
*/
|
*/
|
||||||
public static DataSize ofGigabytes(long gigabytes) {
|
public static DataSize ofGigabytes(long gigabytes) {
|
||||||
return new DataSize(Math.multiplyExact(gigabytes, BYTES_PER_GB));
|
return new DataSize(Math.multiplyExact(gigabytes, BYTES_PER_GB));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得对应terabytes的{@link DataSize}
|
* 获得对应terabytes的DataSize
|
||||||
*
|
*
|
||||||
* @param terabytes terabytes大小,可正可负
|
* @param terabytes terabytes大小,可正可负
|
||||||
* @return a {@link DataSize}
|
* @return a DataSize
|
||||||
*/
|
*/
|
||||||
public static DataSize ofTerabytes(long terabytes) {
|
public static DataSize ofTerabytes(long terabytes) {
|
||||||
return new DataSize(Math.multiplyExact(terabytes, BYTES_PER_TB));
|
return new DataSize(Math.multiplyExact(terabytes, BYTES_PER_TB));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得指定{@link DataUnit}对应的{@link DataSize}
|
* 获得指定{@link DataUnit}对应的DataSize
|
||||||
*
|
*
|
||||||
* @param amount 大小
|
* @param amount 大小
|
||||||
* @param unit 数据大小单位,null表示默认的BYTES
|
* @param unit 数据大小单位,null表示默认的BYTES
|
||||||
* @return {@link DataSize}
|
* @return DataSize
|
||||||
*/
|
*/
|
||||||
public static DataSize of(long amount, DataUnit unit) {
|
public static DataSize of(long amount, DataUnit unit) {
|
||||||
if(null == unit){
|
if(null == unit){
|
||||||
@ -132,11 +132,12 @@ public final class DataSize implements Comparable<DataSize> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得指定{@link DataUnit}对应的{@link DataSize}
|
* 获得指定{@link DataUnit}对应的DataSize
|
||||||
*
|
*
|
||||||
* @param amount 大小
|
* @param amount 大小
|
||||||
* @param unit 数据大小单位,null表示默认的BYTES
|
* @param unit 数据大小单位,null表示默认的BYTES
|
||||||
* @return {@link DataSize}
|
* @return DataSize
|
||||||
|
* @since 5.4.5
|
||||||
*/
|
*/
|
||||||
public static DataSize of(BigDecimal amount, DataUnit unit) {
|
public static DataSize of(BigDecimal amount, DataUnit unit) {
|
||||||
if(null == unit){
|
if(null == unit){
|
||||||
@ -146,7 +147,7 @@ public final class DataSize implements Comparable<DataSize> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取指定数据大小文本对应的{@link DataSize}对象,如果无单位指定,默认获取{@link DataUnit#BYTES}
|
* 获取指定数据大小文本对应的DataSize对象,如果无单位指定,默认获取{@link DataUnit#BYTES}
|
||||||
* <p>
|
* <p>
|
||||||
* 例如:
|
* 例如:
|
||||||
* <pre>
|
* <pre>
|
||||||
@ -156,7 +157,7 @@ public final class DataSize implements Comparable<DataSize> {
|
|||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param text the text to parse
|
* @param text the text to parse
|
||||||
* @return the parsed {@link DataSize}
|
* @return the parsed DataSize
|
||||||
* @see #parse(CharSequence, DataUnit)
|
* @see #parse(CharSequence, DataUnit)
|
||||||
*/
|
*/
|
||||||
public static DataSize parse(CharSequence text) {
|
public static DataSize parse(CharSequence text) {
|
||||||
@ -164,7 +165,7 @@ public final class DataSize implements Comparable<DataSize> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtain a {@link DataSize} from a text string such as {@code 12MB} using
|
* Obtain a DataSize from a text string such as {@code 12MB} using
|
||||||
* the specified default {@link DataUnit} if no unit is specified.
|
* the specified default {@link DataUnit} if no unit is specified.
|
||||||
* <p>
|
* <p>
|
||||||
* The string starts with a number followed optionally by a unit matching one of the
|
* The string starts with a number followed optionally by a unit matching one of the
|
||||||
@ -179,21 +180,16 @@ public final class DataSize implements Comparable<DataSize> {
|
|||||||
*
|
*
|
||||||
* @param text the text to parse
|
* @param text the text to parse
|
||||||
* @param defaultUnit 默认的数据单位
|
* @param defaultUnit 默认的数据单位
|
||||||
* @return the parsed {@link DataSize}
|
* @return the parsed DataSize
|
||||||
*/
|
*/
|
||||||
public static DataSize parse(CharSequence text, DataUnit defaultUnit) {
|
public static DataSize parse(CharSequence text, DataUnit defaultUnit) {
|
||||||
Assert.notNull(text, "Text must not be null");
|
Assert.notNull(text, "Text must not be null");
|
||||||
try {
|
try {
|
||||||
Matcher matcher = PATTERN.matcher(text);
|
final Matcher matcher = PATTERN.matcher(text);
|
||||||
Assert.state(matcher.matches(), "Does not match data size pattern");
|
Assert.state(matcher.matches(), "Does not match data size pattern");
|
||||||
DataUnit unit = determineDataUnit(matcher.group(3), defaultUnit);
|
|
||||||
String value = matcher.group(1);
|
final DataUnit unit = determineDataUnit(matcher.group(3), defaultUnit);
|
||||||
if (value.indexOf(".") > -1) {
|
return DataSize.of(new BigDecimal(matcher.group(1)), unit);
|
||||||
return DataSize.of(new BigDecimal(value), unit);
|
|
||||||
} else {
|
|
||||||
long amount = Long.parseLong(matcher.group(1));
|
|
||||||
return DataSize.of(amount, unit);
|
|
||||||
}
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new IllegalArgumentException("'" + text + "' is not a valid data size", ex);
|
throw new IllegalArgumentException("'" + text + "' is not a valid data size", ex);
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,12 @@ public class DataSizeUtilTest {
|
|||||||
parse = DataSizeUtil.parse("3.1MB");
|
parse = DataSizeUtil.parse("3.1MB");
|
||||||
Assert.assertEquals(3250585, parse);
|
Assert.assertEquals(3250585, parse);
|
||||||
|
|
||||||
|
parse = DataSizeUtil.parse("-3.1MB");
|
||||||
|
Assert.assertEquals(-3250585, parse);
|
||||||
|
|
||||||
|
parse = DataSizeUtil.parse("+3.1MB");
|
||||||
|
Assert.assertEquals(3250585, parse);
|
||||||
|
|
||||||
parse = DataSizeUtil.parse("3.1mb");
|
parse = DataSizeUtil.parse("3.1mb");
|
||||||
Assert.assertEquals(3250585, parse);
|
Assert.assertEquals(3250585, parse);
|
||||||
|
|
||||||
@ -35,7 +41,7 @@ public class DataSizeUtilTest {
|
|||||||
Assert.assertEquals(3, parse);
|
Assert.assertEquals(3, parse);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
parse = DataSizeUtil.parse("3.1.3");
|
DataSizeUtil.parse("3.1.3");
|
||||||
} catch (IllegalArgumentException ie) {
|
} catch (IllegalArgumentException ie) {
|
||||||
Assert.assertEquals("'3.1.3' is not a valid data size", ie.getMessage());
|
Assert.assertEquals("'3.1.3' is not a valid data size", ie.getMessage());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user