forked from plusone/plusone-commons
Compare commits
2 Commits
feature/Da
...
feature/nu
Author | SHA1 | Date | |
---|---|---|---|
825a99dab6 | |||
f3218420ed |
@@ -27,11 +27,11 @@ import java.time.Year;
|
|||||||
import java.time.YearMonth;
|
import java.time.YearMonth;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
|
import java.time.chrono.IsoChronology;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import com.google.common.collect.BoundType;
|
|
||||||
import com.google.common.collect.Range;
|
import com.google.common.collect.Range;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.time.Quarter;
|
import xyz.zhouxy.plusone.commons.time.Quarter;
|
||||||
@@ -673,44 +673,6 @@ public class DateTimeTools {
|
|||||||
return Range.closedOpen(date.atStartOfDay(zone), startOfNextDate(date, zone));
|
return Range.closedOpen(date.atStartOfDay(zone), startOfNextDate(date, zone));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 将指定日期范围转为日期时间范围
|
|
||||||
*
|
|
||||||
* @param dateRange 日期范围
|
|
||||||
* @return 对应的日期时间范围
|
|
||||||
*/
|
|
||||||
public static Range<LocalDateTime> toDateTimeRange(Range<LocalDate> dateRange) {
|
|
||||||
BoundType lowerBoundType = dateRange.lowerBoundType();
|
|
||||||
LocalDateTime lowerEndpoint = lowerBoundType == BoundType.CLOSED
|
|
||||||
? dateRange.lowerEndpoint().atStartOfDay()
|
|
||||||
: dateRange.lowerEndpoint().plusDays(1).atStartOfDay();
|
|
||||||
BoundType upperBoundType = dateRange.upperBoundType();
|
|
||||||
LocalDateTime upperEndpoint = upperBoundType == BoundType.CLOSED
|
|
||||||
? dateRange.upperEndpoint().plusDays(1).atStartOfDay()
|
|
||||||
: dateRange.upperEndpoint().atStartOfDay();
|
|
||||||
|
|
||||||
return Range.closedOpen(lowerEndpoint, upperEndpoint);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将指定日期范围转为日期时间范围
|
|
||||||
*
|
|
||||||
* @param dateRange 日期范围
|
|
||||||
* @return 对应的日期时间范围
|
|
||||||
*/
|
|
||||||
public static Range<ZonedDateTime> toDateTimeRange(Range<LocalDate> dateRange, ZoneId zone) {
|
|
||||||
BoundType lowerBoundType = dateRange.lowerBoundType();
|
|
||||||
ZonedDateTime lowerEndpoint = lowerBoundType == BoundType.CLOSED
|
|
||||||
? dateRange.lowerEndpoint().atStartOfDay(zone)
|
|
||||||
: dateRange.lowerEndpoint().plusDays(1).atStartOfDay(zone);
|
|
||||||
BoundType upperBoundType = dateRange.upperBoundType();
|
|
||||||
ZonedDateTime upperEndpoint = upperBoundType == BoundType.CLOSED
|
|
||||||
? dateRange.upperEndpoint().plusDays(1).atStartOfDay(zone)
|
|
||||||
: dateRange.upperEndpoint().atStartOfDay(zone);
|
|
||||||
|
|
||||||
return Range.closedOpen(lowerEndpoint, upperEndpoint);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断指定年份是否为闰年
|
* 判断指定年份是否为闰年
|
||||||
*
|
*
|
||||||
@@ -718,7 +680,7 @@ public class DateTimeTools {
|
|||||||
* @return 指定年份是否为闰年
|
* @return 指定年份是否为闰年
|
||||||
*/
|
*/
|
||||||
public static boolean isLeapYear(int year) {
|
public static boolean isLeapYear(int year) {
|
||||||
return Year.isLeap(year);
|
return IsoChronology.INSTANCE.isLeapYear(year);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================================
|
// ================================
|
||||||
|
@@ -29,7 +29,9 @@ import javax.annotation.Nullable;
|
|||||||
*/
|
*/
|
||||||
public class Numbers {
|
public class Numbers {
|
||||||
|
|
||||||
|
// ================================
|
||||||
// #region - sum
|
// #region - sum
|
||||||
|
// ================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 求和
|
* 求和
|
||||||
@@ -131,9 +133,13 @@ public class Numbers {
|
|||||||
return BigDecimals.sum(numbers);
|
return BigDecimals.sum(numbers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
// #endregion
|
// #endregion
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
// #region - nullToZero
|
// #region - nullToZero
|
||||||
|
// ================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将 {@code null} 转换为 {@code 0}
|
* 将 {@code null} 转换为 {@code 0}
|
||||||
@@ -217,7 +223,122 @@ public class Numbers {
|
|||||||
return BigDecimals.nullToZero(val);
|
return BigDecimals.nullToZero(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
// #endregion
|
// ================================
|
||||||
|
// #endregion - nullToZero
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - parse
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将字符串转为对应 {@link Short},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||||
|
*
|
||||||
|
* @param str 要转换的字符串
|
||||||
|
* @param defaultValue 默认值
|
||||||
|
* @return 转换结果
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public static Short parseShort(@Nullable String str, @Nullable Short defaultValue) {
|
||||||
|
if (StringTools.isBlank(str)) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return Short.parseShort(str);
|
||||||
|
}
|
||||||
|
catch (NumberFormatException ignore) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将字符串转为 {@link Integer},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||||
|
*
|
||||||
|
* @param str 要转换的字符串
|
||||||
|
* @param defaultValue 默认值
|
||||||
|
* @return 转换结果
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public static Integer parseInteger(@Nullable String str, @Nullable Integer defaultValue) {
|
||||||
|
if (StringTools.isBlank(str)) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return Integer.parseInt(str);
|
||||||
|
}
|
||||||
|
catch (NumberFormatException ignore) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将字符串转为 {@link Long},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||||
|
*
|
||||||
|
* @param str 要转换的字符串
|
||||||
|
* @param defaultValue 默认值
|
||||||
|
* @return 转换结果
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public static Long parseLong(@Nullable String str, @Nullable Long defaultValue) {
|
||||||
|
if (StringTools.isBlank(str)) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return Long.parseLong(str);
|
||||||
|
}
|
||||||
|
catch (NumberFormatException ignore) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将字符串转为 {@link Float},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||||
|
*
|
||||||
|
* @param str 要转换的字符串
|
||||||
|
* @param defaultValue 默认值
|
||||||
|
* @return 转换结果
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public static Float parseFloat(@Nullable String str, @Nullable Float defaultValue) {
|
||||||
|
if (StringTools.isBlank(str)) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return Float.parseFloat(str);
|
||||||
|
}
|
||||||
|
catch (NumberFormatException ignore) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将字符串转为 {@link Double},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||||
|
*
|
||||||
|
* @param str 要转换的字符串
|
||||||
|
* @param defaultValue 默认值
|
||||||
|
* @return 转换结果
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public static Double parseDouble(@Nullable String str, @Nullable Double defaultValue) {
|
||||||
|
if (StringTools.isBlank(str)) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return Double.parseDouble(str);
|
||||||
|
}
|
||||||
|
catch (NumberFormatException ignore) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - parse
|
||||||
|
// ================================
|
||||||
|
|
||||||
private Numbers() {
|
private Numbers() {
|
||||||
throw new IllegalStateException("Utility class");
|
throw new IllegalStateException("Utility class");
|
||||||
|
@@ -43,7 +43,6 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.google.common.collect.BoundType;
|
|
||||||
import com.google.common.collect.Range;
|
import com.google.common.collect.Range;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.time.Quarter;
|
import xyz.zhouxy.plusone.commons.time.Quarter;
|
||||||
@@ -84,6 +83,24 @@ class DateTimeToolsTests {
|
|||||||
CALENDAR.setTime(DATE);
|
CALENDAR.setTime(DATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Joda
|
||||||
|
static final org.joda.time.LocalDateTime JODA_LOCAL_DATE_TIME
|
||||||
|
= new org.joda.time.LocalDateTime(2024, 12, 29, 12, 58, 30, 333);
|
||||||
|
static final org.joda.time.LocalDate JODA_LOCAL_DATE = JODA_LOCAL_DATE_TIME.toLocalDate();
|
||||||
|
static final org.joda.time.LocalTime JODA_LOCAL_TIME = JODA_LOCAL_DATE_TIME.toLocalTime();
|
||||||
|
|
||||||
|
// Joda - 2024-12-29 12:58:30.333 SystemDefaultZone
|
||||||
|
static final org.joda.time.DateTimeZone JODA_SYS_ZONE = org.joda.time.DateTimeZone.getDefault();
|
||||||
|
static final org.joda.time.DateTime JODA_DATE_TIME_WITH_SYS_ZONE = JODA_LOCAL_DATE_TIME.toDateTime(JODA_SYS_ZONE);
|
||||||
|
static final org.joda.time.Instant JODA_INSTANT_WITH_SYS_ZONE = JODA_DATE_TIME_WITH_SYS_ZONE.toInstant();
|
||||||
|
static final long JODA_INSTANT_MILLIS = JODA_INSTANT_WITH_SYS_ZONE.getMillis();
|
||||||
|
|
||||||
|
// Joda - 2024-12-29 12:58:30.333 GMT+04:00
|
||||||
|
static final org.joda.time.DateTimeZone JODA_ZONE = org.joda.time.DateTimeZone.forID("GMT+04:00");
|
||||||
|
static final org.joda.time.DateTime JODA_DATE_TIME = JODA_LOCAL_DATE_TIME.toDateTime(JODA_ZONE);
|
||||||
|
static final org.joda.time.Instant JODA_INSTANT = JODA_DATE_TIME.toInstant();
|
||||||
|
static final long JODA_MILLIS = JODA_INSTANT.getMillis();
|
||||||
|
|
||||||
// ================================
|
// ================================
|
||||||
// #region - toDate
|
// #region - toDate
|
||||||
// ================================
|
// ================================
|
||||||
@@ -92,7 +109,9 @@ class DateTimeToolsTests {
|
|||||||
void toDate_timeMillis() {
|
void toDate_timeMillis() {
|
||||||
assertNotEquals(SYS_DATE, DATE);
|
assertNotEquals(SYS_DATE, DATE);
|
||||||
log.info("SYS_DATE: {}, DATE: {}", SYS_DATE, DATE);
|
log.info("SYS_DATE: {}, DATE: {}", SYS_DATE, DATE);
|
||||||
|
assertEquals(SYS_DATE, JODA_DATE_TIME_WITH_SYS_ZONE.toDate());
|
||||||
assertEquals(SYS_DATE, DateTimeTools.toDate(INSTANT_MILLIS));
|
assertEquals(SYS_DATE, DateTimeTools.toDate(INSTANT_MILLIS));
|
||||||
|
assertEquals(DATE, JODA_DATE_TIME.toDate());
|
||||||
assertEquals(DATE, DateTimeTools.toDate(MILLIS));
|
assertEquals(DATE, DateTimeTools.toDate(MILLIS));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -378,7 +397,7 @@ class DateTimeToolsTests {
|
|||||||
// ================================
|
// ================================
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void toDateTimeRange_specifiedDate() {
|
void startDateTimeRange() {
|
||||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(LOCAL_DATE);
|
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(LOCAL_DATE);
|
||||||
assertEquals(LOCAL_DATE.atStartOfDay(), localDateTimeRange.lowerEndpoint());
|
assertEquals(LOCAL_DATE.atStartOfDay(), localDateTimeRange.lowerEndpoint());
|
||||||
assertEquals(LocalDate.of(2024, 12, 30).atStartOfDay(), localDateTimeRange.upperEndpoint());
|
assertEquals(LocalDate.of(2024, 12, 30).atStartOfDay(), localDateTimeRange.upperEndpoint());
|
||||||
@@ -392,94 +411,6 @@ class DateTimeToolsTests {
|
|||||||
assertFalse(zonedDateTimeRange.contains(LocalDate.of(2024, 12, 30).atStartOfDay().atZone(SYS_ZONE_ID)));
|
assertFalse(zonedDateTimeRange.contains(LocalDate.of(2024, 12, 30).atStartOfDay().atZone(SYS_ZONE_ID)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void toDateTimeRange_dateRange_openRange() {
|
|
||||||
// (2000-01-01..2025-10-01) -> [2000-01-02T00:00..2025-10-01T00:00)
|
|
||||||
Range<LocalDate> dateRange = Range.open(
|
|
||||||
LocalDate.of(2000, 1, 1),
|
|
||||||
LocalDate.of(2025, 10, 1));
|
|
||||||
|
|
||||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(dateRange);
|
|
||||||
assertEquals(BoundType.CLOSED, localDateTimeRange.lowerBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2000, 1, 2, 0, 0), localDateTimeRange.lowerEndpoint());
|
|
||||||
assertEquals(BoundType.OPEN, localDateTimeRange.upperBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2025, 10, 1, 0, 0), localDateTimeRange.upperEndpoint());
|
|
||||||
log.info(localDateTimeRange.toString());
|
|
||||||
|
|
||||||
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(dateRange, ZONE_ID);
|
|
||||||
assertEquals(BoundType.CLOSED, zonedDateTimeRange.lowerBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2000, 1, 2, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
|
||||||
assertEquals(BoundType.OPEN, zonedDateTimeRange.upperBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2025, 10, 1, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
|
||||||
log.info(zonedDateTimeRange.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void toDateTimeRange_dateRange_openClosedRange() {
|
|
||||||
// (2000-01-01..2025-10-01] -> [2025-01-02T00-00..2025-10-02 00:00)
|
|
||||||
Range<LocalDate> dateRange = Range.openClosed(
|
|
||||||
LocalDate.of(2000, 1, 1),
|
|
||||||
LocalDate.of(2025, 10, 1));
|
|
||||||
|
|
||||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(dateRange);
|
|
||||||
assertEquals(BoundType.CLOSED, localDateTimeRange.lowerBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2000, 1, 2, 0, 0), localDateTimeRange.lowerEndpoint());
|
|
||||||
assertEquals(BoundType.OPEN, localDateTimeRange.upperBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2025, 10, 2, 0, 0), localDateTimeRange.upperEndpoint());
|
|
||||||
log.info(localDateTimeRange.toString());
|
|
||||||
|
|
||||||
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(dateRange, ZONE_ID);
|
|
||||||
assertEquals(BoundType.CLOSED, zonedDateTimeRange.lowerBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2000, 1, 2, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
|
||||||
assertEquals(BoundType.OPEN, zonedDateTimeRange.upperBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2025, 10, 2, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
|
||||||
log.info(zonedDateTimeRange.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void toDateTimeRange_dateRange_closedRange() {
|
|
||||||
// [2000-01-01..2025-10-01] -> [2025-01-01T00-00..2025-10-02 00:00)
|
|
||||||
Range<LocalDate> dateRange = Range.closed(
|
|
||||||
LocalDate.of(2000, 1, 1),
|
|
||||||
LocalDate.of(2025, 10, 1));
|
|
||||||
|
|
||||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(dateRange);
|
|
||||||
assertEquals(BoundType.CLOSED, localDateTimeRange.lowerBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2000, 1, 1, 0, 0), localDateTimeRange.lowerEndpoint());
|
|
||||||
assertEquals(BoundType.OPEN, localDateTimeRange.upperBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2025, 10, 2, 0, 0), localDateTimeRange.upperEndpoint());
|
|
||||||
log.info(localDateTimeRange.toString());
|
|
||||||
|
|
||||||
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(dateRange, ZONE_ID);
|
|
||||||
assertEquals(BoundType.CLOSED, zonedDateTimeRange.lowerBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2000, 1, 1, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
|
||||||
assertEquals(BoundType.OPEN, zonedDateTimeRange.upperBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2025, 10, 2, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
|
||||||
log.info(zonedDateTimeRange.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void toDateTimeRange_dateRange_closedOpenRange() {
|
|
||||||
// [2025-01-01..2025-10-01) -> [2025-01-01T00-00..2025-10-01 00:00)
|
|
||||||
Range<LocalDate> dateRange = Range.closedOpen(
|
|
||||||
LocalDate.of(2000, 1, 1),
|
|
||||||
LocalDate.of(2025, 10, 1));
|
|
||||||
|
|
||||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(dateRange);
|
|
||||||
assertEquals(BoundType.CLOSED, localDateTimeRange.lowerBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2000, 1, 1, 0, 0), localDateTimeRange.lowerEndpoint());
|
|
||||||
assertEquals(BoundType.OPEN, localDateTimeRange.upperBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2025, 10, 1, 0, 0), localDateTimeRange.upperEndpoint());
|
|
||||||
log.info(localDateTimeRange.toString());
|
|
||||||
|
|
||||||
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(dateRange, ZONE_ID);
|
|
||||||
assertEquals(BoundType.CLOSED, zonedDateTimeRange.lowerBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2000, 1, 1, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
|
||||||
assertEquals(BoundType.OPEN, zonedDateTimeRange.upperBoundType());
|
|
||||||
assertEquals(LocalDateTime.of(2025, 10, 1, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
|
||||||
log.info(zonedDateTimeRange.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
// ================================
|
// ================================
|
||||||
// #endregion - others
|
// #endregion - others
|
||||||
// ================================
|
// ================================
|
||||||
|
@@ -26,6 +26,7 @@ import java.util.Arrays;
|
|||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
public
|
public
|
||||||
@@ -68,9 +69,16 @@ class NumbersTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sum_BigIntegerArray_ReturnsCorrectSum() {
|
public void sum_BigIntegerArray_ReturnsCorrectSum() {
|
||||||
BigInteger[] numbers = {new BigInteger("1"), new BigInteger("2"), new BigInteger("3")};
|
BigInteger[] numbers = {
|
||||||
|
new BigInteger("1"),
|
||||||
|
new BigInteger("2"),
|
||||||
|
null,
|
||||||
|
new BigInteger("3")
|
||||||
|
};
|
||||||
BigInteger result = Numbers.sum(numbers);
|
BigInteger result = Numbers.sum(numbers);
|
||||||
assertEquals(new BigInteger("6"), result);
|
assertEquals(new BigInteger("6"), result);
|
||||||
|
|
||||||
|
assertEquals(BigInteger.ZERO, Numbers.sum(new BigInteger[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -192,6 +200,100 @@ class NumbersTests {
|
|||||||
assertEquals(BigDecimal.ZERO, result);
|
assertEquals(BigDecimal.ZERO, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link Numbers#parseShort(String, Short)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void parseShort() {
|
||||||
|
assertEquals((short) 12345, Numbers.parseShort("12345", (short) 5));
|
||||||
|
assertEquals((short) 5, Numbers.parseShort("1234.5", (short) 5));
|
||||||
|
assertEquals((short) 5, Numbers.parseShort("", (short) 5));
|
||||||
|
assertEquals((short) 5, Numbers.parseShort(null, (short) 5));
|
||||||
|
|
||||||
|
assertEquals((short) 12345, Numbers.parseShort("12345", null));
|
||||||
|
assertNull(Numbers.parseShort("1234.5", null));
|
||||||
|
assertNull(Numbers.parseShort("", null));
|
||||||
|
assertNull(Numbers.parseShort(null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link Numbers#parseInteger(String, Integer)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void parseInteger() {
|
||||||
|
assertEquals(12345, Numbers.parseInteger("12345", 5));
|
||||||
|
assertEquals(5, Numbers.parseInteger("1234.5", 5));
|
||||||
|
assertEquals(5, Numbers.parseInteger("", 5));
|
||||||
|
assertEquals(5, Numbers.parseInteger(null, 5));
|
||||||
|
|
||||||
|
assertEquals(12345, Numbers.parseInteger("12345", null));
|
||||||
|
assertNull(Numbers.parseInteger("1234.5", null));
|
||||||
|
assertNull(Numbers.parseInteger("", null));
|
||||||
|
assertNull(Numbers.parseInteger(null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link Numbers#parseLong(String, Long)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void parseLong() {
|
||||||
|
assertEquals(12345L, Numbers.parseLong("12345", 5L));
|
||||||
|
assertEquals(5L, Numbers.parseLong("1234.5", 5L));
|
||||||
|
assertEquals(5L, Numbers.parseLong("", 5L));
|
||||||
|
assertEquals(5L, Numbers.parseLong(null, 5L));
|
||||||
|
|
||||||
|
assertEquals(12345L, Numbers.parseLong("12345", null));
|
||||||
|
assertNull(Numbers.parseLong("1234.5", null));
|
||||||
|
assertNull(Numbers.parseLong("", null));
|
||||||
|
assertNull(Numbers.parseLong(null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link Numbers#parseFloat(String, Float)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void parseFloat() {
|
||||||
|
assertEquals(1.2345f, Numbers.parseFloat("1.2345", 5.1f));
|
||||||
|
assertEquals(5.0f, Numbers.parseFloat("a", 5.0f));
|
||||||
|
assertEquals(5.0f, Numbers.parseFloat("-001Z.2345", 5.0f));
|
||||||
|
assertEquals(5.0f, Numbers.parseFloat("+001AB.2345", 5.0f));
|
||||||
|
assertEquals(5.0f, Numbers.parseFloat("001Z.2345", 5.0f));
|
||||||
|
assertEquals(5.0f, Numbers.parseFloat("", 5.0f));
|
||||||
|
assertEquals(5.0f, Numbers.parseFloat(null, 5.0f));
|
||||||
|
|
||||||
|
assertEquals(1.2345f, Numbers.parseFloat("1.2345", null));
|
||||||
|
assertNull(Numbers.parseFloat("a", null));
|
||||||
|
assertNull(Numbers.parseFloat("-001Z.2345", null));
|
||||||
|
assertNull(Numbers.parseFloat("+001AB.2345", null));
|
||||||
|
assertNull(Numbers.parseFloat("001Z.2345", null));
|
||||||
|
assertNull(Numbers.parseFloat("", null));
|
||||||
|
assertNull(Numbers.parseFloat(null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link Numbers#parseDouble(String, Double)}.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void parseDouble() {
|
||||||
|
assertEquals(1.2345d, Numbers.parseDouble("1.2345", 5.1d));
|
||||||
|
assertEquals(5.0d, Numbers.parseDouble("a", 5.0d));
|
||||||
|
assertEquals(1.2345d, Numbers.parseDouble("001.2345", 5.1d));
|
||||||
|
assertEquals(-1.2345d, Numbers.parseDouble("-001.2345", 5.1d));
|
||||||
|
assertEquals(1.2345d, Numbers.parseDouble("+001.2345", 5.1d));
|
||||||
|
assertEquals(0d, Numbers.parseDouble("000.00", 5.1d));
|
||||||
|
assertEquals(5.1d, Numbers.parseDouble("", 5.1d));
|
||||||
|
assertEquals(5.1d, Numbers.parseDouble((String) null, 5.1d));
|
||||||
|
|
||||||
|
assertEquals(1.2345d, Numbers.parseDouble("1.2345", null));
|
||||||
|
assertEquals(null, Numbers.parseDouble("a", null));
|
||||||
|
assertEquals(1.2345d, Numbers.parseDouble("001.2345", null));
|
||||||
|
assertEquals(-1.2345d, Numbers.parseDouble("-001.2345", null));
|
||||||
|
assertEquals(1.2345d, Numbers.parseDouble("+001.2345", null));
|
||||||
|
assertEquals(0d, Numbers.parseDouble("000.00", null));
|
||||||
|
assertEquals(null, Numbers.parseDouble("", null));
|
||||||
|
assertEquals(null, Numbers.parseDouble((String) null, null));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
|
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
|
||||||
Constructor<?>[] constructors = Numbers.class.getDeclaredConstructors();
|
Constructor<?>[] constructors = Numbers.class.getDeclaredConstructors();
|
||||||
|
Reference in New Issue
Block a user