This is an automated email from the ASF dual-hosted git repository. doebele pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/empire-db.git
The following commit(s) were added to refs/heads/master by this push: new 8a42edc4 EMPIREDB-444 ObjectUtils: rollback of funcitons getInteger(), getLong(), getDecimal() to not throwing a NumberFormatException. 8a42edc4 is described below commit 8a42edc41ceaf2cee215aac028eb91dc831c99f5 Author: Rainer Döbele <rdoeb...@users.noreply.github.com> AuthorDate: Fri Nov 1 14:18:09 2024 +0100 EMPIREDB-444 ObjectUtils: rollback of funcitons getInteger(), getLong(), getDecimal() to not throwing a NumberFormatException. --- .../org/apache/empire/commons/ObjectUtils.java | 102 ++++++++++----------- .../main/java/org/apache/empire/db/DBDatabase.java | 8 +- .../org/apache/empire/commons/ObjectUtilsTest.java | 13 ++- 3 files changed, 63 insertions(+), 60 deletions(-) diff --git a/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java b/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java index 7268cc14..b12228a7 100644 --- a/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java +++ b/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java @@ -190,7 +190,7 @@ public final class ObjectUtils } /** - * @Deprecated use getInteger() instead + * @Deprecated use getValueUtils().toInteger() instead * @param v the value to convert * @return the integer value */ @@ -200,18 +200,6 @@ public final class ObjectUtils return valueUtils.toInteger(v); } - /** - * Converts an object value to an integer. - * <P> - * If the object value supplied is null or if conversion is not possible then 0 is returned. - * @param v the object value to convert - * @return the Integer value of v or 0 - */ - public static int getInteger(Object v) - { - return valueUtils.toInteger(v); - } - /** * Converts an object value to an integer. * <P> @@ -233,9 +221,21 @@ public final class ObjectUtils return defValue; } } + + /** + * Converts an object value to an integer. + * <P> + * If the object value supplied is null or if conversion is not possible then 0 is returned. + * @param v the object value to convert + * @return the Integer value of v or 0 + */ + public static int getInteger(Object v) + { + return getInteger(v, 0); + } /** - * @Deprecated use getLong() instead + * @Deprecated use getValueUtils().toLong() instead * @param v the value to convert * @return the long value */ @@ -245,18 +245,6 @@ public final class ObjectUtils return valueUtils.toLong(v); } - /** - * Converts an object value to a long. - * <P> - * If the object value supplied is null or if conversion is not possible then 0 is returned. - * @param v the object value to convert - * @return the Long value of v or 0 - */ - public static long getLong(Object v) - { - return valueUtils.toLong(v); - } - /** * Converts an object value to a long. * <P> @@ -278,26 +266,26 @@ public final class ObjectUtils return defValue; } } - + /** - * @Deprecated use getDouble() instead - * @param v the value to convert - * @return the double value + * Converts an object value to a long. + * <P> + * If the object value supplied is null or if conversion is not possible then 0 is returned. + * @param v the object value to convert + * @return the Long value of v or 0 */ - @Deprecated - public static double toDouble(Object v) + public static long getLong(Object v) { - return valueUtils.toDouble(v); + return getLong(v, 0); } /** - * Converts an object value to a double. - * <P> - * If the object value supplied is null or if conversion is not possible then 0.0 is returned. - * @param v the object value to convert - * @return the Long value of v or 0 + * @Deprecated use getValueUtils().toDouble() instead + * @param v the value to convert + * @return the double value */ - public static double getDouble(Object v) + @Deprecated + public static double toDouble(Object v) { return valueUtils.toDouble(v); } @@ -325,24 +313,24 @@ public final class ObjectUtils } /** - * @Deprecated use getDecimal() instead - * @param v the value to convert - * @return the decimal value + * Converts an object value to a double. + * <P> + * If the object value supplied is null or if conversion is not possible then 0.0 is returned. + * @param v the object value to convert + * @return the Long value of v or 0 */ - @Deprecated - public static BigDecimal toDecimal(Object v) + public static double getDouble(Object v) { - return valueUtils.toDecimal(v); + return getDouble(v, 0.0d); } /** - * Converts an object value to a BigDecimal. - * <P> - * If the object value supplied is null or if conversion is not possible then 0.0 is returned. - * @param v the object value to convert - * @return the Long value of v or 0 + * @Deprecated use getValueUtils().toDecimal() instead + * @param v the value to convert + * @return the decimal value */ - public static BigDecimal getDecimal(Object v) + @Deprecated + public static BigDecimal toDecimal(Object v) { return valueUtils.toDecimal(v); } @@ -368,6 +356,18 @@ public final class ObjectUtils return defValue; } } + + /** + * Converts an object value to a BigDecimal. + * <P> + * If the object value supplied is null or if conversion is not possible then 0.0 is returned. + * @param v the object value to convert + * @return the Long value of v or 0 + */ + public static BigDecimal getDecimal(Object v) + { + return getDecimal(v, BigDecimal.ZERO); + } /** * Converts an object value to a boolean. diff --git a/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java b/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java index 959889ee..d0b5023a 100644 --- a/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java +++ b/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java @@ -1118,7 +1118,7 @@ public abstract class DBDatabase extends DBObject if (!(value instanceof java.lang.Number)) { try { // Convert to Decimal - value = ObjectUtils.getDecimal(value); + value = ObjectUtils.getValueUtils().toDecimal(value); // throws NumberFormatException if not a number! } catch (NumberFormatException e) { log.info("Parsing '{}' to Decimal failed for column {}. Message is "+e.toString(), value, column.getName()); @@ -1133,7 +1133,7 @@ public abstract class DBDatabase extends DBObject if (!(value instanceof java.lang.Number)) { try { // Convert to Double - value = ObjectUtils.getDouble(value); + value = ObjectUtils.getValueUtils().toDouble(value); // throws NumberFormatException if not a number! } catch (NumberFormatException e) { log.info("Parsing '{}' to Double failed for column {}. Message is "+e.toString(), value, column.getName()); @@ -1152,7 +1152,7 @@ public abstract class DBDatabase extends DBObject if (!(value instanceof java.lang.Number)) { try { // Convert to Long - value = ObjectUtils.getLong(value); + value = ObjectUtils.getValueUtils().toLong(value); } catch (NumberFormatException e) { log.info("Parsing '{}' to Integer failed for column {}. Message is "+e.toString(), value, column.getName()); throw new FieldIllegalValueException(column, String.valueOf(value), e); @@ -1204,7 +1204,7 @@ public abstract class DBDatabase extends DBObject // Check overall if (type==DataType.DECIMAL) { // Convert to Decimal - BigDecimal dv = ObjectUtils.getDecimal(n); + BigDecimal dv = ObjectUtils.getValueUtils().toDecimal(n); int prec = dv.precision(); int scale = dv.scale(); // check precision and scale diff --git a/empire-db/src/test/java/org/apache/empire/commons/ObjectUtilsTest.java b/empire-db/src/test/java/org/apache/empire/commons/ObjectUtilsTest.java index 5cccd8bf..07730988 100644 --- a/empire-db/src/test/java/org/apache/empire/commons/ObjectUtilsTest.java +++ b/empire-db/src/test/java/org/apache/empire/commons/ObjectUtilsTest.java @@ -160,7 +160,8 @@ public class ObjectUtilsTest { assertEquals(0, ObjectUtils.getInteger(null)); assertEquals(0, ObjectUtils.getInteger("")); - assertThrows(NumberFormatException.class, () -> ObjectUtils.getInteger("JUnit")); + assertEquals(0, ObjectUtils.getInteger("JUnit")); + assertThrows(NumberFormatException.class, () -> ObjectUtils.getValueUtils().toInteger("JUnit")); assertEquals(456, ObjectUtils.getInteger("456")); assertEquals(456, ObjectUtils.getInteger(Long.valueOf(456))); @@ -204,7 +205,8 @@ public class ObjectUtilsTest { assertEquals(0, ObjectUtils.getLong(null)); assertEquals(0, ObjectUtils.getLong("")); - assertThrows(NumberFormatException.class, () -> ObjectUtils.getLong("JUnit")); + assertEquals(0, ObjectUtils.getLong("JUnit")); + assertThrows(NumberFormatException.class, () -> ObjectUtils.getValueUtils().toLong("JUnit")); assertEquals(456L, ObjectUtils.getLong("456")); assertEquals(456L, ObjectUtils.getLong(Long.valueOf(456))); @@ -244,9 +246,10 @@ public class ObjectUtilsTest @Test public void testGetDoubleObject() { - assertEquals(0, ObjectUtils.getDouble(null), 0); - assertEquals(0, ObjectUtils.getDouble(""), 0); - assertThrows(NumberFormatException.class, () -> ObjectUtils.getDouble("JUnit")); + assertEquals(0.0d, ObjectUtils.getDouble(null), 0); + assertEquals(0.0d, ObjectUtils.getDouble(""), 0); + assertEquals(0.0d, ObjectUtils.getDouble("JUnit"), 0); + assertThrows(NumberFormatException.class, () -> ObjectUtils.getValueUtils().toDouble("JUnit")); assertEquals(456.123d, ObjectUtils.getDouble("456.123"),0); assertEquals(456d, ObjectUtils.getDouble(Long.valueOf(456)),0);