Copilot commented on code in PR #7400: URL: https://github.com/apache/ignite-3/pull/7400#discussion_r2690836658
########## modules/core/src/main/java/org/apache/ignite/internal/util/TupleTypeCastUtils.java: ########## @@ -0,0 +1,352 @@ +/* + * 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.ignite.internal.util; + +import org.apache.ignite.internal.lang.IgniteStringFormatter; +import org.apache.ignite.internal.lang.InternalTuple; +import org.apache.ignite.sql.ColumnType; + +/** + * Helper methods for reading values from {@link InternalTuple} with allowed numeric type conversions. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link ColumnType#INT64} to {@link ColumnType#INT8} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link ColumnType#INT8}).</li> + * <li>{@link ColumnType#FLOAT} to {@link ColumnType#DOUBLE} are always allowed.</li> + * <li>{@link ColumnType#DOUBLE} to {@link ColumnType#FLOAT} with precision checks (may throw {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss).</li> + * </ul> + */ +public class TupleTypeCastUtils { + private static final String TYPE_CAST_ERROR_COLUMN_NAME = "Column with name '{}' has type {} but {} was requested"; + + private static final String TYPE_CAST_ERROR_COLUMN_INDEX = "Column with index {} has type {} but {} was requested"; + + private static final int INT_COLUMN_TYPES_BITMASK = buildIntegerTypesBitMask(); + + /** Reads a value from the tuple and converts it to a byte if possible. */ + public static byte readByteValue(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType actualType, int columnIndex) { + if (!integerType(actualType)) { + throwClassCastException(ColumnType.INT8, actualType, columnIndex); + } + + IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnIndex); + + return castToByte(binaryTuple, binaryTupleIndex, actualType); + } + + /** Reads a value from the tuple and converts it to a byte if possible. */ + public static byte readByteValue(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType actualType, String columnName) { + if (!integerType(actualType)) { + throwClassCastException(ColumnType.INT8, actualType, columnName); + } + + IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnName); + + return castToByte(binaryTuple, binaryTupleIndex, actualType); + } + + /** Reads a value from the tuple and converts it to a short if possible. */ + public static short readShortValue(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType actualType, int columnIndex) { + if (!integerType(actualType)) { + throwClassCastException(ColumnType.INT16, actualType, columnIndex); + } + + IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnIndex); + + return castToShort(binaryTuple, binaryTupleIndex, actualType); + } + + /** Reads a value from the tuple and converts it to a short if possible. */ + public static short readShortValue(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType actualType, String columnName) { + if (!integerType(actualType)) { + throwClassCastException(ColumnType.INT16, actualType, columnName); + } + + IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnName); + + return castToShort(binaryTuple, binaryTupleIndex, actualType); + } + + /** Reads a value from the tuple and converts it to an int if possible. */ + public static int readIntValue(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType actualType, int columnIndex) { + if (!integerType(actualType)) { + throwClassCastException(ColumnType.INT32, actualType, columnIndex); + } + + IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnIndex); + + return castToInt(binaryTuple, binaryTupleIndex, actualType); + } + + /** Reads a value from the tuple and converts it to an int if possible. */ + public static int readIntValue(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType actualType, String columnName) { + if (!integerType(actualType)) { + throwClassCastException(ColumnType.INT32, actualType, columnName); + } + + IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnName); + + return castToInt(binaryTuple, binaryTupleIndex, actualType); + } + + /** Reads a value from the tuple and returns it as a long. Only integer column types are allowed. */ + public static long readLongValue(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType actualType, int columnIndex) { + if (!integerType(actualType)) { + throwClassCastException(ColumnType.INT64, actualType, columnIndex); + } + + IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnIndex); + + return binaryTuple.longValue(binaryTupleIndex); + } + + /** Reads a value from the tuple and returns it as a long. Only integer column types are allowed. */ + public static long readLongValue(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType actualType, String columnName) { + if (!integerType(actualType)) { + throwClassCastException(ColumnType.INT64, actualType, columnName); + } + + IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnName); + + return binaryTuple.longValue(binaryTupleIndex); + } + + /** Reads a value from the tuple and converts it to a float if possible. */ + public static float readFloatValue(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType actualType, int columnIndex) { + if (actualType == ColumnType.FLOAT || actualType == ColumnType.DOUBLE) { + IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnIndex); + + return castToFloat(binaryTuple, binaryTupleIndex, actualType); + } + + throw newClassCastException(ColumnType.FLOAT, actualType, columnIndex); + } + + /** Reads a value from the tuple and converts it to a float if possible. */ + public static float readFloatValue(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType actualType, String columnName) { + if (actualType == ColumnType.FLOAT || actualType == ColumnType.DOUBLE) { + IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnName); + + return castToFloat(binaryTuple, binaryTupleIndex, actualType); + } + + throw newClassCastException(ColumnType.FLOAT, actualType, columnName); + } + + /** Reads a value from the tuple and returns it as a double. */ + public static double readDoubleValue(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType actualType, int columnIndex) { + if (actualType == ColumnType.DOUBLE || actualType == ColumnType.FLOAT) { + IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnIndex); + + return binaryTuple.doubleValue(binaryTupleIndex); + } + + throw newClassCastException(ColumnType.DOUBLE, actualType, columnIndex); + } + + /** Reads a value from the tuple and returns it as a double. */ + public static double readDoubleValue(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType actualType, String columnName) { + if (actualType == ColumnType.DOUBLE || actualType == ColumnType.FLOAT) { + IgniteUtils.ensureNotNull(binaryTuple, binaryTupleIndex, columnName); + + return binaryTuple.doubleValue(binaryTupleIndex); + } + + throw newClassCastException(ColumnType.DOUBLE, actualType, columnName); + } + + /** + * Validates that the requested column type matches the actual type and throws {@link ClassCastException} + * otherwise. + */ + public static void validateColumnType(ColumnType requestedType, ColumnType actualType, int columnIndex) { + if (requestedType != actualType) { + throwClassCastException(requestedType, actualType, columnIndex); + } + } + + /** + * Validates that the requested column type matches the actual type and throws {@link ClassCastException} + * otherwise. + */ + public static void validateColumnType(ColumnType requestedType, ColumnType actualType, String columnName) { + if (requestedType != actualType) { + throwClassCastException(requestedType, actualType, columnName); + } + } + + /** Casts an integer value from the tuple to {@code byte} performing range checks. */ + private static byte castToByte(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType valueType) { + switch (valueType) { + case INT8: + return binaryTuple.byteValue(binaryTupleIndex); + + case INT16: { + short value = binaryTuple.shortValue(binaryTupleIndex); + byte byteValue = (byte) value; + + if (byteValue == value) { + return byteValue; + } + + throw new ArithmeticException("Byte value overflow: " + value); + } + case INT32: { + int value = binaryTuple.intValue(binaryTupleIndex); + byte byteValue = (byte) value; + + if (byteValue == value) { + return byteValue; + } + + throw new ArithmeticException("Byte value overflow: " + value); + } + case INT64: { + long value = binaryTuple.longValue(binaryTupleIndex); + byte byteValue = (byte) value; + + if (byteValue == value) { + return byteValue; + } + + throw new ArithmeticException("Byte value overflow: " + value); + } + + default: + } + + throw new IllegalArgumentException("invalid type: " + valueType); + } + + /** Casts an integer value from the tuple to {@code short} performing range checks. */ + private static short castToShort(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType valueType) { + switch (valueType) { + case INT16: + case INT8: + return binaryTuple.shortValue(binaryTupleIndex); + + case INT32: { + int value = binaryTuple.intValue(binaryTupleIndex); + short shortValue = (short) value; + + if (shortValue == value) { + return shortValue; + } + + throw new ArithmeticException("Short value overflow: " + value); + } + case INT64: { + long value = binaryTuple.longValue(binaryTupleIndex); + short shortValue = (short) value; + + if (shortValue == value) { + return shortValue; + } + + throw new ArithmeticException("Short value overflow: " + value); + } + + default: + } + + throw new IllegalArgumentException("invalid type: " + valueType); + } + + /** Casts an integer value from the tuple to {@code int} performing range checks. */ + private static int castToInt(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType valueType) { + switch (valueType) { + case INT32: + case INT16: + case INT8: + return binaryTuple.intValue(binaryTupleIndex); + + case INT64: { + long value = binaryTuple.longValue(binaryTupleIndex); + int intValue = (int) value; + + if (intValue == value) { + return intValue; + } + + throw new ArithmeticException("Int value overflow: " + value); + } + + default: + assert false : valueType; + } + + throw new IllegalArgumentException("invalid type: " + valueType); + } + + /** Casts a floating-point value from the tuple to {@code float} performing precision checks. */ + private static float castToFloat(InternalTuple binaryTuple, int binaryTupleIndex, ColumnType actualType) { + if (actualType == ColumnType.FLOAT) { + return binaryTuple.floatValue(binaryTupleIndex); + } + + double doubleValue = binaryTuple.doubleValue(binaryTupleIndex); + float floatValue = (float) doubleValue; + + if (doubleValue == floatValue || Double.isNaN(doubleValue)) { + return floatValue; + } + + throw new ArithmeticException("Float value overflow: " + doubleValue); Review Comment: Using floating-point equality comparison (==) without proper handling of special cases could be problematic. While the code includes a check for NaN, it doesn't handle the case where the double value is infinity. When a double infinity is cast to float, it remains infinity, and the equality check will pass, which is correct. However, consider adding an explicit comment explaining that infinity values are intentionally handled by the equality check to make the logic clearer. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; + } + + /** + * Casts a {@link Number} to {@code short}. + * + * @param number Number to cast. + * @return Short primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code short} range. + */ + public static short castToShort(Number number) { + if (number instanceof Short) { + return (short) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Byte) { + long longVal = number.longValue(); + short shortVal = number.shortValue(); + + if (longVal == shortVal) { + return shortVal; + } + + throw new ArithmeticException("Short value overflow: " + number); + } + + return (short) number; + } + + /** + * Casts a {@link Number} to {@code int}. + * + * @param number Number to cast. + * @return Int primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code int} range. + */ + public static int castToInt(Number number) { + if (number instanceof Integer) { + return (int) number; + } + + if (number instanceof Long || number instanceof Short || number instanceof Byte) { + long longVal = number.longValue(); + int intVal = number.intValue(); + + if (longVal == intVal) { + return intVal; + } + + throw new ArithmeticException("Int value overflow: " + number); + } + + return (int) number; + } + + /** + * Casts a {@link Number} to {@code long}. + * + * @param number Number to cast. + * @return Long primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code long} range. + */ + public static long castToLong(Number number) { + if (number instanceof Long || number instanceof Integer || number instanceof Short || number instanceof Byte) { + return number.longValue(); + } + + return (long) number; + } + + /** + * Casts a {@link Number} to {@code float}. + * + * @param number Number to cast. + * @return Float primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code float} range. + */ + public static float castToFloat(Number number) { + if (number instanceof Float) { + return (float) number; + } + + if (number instanceof Double) { + double doubleVal = number.doubleValue(); + float floatVal = number.floatValue(); + + //noinspection FloatingPointEquality + if (doubleVal == floatVal || Double.isNaN(doubleVal)) { + return floatVal; + } + + throw new ArithmeticException("Float value overflow: " + number); + } + + return (float) number; + } + + /** + * Casts a {@link Number} to {@code double}. + * + * @param number Number to cast. + * @return Double primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code double} range. + */ + public static double castToDouble(Number number) { + if (number instanceof Double || number instanceof Float) { + return number.doubleValue(); + } + + return (double) number; + } Review Comment: The method does not have a null check for the number parameter. If a null Number is passed, the instanceof checks will all fail and the fallback cast will throw a NullPointerException. Consider adding an explicit null check with a clear error message, or document in the Javadoc that the parameter must not be null. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; + } + + /** + * Casts a {@link Number} to {@code short}. + * + * @param number Number to cast. + * @return Short primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code short} range. + */ + public static short castToShort(Number number) { + if (number instanceof Short) { + return (short) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Byte) { + long longVal = number.longValue(); + short shortVal = number.shortValue(); + + if (longVal == shortVal) { + return shortVal; + } + + throw new ArithmeticException("Short value overflow: " + number); + } + + return (short) number; + } + + /** + * Casts a {@link Number} to {@code int}. + * + * @param number Number to cast. + * @return Int primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code int} range. + */ + public static int castToInt(Number number) { + if (number instanceof Integer) { + return (int) number; + } + + if (number instanceof Long || number instanceof Short || number instanceof Byte) { + long longVal = number.longValue(); + int intVal = number.intValue(); + + if (longVal == intVal) { + return intVal; + } + + throw new ArithmeticException("Int value overflow: " + number); + } + + return (int) number; + } + + /** + * Casts a {@link Number} to {@code long}. + * + * @param number Number to cast. + * @return Long primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code long} range. + */ + public static long castToLong(Number number) { + if (number instanceof Long || number instanceof Integer || number instanceof Short || number instanceof Byte) { + return number.longValue(); + } + + return (long) number; + } + + /** + * Casts a {@link Number} to {@code float}. + * + * @param number Number to cast. + * @return Float primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code float} range. + */ + public static float castToFloat(Number number) { + if (number instanceof Float) { + return (float) number; + } + + if (number instanceof Double) { + double doubleVal = number.doubleValue(); + float floatVal = number.floatValue(); + + //noinspection FloatingPointEquality + if (doubleVal == floatVal || Double.isNaN(doubleVal)) { + return floatVal; + } + + throw new ArithmeticException("Float value overflow: " + number); + } + + return (float) number; Review Comment: The fallback return statement `return (float) number;` at line 155 will silently cast any unsupported Number type without proper validation. This could lead to unexpected behavior or ClassCastException for types like BigDecimal or BigInteger. Consider throwing a ClassCastException explicitly for unsupported types instead of attempting a blind cast. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; + } + + /** + * Casts a {@link Number} to {@code short}. + * + * @param number Number to cast. + * @return Short primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code short} range. + */ + public static short castToShort(Number number) { + if (number instanceof Short) { + return (short) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Byte) { + long longVal = number.longValue(); + short shortVal = number.shortValue(); + + if (longVal == shortVal) { + return shortVal; + } + + throw new ArithmeticException("Short value overflow: " + number); + } + + return (short) number; + } + + /** + * Casts a {@link Number} to {@code int}. + * + * @param number Number to cast. + * @return Int primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code int} range. + */ + public static int castToInt(Number number) { + if (number instanceof Integer) { + return (int) number; + } + + if (number instanceof Long || number instanceof Short || number instanceof Byte) { + long longVal = number.longValue(); + int intVal = number.intValue(); + + if (longVal == intVal) { + return intVal; + } + + throw new ArithmeticException("Int value overflow: " + number); + } + + return (int) number; + } + + /** + * Casts a {@link Number} to {@code long}. + * + * @param number Number to cast. + * @return Long primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code long} range. + */ + public static long castToLong(Number number) { + if (number instanceof Long || number instanceof Integer || number instanceof Short || number instanceof Byte) { + return number.longValue(); + } + + return (long) number; + } + + /** + * Casts a {@link Number} to {@code float}. + * + * @param number Number to cast. + * @return Float primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code float} range. + */ + public static float castToFloat(Number number) { + if (number instanceof Float) { + return (float) number; + } + + if (number instanceof Double) { + double doubleVal = number.doubleValue(); + float floatVal = number.floatValue(); + + //noinspection FloatingPointEquality + if (doubleVal == floatVal || Double.isNaN(doubleVal)) { + return floatVal; + } + + throw new ArithmeticException("Float value overflow: " + number); + } + + return (float) number; + } + + /** + * Casts a {@link Number} to {@code double}. + * + * @param number Number to cast. + * @return Double primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code double} range. + */ + public static double castToDouble(Number number) { + if (number instanceof Double || number instanceof Float) { + return number.doubleValue(); + } + + return (double) number; Review Comment: The fallback return statement `return (double) number;` at line 171 will silently cast any unsupported Number type without proper validation. This could lead to unexpected behavior or ClassCastException for types like BigDecimal or BigInteger. Consider throwing a ClassCastException explicitly for unsupported types instead of attempting a blind cast. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; + } + + /** + * Casts a {@link Number} to {@code short}. + * + * @param number Number to cast. + * @return Short primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code short} range. + */ + public static short castToShort(Number number) { + if (number instanceof Short) { + return (short) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Byte) { + long longVal = number.longValue(); + short shortVal = number.shortValue(); + + if (longVal == shortVal) { + return shortVal; + } + + throw new ArithmeticException("Short value overflow: " + number); + } + + return (short) number; + } + + /** + * Casts a {@link Number} to {@code int}. + * + * @param number Number to cast. + * @return Int primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code int} range. + */ + public static int castToInt(Number number) { + if (number instanceof Integer) { + return (int) number; + } + + if (number instanceof Long || number instanceof Short || number instanceof Byte) { + long longVal = number.longValue(); + int intVal = number.intValue(); + + if (longVal == intVal) { + return intVal; + } + + throw new ArithmeticException("Int value overflow: " + number); + } + + return (int) number; + } + + /** + * Casts a {@link Number} to {@code long}. + * + * @param number Number to cast. + * @return Long primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code long} range. + */ + public static long castToLong(Number number) { + if (number instanceof Long || number instanceof Integer || number instanceof Short || number instanceof Byte) { + return number.longValue(); + } + + return (long) number; + } + + /** + * Casts a {@link Number} to {@code float}. + * + * @param number Number to cast. + * @return Float primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code float} range. + */ + public static float castToFloat(Number number) { + if (number instanceof Float) { + return (float) number; + } + + if (number instanceof Double) { + double doubleVal = number.doubleValue(); + float floatVal = number.floatValue(); + + //noinspection FloatingPointEquality + if (doubleVal == floatVal || Double.isNaN(doubleVal)) { + return floatVal; + } + + throw new ArithmeticException("Float value overflow: " + number); + } + + return (float) number; + } Review Comment: The method does not have a null check for the number parameter. If a null Number is passed, the instanceof checks will all fail and the fallback cast will throw a NullPointerException. Consider adding an explicit null check with a clear error message, or document in the Javadoc that the parameter must not be null. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; + } + + /** + * Casts a {@link Number} to {@code short}. + * + * @param number Number to cast. + * @return Short primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code short} range. + */ + public static short castToShort(Number number) { + if (number instanceof Short) { + return (short) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Byte) { + long longVal = number.longValue(); + short shortVal = number.shortValue(); + + if (longVal == shortVal) { + return shortVal; + } + + throw new ArithmeticException("Short value overflow: " + number); + } + + return (short) number; Review Comment: The fallback return statement `return (short) number;` at line 84 will silently cast any unsupported Number type without proper validation or overflow checking. This could lead to unexpected behavior or ClassCastException for types like BigDecimal or BigInteger. Consider throwing a ClassCastException explicitly for unsupported types instead of attempting a blind cast. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; + } + + /** + * Casts a {@link Number} to {@code short}. + * + * @param number Number to cast. + * @return Short primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code short} range. + */ + public static short castToShort(Number number) { + if (number instanceof Short) { + return (short) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Byte) { + long longVal = number.longValue(); + short shortVal = number.shortValue(); + + if (longVal == shortVal) { + return shortVal; + } + + throw new ArithmeticException("Short value overflow: " + number); + } + + return (short) number; + } + + /** + * Casts a {@link Number} to {@code int}. + * + * @param number Number to cast. + * @return Int primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code int} range. + */ + public static int castToInt(Number number) { + if (number instanceof Integer) { + return (int) number; + } + + if (number instanceof Long || number instanceof Short || number instanceof Byte) { + long longVal = number.longValue(); + int intVal = number.intValue(); + + if (longVal == intVal) { + return intVal; + } + + throw new ArithmeticException("Int value overflow: " + number); + } + + return (int) number; + } + + /** + * Casts a {@link Number} to {@code long}. + * + * @param number Number to cast. + * @return Long primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code long} range. + */ + public static long castToLong(Number number) { + if (number instanceof Long || number instanceof Integer || number instanceof Short || number instanceof Byte) { + return number.longValue(); + } + + return (long) number; Review Comment: The fallback return statement `return (long) number;` at line 127 will silently cast any unsupported Number type without proper validation. This could lead to unexpected behavior or ClassCastException for types like BigDecimal or BigInteger. Consider throwing a ClassCastException explicitly for unsupported types instead of attempting a blind cast. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; + } + + /** + * Casts a {@link Number} to {@code short}. + * + * @param number Number to cast. + * @return Short primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code short} range. + */ + public static short castToShort(Number number) { + if (number instanceof Short) { + return (short) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Byte) { + long longVal = number.longValue(); + short shortVal = number.shortValue(); + + if (longVal == shortVal) { + return shortVal; + } + + throw new ArithmeticException("Short value overflow: " + number); + } + + return (short) number; + } + + /** + * Casts a {@link Number} to {@code int}. + * + * @param number Number to cast. + * @return Int primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code int} range. + */ + public static int castToInt(Number number) { + if (number instanceof Integer) { + return (int) number; + } + + if (number instanceof Long || number instanceof Short || number instanceof Byte) { + long longVal = number.longValue(); + int intVal = number.intValue(); + + if (longVal == intVal) { + return intVal; + } + + throw new ArithmeticException("Int value overflow: " + number); + } + + return (int) number; Review Comment: The fallback return statement `return (int) number;` at line 111 will silently cast any unsupported Number type without proper validation or overflow checking. This could lead to unexpected behavior or ClassCastException for types like BigDecimal or BigInteger. Consider throwing a ClassCastException explicitly for unsupported types instead of attempting a blind cast. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; + } + + /** + * Casts a {@link Number} to {@code short}. + * + * @param number Number to cast. + * @return Short primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code short} range. + */ + public static short castToShort(Number number) { + if (number instanceof Short) { + return (short) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Byte) { + long longVal = number.longValue(); + short shortVal = number.shortValue(); + + if (longVal == shortVal) { + return shortVal; + } + + throw new ArithmeticException("Short value overflow: " + number); + } + + return (short) number; + } + + /** + * Casts a {@link Number} to {@code int}. + * + * @param number Number to cast. + * @return Int primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code int} range. + */ + public static int castToInt(Number number) { + if (number instanceof Integer) { + return (int) number; + } + + if (number instanceof Long || number instanceof Short || number instanceof Byte) { + long longVal = number.longValue(); + int intVal = number.intValue(); + + if (longVal == intVal) { + return intVal; + } + + throw new ArithmeticException("Int value overflow: " + number); + } + + return (int) number; + } Review Comment: The method does not have a null check for the number parameter. If a null Number is passed, the instanceof checks will all fail and the fallback cast will throw a NullPointerException. Consider adding an explicit null check with a clear error message, or document in the Javadoc that the parameter must not be null. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; Review Comment: The fallback return statement `return (byte) number;` at line 57 will silently cast any unsupported Number type without proper validation or overflow checking. This could lead to unexpected behavior or ClassCastException for types like BigDecimal or BigInteger. Consider throwing a ClassCastException explicitly for unsupported types instead of attempting a blind cast. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; + } Review Comment: The method does not have a null check for the number parameter. If a null Number is passed, the instanceof checks will all fail and the fallback cast will throw a NullPointerException. Consider adding an explicit null check with a clear error message, or document in the Javadoc that the parameter must not be null. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; + } + + /** + * Casts a {@link Number} to {@code short}. + * + * @param number Number to cast. + * @return Short primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code short} range. + */ + public static short castToShort(Number number) { + if (number instanceof Short) { + return (short) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Byte) { + long longVal = number.longValue(); + short shortVal = number.shortValue(); + + if (longVal == shortVal) { + return shortVal; + } + + throw new ArithmeticException("Short value overflow: " + number); + } + + return (short) number; + } + + /** + * Casts a {@link Number} to {@code int}. + * + * @param number Number to cast. + * @return Int primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code int} range. + */ + public static int castToInt(Number number) { + if (number instanceof Integer) { + return (int) number; + } + + if (number instanceof Long || number instanceof Short || number instanceof Byte) { + long longVal = number.longValue(); + int intVal = number.intValue(); + + if (longVal == intVal) { + return intVal; + } + + throw new ArithmeticException("Int value overflow: " + number); + } + + return (int) number; + } + + /** + * Casts a {@link Number} to {@code long}. + * + * @param number Number to cast. + * @return Long primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code long} range. + */ + public static long castToLong(Number number) { + if (number instanceof Long || number instanceof Integer || number instanceof Short || number instanceof Byte) { + return number.longValue(); + } + + return (long) number; + } + + /** + * Casts a {@link Number} to {@code float}. + * + * @param number Number to cast. + * @return Float primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code float} range. + */ + public static float castToFloat(Number number) { + if (number instanceof Float) { + return (float) number; + } + + if (number instanceof Double) { + double doubleVal = number.doubleValue(); + float floatVal = number.floatValue(); + + //noinspection FloatingPointEquality + if (doubleVal == floatVal || Double.isNaN(doubleVal)) { + return floatVal; + } + + throw new ArithmeticException("Float value overflow: " + number); Review Comment: Using floating-point equality comparison (==) without proper handling of special cases could be problematic. While the code includes a check for NaN, it doesn't handle the case where the double value is infinity. When a double infinity is cast to float, it remains infinity, and the equality check will pass, which is correct. However, consider adding an explicit comment explaining that infinity values are intentionally handled by the equality check to make the logic clearer. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; + } + + /** + * Casts a {@link Number} to {@code short}. + * + * @param number Number to cast. + * @return Short primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code short} range. + */ + public static short castToShort(Number number) { + if (number instanceof Short) { + return (short) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Byte) { + long longVal = number.longValue(); + short shortVal = number.shortValue(); + + if (longVal == shortVal) { + return shortVal; + } + + throw new ArithmeticException("Short value overflow: " + number); + } + + return (short) number; + } + + /** + * Casts a {@link Number} to {@code int}. + * + * @param number Number to cast. + * @return Int primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code int} range. + */ + public static int castToInt(Number number) { + if (number instanceof Integer) { + return (int) number; + } + + if (number instanceof Long || number instanceof Short || number instanceof Byte) { + long longVal = number.longValue(); + int intVal = number.intValue(); + + if (longVal == intVal) { + return intVal; + } + + throw new ArithmeticException("Int value overflow: " + number); + } + + return (int) number; + } + + /** + * Casts a {@link Number} to {@code long}. + * + * @param number Number to cast. + * @return Long primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code long} range. + */ + public static long castToLong(Number number) { + if (number instanceof Long || number instanceof Integer || number instanceof Short || number instanceof Byte) { + return number.longValue(); + } + + return (long) number; + } + + /** + * Casts a {@link Number} to {@code float}. + * + * @param number Number to cast. + * @return Float primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code float} range. + */ + public static float castToFloat(Number number) { + if (number instanceof Float) { + return (float) number; + } + + if (number instanceof Double) { + double doubleVal = number.doubleValue(); + float floatVal = number.floatValue(); + + //noinspection FloatingPointEquality + if (doubleVal == floatVal || Double.isNaN(doubleVal)) { + return floatVal; + } + + throw new ArithmeticException("Float value overflow: " + number); + } + + return (float) number; + } + + /** + * Casts a {@link Number} to {@code double}. + * + * @param number Number to cast. + * @return Double primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code double} range. + */ Review Comment: The Javadoc for castToDouble states it may throw ArithmeticException on overflow, but double can represent all float values without overflow. The documentation should be corrected to remove the mention of ArithmeticException, or clarify that it only applies to unsupported non-floating-point Number types. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; + } + + /** + * Casts a {@link Number} to {@code short}. + * + * @param number Number to cast. + * @return Short primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code short} range. + */ + public static short castToShort(Number number) { + if (number instanceof Short) { + return (short) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Byte) { + long longVal = number.longValue(); + short shortVal = number.shortValue(); + + if (longVal == shortVal) { + return shortVal; + } + + throw new ArithmeticException("Short value overflow: " + number); + } + + return (short) number; + } + + /** + * Casts a {@link Number} to {@code int}. + * + * @param number Number to cast. + * @return Int primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code int} range. + */ + public static int castToInt(Number number) { + if (number instanceof Integer) { + return (int) number; + } + + if (number instanceof Long || number instanceof Short || number instanceof Byte) { + long longVal = number.longValue(); + int intVal = number.intValue(); + + if (longVal == intVal) { + return intVal; + } + + throw new ArithmeticException("Int value overflow: " + number); + } + + return (int) number; + } + + /** + * Casts a {@link Number} to {@code long}. + * + * @param number Number to cast. + * @return Long primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code long} range. + */ + public static long castToLong(Number number) { + if (number instanceof Long || number instanceof Integer || number instanceof Short || number instanceof Byte) { + return number.longValue(); + } + + return (long) number; + } Review Comment: The method does not have a null check for the number parameter. If a null Number is passed, the instanceof checks will all fail and the fallback cast will throw a NullPointerException. Consider adding an explicit null check with a clear error message, or document in the Javadoc that the parameter must not be null. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; + } + + /** + * Casts a {@link Number} to {@code short}. + * + * @param number Number to cast. + * @return Short primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code short} range. + */ + public static short castToShort(Number number) { + if (number instanceof Short) { + return (short) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Byte) { + long longVal = number.longValue(); + short shortVal = number.shortValue(); + + if (longVal == shortVal) { + return shortVal; + } + + throw new ArithmeticException("Short value overflow: " + number); + } + + return (short) number; + } Review Comment: The method does not have a null check for the number parameter. If a null Number is passed, the instanceof checks will all fail and the fallback cast will throw a NullPointerException. Consider adding an explicit null check with a clear error message, or document in the Javadoc that the parameter must not be null. ########## modules/api/src/main/java/org/apache/ignite/lang/util/NumericTypeCastUtils.java: ########## @@ -0,0 +1,173 @@ +/* + * 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.ignite.lang.util; + +/** + * Utility helpers for converting {@link Number} instances to Java primitive numeric types. + * + * <p>The following conversions are supported: + * <ul> + * <li>Any integer type to any other integer type with range checks (e.g. {@link Long} to {@link Byte} + * may throw {@link ArithmeticException} if the value doesn't fit into {@link Byte} range).</li> + * <li>{@link Float} to {@link Double} are always allowed.</li> + * <li>{@link Double} to {@link Float} with precision checks. Throws {@link ArithmeticException} + * if the value cannot be represented as FLOAT without precision loss.</li> + * </ul> + */ +public class NumericTypeCastUtils { + /** + * Casts a {@link Number} to {@code byte}. + * + * @param number Number to cast. + * @return Byte primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code byte} range. + */ + public static byte castToByte(Number number) { + if (number instanceof Byte) { + return (byte) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Short) { + long longVal = number.longValue(); + byte byteVal = number.byteValue(); + + if (longVal == byteVal) { + return byteVal; + } + + throw new ArithmeticException("Byte value overflow: " + number); + } + + return (byte) number; + } + + /** + * Casts a {@link Number} to {@code short}. + * + * @param number Number to cast. + * @return Short primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException if value overflows {@code short} range. + */ + public static short castToShort(Number number) { + if (number instanceof Short) { + return (short) number; + } + + if (number instanceof Long || number instanceof Integer || number instanceof Byte) { + long longVal = number.longValue(); + short shortVal = number.shortValue(); + + if (longVal == shortVal) { + return shortVal; + } + + throw new ArithmeticException("Short value overflow: " + number); + } + + return (short) number; + } + + /** + * Casts a {@link Number} to {@code int}. + * + * @param number Number to cast. + * @return Int primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code int} range. + */ + public static int castToInt(Number number) { + if (number instanceof Integer) { + return (int) number; + } + + if (number instanceof Long || number instanceof Short || number instanceof Byte) { + long longVal = number.longValue(); + int intVal = number.intValue(); + + if (longVal == intVal) { + return intVal; + } + + throw new ArithmeticException("Int value overflow: " + number); + } + + return (int) number; + } + + /** + * Casts a {@link Number} to {@code long}. + * + * @param number Number to cast. + * @return Long primitive value. + * @throws ClassCastException If the provided {@code number} is of unsupported type. + * @throws ArithmeticException If the value overflows {@code long} range. + */ Review Comment: The Javadoc for castToLong states it may throw ArithmeticException on overflow, but long is the widest integer type being converted from, so there is no overflow scenario. The documentation should be corrected to remove the mention of ArithmeticException, or clarify that it only applies to unsupported non-integer Number types. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
