Skip to content
Closed
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,8 @@ public CellDataTypeEnum supportExcelTypeKey() {
@Override
public Byte convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return cellData.getNumberValue().byteValue();
return NumberUtils.narrowInRange(cellData.getNumberValue(), Byte.MIN_VALUE, Byte.MAX_VALUE)
.byteValueExact();
}

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

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ public static WriteCellData<?> formatToCellData(Number num, ExcelContentProperty
return cellData;
}

/**
* Narrow a cell number to an integral value in the closed range [{@code minValue}, {@code maxValue}]. The
* fractional part is truncated toward zero, matching the previous narrowing behavior; a value whose
* truncated integer part falls outside the range throws {@link ArithmeticException} instead of silently
* wrapping around.
*
* @param value the cell number
* @param minValue the inclusive lower bound of the target type
* @param maxValue the inclusive upper bound of the target type
* @return the truncated value, guaranteed to be integral and within the range
*/
public static BigDecimal narrowInRange(BigDecimal value, long minValue, long maxValue) {
BigDecimal truncated = value.setScale(0, RoundingMode.DOWN);
if (truncated.compareTo(BigDecimal.valueOf(minValue)) < 0
|| truncated.compareTo(BigDecimal.valueOf(maxValue)) > 0) {
throw new ArithmeticException(
truncated + " is out of the required range [" + minValue + ", " + maxValue + "]");
}
return truncated;
}

/**
* parse
*
Expand All @@ -118,9 +139,12 @@ public static WriteCellData<?> formatToCellData(Number num, ExcelContentProperty
*/
public static Short parseShort(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string).shortValue();
return narrowInRange(new BigDecimal(string), Short.MIN_VALUE, Short.MAX_VALUE)
.shortValueExact();
}
return parse(string, contentProperty).shortValue();
return narrowInRange(
new BigDecimal(parse(string, contentProperty).toString()), Short.MIN_VALUE, Short.MAX_VALUE)
.shortValueExact();
}

/**
Expand All @@ -132,9 +156,11 @@ public static Short parseShort(String string, ExcelContentProperty contentProper
*/
public static Long parseLong(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string).longValue();
return narrowInRange(new BigDecimal(string), Long.MIN_VALUE, Long.MAX_VALUE)
.longValueExact();
}
return parse(string, contentProperty).longValue();
return narrowInRange(new BigDecimal(parse(string, contentProperty).toString()), Long.MIN_VALUE, Long.MAX_VALUE)
.longValueExact();
}

/**
Expand All @@ -146,9 +172,12 @@ public static Long parseLong(String string, ExcelContentProperty contentProperty
*/
public static Integer parseInteger(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string).intValue();
return narrowInRange(new BigDecimal(string), Integer.MIN_VALUE, Integer.MAX_VALUE)
.intValueExact();
}
return parse(string, contentProperty).intValue();
return narrowInRange(
new BigDecimal(parse(string, contentProperty).toString()), Integer.MIN_VALUE, Integer.MAX_VALUE)
.intValueExact();
}

/**
Expand Down Expand Up @@ -189,9 +218,11 @@ public static BigDecimal parseBigDecimal(String string, ExcelContentProperty con
*/
public static Byte parseByte(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string).byteValue();
return narrowInRange(new BigDecimal(string), Byte.MIN_VALUE, Byte.MAX_VALUE)
.byteValueExact();
}
return parse(string, contentProperty).byteValue();
return narrowInRange(new BigDecimal(parse(string, contentProperty).toString()), Byte.MIN_VALUE, Byte.MAX_VALUE)
.byteValueExact();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.sheet.converters.byteconverter;

import java.math.BigDecimal;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.testkit.Tags;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

/**
* Tests {@link ByteNumberConverter}.
*/
@Tag(Tags.UNIT)
class ByteNumberConverterTest {

private final ByteNumberConverter converter = new ByteNumberConverter();

@Test
void convertToJavaDataAcceptsInRangeNumber() {
Byte actual = converter.convertToJavaData(new ReadCellData<>(new BigDecimal("100")), null, null);
Assertions.assertEquals((byte) 100, actual);
}

@Test
void convertToJavaDataTruncatesFractionLikeBefore() {
Byte actual = converter.convertToJavaData(new ReadCellData<>(new BigDecimal("12.9")), null, null);
Assertions.assertEquals((byte) 12, actual);
}

@Test
void convertToJavaDataAcceptsBoundaryValues() {
Assertions.assertEquals(
Byte.MAX_VALUE, converter.convertToJavaData(new ReadCellData<>(new BigDecimal("127")), null, null));
Assertions.assertEquals(
Byte.MIN_VALUE, converter.convertToJavaData(new ReadCellData<>(new BigDecimal("-128")), null, null));
}

@Test
void convertToJavaDataRejectsOutOfRangeNumber() {
Assertions.assertThrows(
ArithmeticException.class,
() -> converter.convertToJavaData(new ReadCellData<>(new BigDecimal("300")), null, null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.sheet.converters.integer;

import java.math.BigDecimal;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.testkit.Tags;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

/**
* Tests {@link IntegerNumberConverter}.
*/
@Tag(Tags.UNIT)
class IntegerNumberConverterTest {

private final IntegerNumberConverter converter = new IntegerNumberConverter();

@Test
void convertToJavaDataAcceptsInRangeNumber() {
Integer actual = converter.convertToJavaData(new ReadCellData<>(new BigDecimal("100")), null, null);
Assertions.assertEquals(100, actual);
}

@Test
void convertToJavaDataTruncatesFractionLikeBefore() {
Integer actual = converter.convertToJavaData(new ReadCellData<>(new BigDecimal("123.9")), null, null);
Assertions.assertEquals(123, actual);
}

@Test
void convertToJavaDataAcceptsBoundaryValues() {
Assertions.assertEquals(
Integer.MAX_VALUE,
converter.convertToJavaData(new ReadCellData<>(new BigDecimal("2147483647")), null, null));
Assertions.assertEquals(
Integer.MIN_VALUE,
converter.convertToJavaData(new ReadCellData<>(new BigDecimal("-2147483648")), null, null));
}

@Test
void convertToJavaDataRejectsOutOfRangeNumber() {
Assertions.assertThrows(
ArithmeticException.class,
() -> converter.convertToJavaData(new ReadCellData<>(new BigDecimal("13800138000")), null, null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.sheet.converters.longconverter;

import java.math.BigDecimal;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.testkit.Tags;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

/**
* Tests {@link LongNumberConverter}.
*/
@Tag(Tags.UNIT)
class LongNumberConverterTest {

private final LongNumberConverter converter = new LongNumberConverter();

@Test
void convertToJavaDataAcceptsInRangeNumber() {
Long actual = converter.convertToJavaData(new ReadCellData<>(new BigDecimal("13800138000")), null, null);
Assertions.assertEquals(13800138000L, actual);
}

@Test
void convertToJavaDataTruncatesFractionLikeBefore() {
Long actual = converter.convertToJavaData(new ReadCellData<>(new BigDecimal("123.9")), null, null);
Assertions.assertEquals(123L, actual);
}

@Test
void convertToJavaDataAcceptsBoundaryValues() {
Assertions.assertEquals(
Long.MAX_VALUE,
converter.convertToJavaData(new ReadCellData<>(new BigDecimal("9223372036854775807")), null, null));
Assertions.assertEquals(
Long.MIN_VALUE,
converter.convertToJavaData(new ReadCellData<>(new BigDecimal("-9223372036854775808")), null, null));
}

@Test
void convertToJavaDataRejectsOutOfRangeNumber() {
Assertions.assertThrows(
ArithmeticException.class,
() -> converter.convertToJavaData(
new ReadCellData<>(new BigDecimal("9223372036854775808")), null, null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.sheet.converters.shortconverter;

import java.math.BigDecimal;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.testkit.Tags;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

/**
* Tests {@link ShortNumberConverter}.
*/
@Tag(Tags.UNIT)
class ShortNumberConverterTest {

private final ShortNumberConverter converter = new ShortNumberConverter();

@Test
void convertToJavaDataAcceptsInRangeNumber() {
Short actual = converter.convertToJavaData(new ReadCellData<>(new BigDecimal("100")), null, null);
Assertions.assertEquals((short) 100, actual);
}

@Test
void convertToJavaDataTruncatesFractionLikeBefore() {
Short actual = converter.convertToJavaData(new ReadCellData<>(new BigDecimal("123.9")), null, null);
Assertions.assertEquals((short) 123, actual);
}

@Test
void convertToJavaDataAcceptsBoundaryValues() {
Assertions.assertEquals(
Short.MAX_VALUE, converter.convertToJavaData(new ReadCellData<>(new BigDecimal("32767")), null, null));
Assertions.assertEquals(
Short.MIN_VALUE, converter.convertToJavaData(new ReadCellData<>(new BigDecimal("-32768")), null, null));
}

@Test
void convertToJavaDataRejectsOutOfRangeNumber() {
Assertions.assertThrows(
ArithmeticException.class,
() -> converter.convertToJavaData(new ReadCellData<>(new BigDecimal("40000")), null, null));
}
}
Loading