Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public CellDataTypeEnum supportExcelTypeKey() {
@Override
public Byte convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return cellData.getNumberValue().byteValue();
return NumberUtils.toByte(cellData.getNumberValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public CellDataTypeEnum supportExcelTypeKey() {
@Override
public Integer convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return cellData.getNumberValue().intValue();
return NumberUtils.toInt(cellData.getNumberValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public CellDataTypeEnum supportExcelTypeKey() {
@Override
public Long convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return cellData.getNumberValue().longValue();
return NumberUtils.toLong(cellData.getNumberValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public CellDataTypeEnum supportExcelTypeKey() {
@Override
public Short convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return cellData.getNumberValue().shortValue();
return NumberUtils.toShort(cellData.getNumberValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ public static WriteCellData<?> formatToCellData(Number num, ExcelContentProperty
* @return
*/
public static Short parseShort(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string).shortValue();
}
return parse(string, contentProperty).shortValue();
return toShort(parseBigDecimal(string, contentProperty));
}

/**
Expand All @@ -131,10 +128,7 @@ public static Short parseShort(String string, ExcelContentProperty contentProper
* @return
*/
public static Long parseLong(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string).longValue();
}
return parse(string, contentProperty).longValue();
return toLong(parseBigDecimal(string, contentProperty));
}

/**
Expand All @@ -145,10 +139,7 @@ public static Long parseLong(String string, ExcelContentProperty contentProperty
* @return An integer converted from a string
*/
public static Integer parseInteger(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string).intValue();
}
return parse(string, contentProperty).intValue();
return toInt(parseBigDecimal(string, contentProperty));
}

/**
Expand Down Expand Up @@ -188,10 +179,50 @@ public static BigDecimal parseBigDecimal(String string, ExcelContentProperty con
* @return
*/
public static Byte parseByte(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string).byteValue();
return toByte(parseBigDecimal(string, contentProperty));
}

/**
* Truncates towards zero like {@link BigDecimal#byteValue()}, but throws instead of wrapping around.
*
* @throws ArithmeticException if the integral part is out of range for {@code byte}
*/
public static byte toByte(BigDecimal value) {
return checkRange(value, IntegralRange.BYTE).byteValue();
}

/**
* Truncates towards zero like {@link BigDecimal#shortValue()}, but throws instead of wrapping around.
*
* @throws ArithmeticException if the integral part is out of range for {@code short}
*/
public static short toShort(BigDecimal value) {
return checkRange(value, IntegralRange.SHORT).shortValue();
}

/**
* Truncates towards zero like {@link BigDecimal#intValue()}, but throws instead of wrapping around.
*
* @throws ArithmeticException if the integral part is out of range for {@code int}
*/
public static int toInt(BigDecimal value) {
return checkRange(value, IntegralRange.INTEGER).intValue();
}

/**
* Truncates towards zero like {@link BigDecimal#longValue()}, but throws instead of wrapping around.
*
* @throws ArithmeticException if the integral part is out of range for {@code long}
*/
public static long toLong(BigDecimal value) {
return checkRange(value, IntegralRange.LONG).longValue();
}

private static BigDecimal checkRange(BigDecimal value, IntegralRange range) {
if (value.compareTo(range.lower) <= 0 || value.compareTo(range.upper) >= 0) {
throw new ArithmeticException(value + " is out of range for " + range.type);
}
return parse(string, contentProperty).byteValue();
return value;
}

/**
Expand Down Expand Up @@ -257,4 +288,21 @@ private static DecimalFormat getCacheDecimalFormat(String format, RoundingMode r
public static void removeThreadLocalCache() {
DECIMAL_FORMAT_THREAD_LOCAL.remove();
}

private enum IntegralRange {
BYTE(Byte.MIN_VALUE, Byte.MAX_VALUE, "Byte"),
SHORT(Short.MIN_VALUE, Short.MAX_VALUE, "Short"),
INTEGER(Integer.MIN_VALUE, Integer.MAX_VALUE, "Integer"),
LONG(Long.MIN_VALUE, Long.MAX_VALUE, "Long");

private final BigDecimal lower;
private final BigDecimal upper;
private final String type;

IntegralRange(long min, long max, String type) {
this.lower = BigDecimal.valueOf(min).subtract(BigDecimal.ONE);
this.upper = BigDecimal.valueOf(max).add(BigDecimal.ONE);
this.type = type;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ void integerConverters() throws Exception {

IntegerNumberConverter numberConverter = new IntegerNumberConverter();
assertEquals(42, toJava(numberConverter, new ReadCellData<>(new BigDecimal("42.9"))));
Assertions.assertThrows(
ArithmeticException.class,
() -> toJava(numberConverter, new ReadCellData<>(new BigDecimal("2147483648"))));
WriteConverterContext<Integer> writeContext = new WriteConverterContext<>();
writeContext.setValue(42);
WriteCellData<?> numberCell = numberConverter.convertToExcelData(writeContext);
Expand All @@ -191,6 +194,9 @@ void longConverters() throws Exception {

LongNumberConverter numberConverter = new LongNumberConverter();
assertEquals(99L, toJava(numberConverter, new ReadCellData<>(new BigDecimal("99.1"))));
Assertions.assertThrows(
ArithmeticException.class,
() -> toJava(numberConverter, new ReadCellData<>(new BigDecimal("9223372036854775808"))));
WriteConverterContext<Long> writeContext = new WriteConverterContext<>();
writeContext.setValue(99L);
assertEquals(
Expand All @@ -215,6 +221,8 @@ void shortConverters() throws Exception {

ShortNumberConverter numberConverter = new ShortNumberConverter();
assertEquals((short) 7, toJava(numberConverter, new ReadCellData<>(new BigDecimal("7.8"))));
Assertions.assertThrows(
ArithmeticException.class, () -> toJava(numberConverter, new ReadCellData<>(new BigDecimal("32768"))));
WriteConverterContext<Short> writeContext = new WriteConverterContext<>();
writeContext.setValue((short) 7);
assertEquals(
Expand All @@ -239,6 +247,8 @@ void byteConverters() throws Exception {

ByteNumberConverter numberConverter = new ByteNumberConverter();
assertEquals((byte) 3, toJava(numberConverter, new ReadCellData<>(new BigDecimal("3.9"))));
Assertions.assertThrows(
ArithmeticException.class, () -> toJava(numberConverter, new ReadCellData<>(new BigDecimal("128"))));
assertEquals(0, toExcel(numberConverter, (byte) 3).getNumberValue().compareTo(new BigDecimal("3")));

ByteStringConverter stringConverter = new ByteStringConverter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,68 @@ void test_parseShort_noFormat() throws ParseException {
Assertions.assertEquals((short) 789, resultEmptyFormat);
}

@Test
void test_parseInteger_noFormat_truncatesFraction() throws ParseException {
Assertions.assertEquals(1, NumberUtils.parseInteger("1.9", null));
Assertions.assertEquals(-1, NumberUtils.parseInteger("-1.9", null));
}

@Test
void test_parseIntegral_noFormat_outOfRange() {
Assertions.assertThrows(ArithmeticException.class, () -> NumberUtils.parseByte("128", null));
Assertions.assertThrows(ArithmeticException.class, () -> NumberUtils.parseShort("32768", null));
Assertions.assertThrows(ArithmeticException.class, () -> NumberUtils.parseInteger("13800138000", null));
Assertions.assertThrows(ArithmeticException.class, () -> NumberUtils.parseLong("9223372036854775808", null));
}

@Test
void test_parseIntegral_withFormat_outOfRange() {
Mockito.when(contentProperty.getNumberFormatProperty()).thenReturn(numberFormatProperty);
Mockito.when(numberFormatProperty.getFormat()).thenReturn("#,###");
Mockito.when(numberFormatProperty.getRoundingMode()).thenReturn(RoundingMode.HALF_UP);

Assertions.assertThrows(ArithmeticException.class, () -> NumberUtils.parseByte("128", contentProperty));
Assertions.assertThrows(ArithmeticException.class, () -> NumberUtils.parseShort("32,768", contentProperty));
Assertions.assertThrows(
ArithmeticException.class, () -> NumberUtils.parseInteger("13,800,138,000", contentProperty));
Assertions.assertThrows(
ArithmeticException.class, () -> NumberUtils.parseLong("9,223,372,036,854,775,808", contentProperty));
}

@Test
void test_toByte_bounds() {
Assertions.assertEquals(Byte.MAX_VALUE, NumberUtils.toByte(new BigDecimal("127.9")));
Assertions.assertEquals(Byte.MIN_VALUE, NumberUtils.toByte(new BigDecimal("-128.9")));
Assertions.assertThrows(ArithmeticException.class, () -> NumberUtils.toByte(new BigDecimal("128")));
Assertions.assertThrows(ArithmeticException.class, () -> NumberUtils.toByte(new BigDecimal("-129")));
}

@Test
void test_toShort_bounds() {
Assertions.assertEquals(Short.MAX_VALUE, NumberUtils.toShort(new BigDecimal("32767.9")));
Assertions.assertEquals(Short.MIN_VALUE, NumberUtils.toShort(new BigDecimal("-32768.9")));
Assertions.assertThrows(ArithmeticException.class, () -> NumberUtils.toShort(new BigDecimal("32768")));
Assertions.assertThrows(ArithmeticException.class, () -> NumberUtils.toShort(new BigDecimal("-32769")));
}

@Test
void test_toInt_bounds() {
Assertions.assertEquals(Integer.MAX_VALUE, NumberUtils.toInt(new BigDecimal("2147483647.9")));
Assertions.assertEquals(Integer.MIN_VALUE, NumberUtils.toInt(new BigDecimal("-2147483648.9")));
Assertions.assertThrows(ArithmeticException.class, () -> NumberUtils.toInt(new BigDecimal("2147483648")));
Assertions.assertThrows(ArithmeticException.class, () -> NumberUtils.toInt(new BigDecimal("-2147483649")));
}

@Test
void test_toLong_bounds() {
Assertions.assertEquals(Long.MAX_VALUE, NumberUtils.toLong(new BigDecimal("9223372036854775807.9")));
Assertions.assertEquals(Long.MIN_VALUE, NumberUtils.toLong(new BigDecimal("-9223372036854775808.9")));
Assertions.assertThrows(
ArithmeticException.class, () -> NumberUtils.toLong(new BigDecimal("9223372036854775808")));
Assertions.assertThrows(
ArithmeticException.class, () -> NumberUtils.toLong(new BigDecimal("-9223372036854775809")));
}

@Test
void test_parse_Error() {
Mockito.when(contentProperty.getNumberFormatProperty()).thenReturn(numberFormatProperty);
Expand Down
Loading