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 a8e39021 EMPIREDB-444 ValueUtils: refactored for consistent naming
a8e39021 is described below
commit a8e39021686016b1b5b31ece1b05989716ccb729
Author: Rainer Döbele <[email protected]>
AuthorDate: Thu Oct 31 22:07:07 2024 +0100
EMPIREDB-444
ValueUtils: refactored for consistent naming
---
.../org/apache/empire/commons/ObjectUtils.java | 161 ++++++++++++---------
.../java/org/apache/empire/commons/ValueUtils.java | 81 +++++++----
.../main/java/org/apache/empire/db/DBDatabase.java | 8 +-
3 files changed, 149 insertions(+), 101 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 3514e4cc..7268cc14 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
@@ -20,6 +20,7 @@ package org.apache.empire.commons;
import java.io.Serializable;
import java.math.BigDecimal;
+import java.sql.Timestamp;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
@@ -189,22 +190,35 @@ public final class ObjectUtils
}
/**
- * converts an object to an integer. If conversion is not possible, an
error is thrown
+ * @Deprecated use getInteger() instead
* @param v the value to convert
* @return the integer value
*/
+ @Deprecated
public static int toInteger(Object v)
{
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>
* If the object value supplied is null or if conversion is not possible
then the default value is returned.
* @param v the obect to convert
* @param defValue the default value if o is null or conversion is not
possible
- * @return the Integer value of o or a default value
+ * @return the Integer value of v or a default value
*/
public static int getInteger(Object v, int defValue)
{
@@ -213,42 +227,43 @@ public final class ObjectUtils
return defValue;
try
{ // Try to convert
- return toInteger(v);
+ return valueUtils.toInteger(v);
} catch (NumberFormatException e) {
log.error(String.format("Cannot convert value [%s] to int", v));
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 o or 0
- */
- public static int getInteger(Object v)
- {
- return getInteger(v, 0);
- }
/**
- * converts an object to a long. If conversion is not possible, an error
is thrown
+ * @Deprecated use getLong() instead
* @param v the value to convert
* @return the long value
- */
+ */
+ @Deprecated
public static long toLong(Object v)
{
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>
* If the object value supplied is null or if conversion is not possible
then the default value is returned.
* @param v the obect to convert
* @param defValue the default value if o is null or conversion is not
possible
- * @return the Integer value of o or a default value
+ * @return the Integer value of v or a default value
*/
public static long getLong(Object v, long defValue)
{
@@ -257,31 +272,32 @@ public final class ObjectUtils
return defValue;
try
{ // Try to convert
- return toLong(v);
+ return valueUtils.toLong(v);
} catch (NumberFormatException e) {
log.error(String.format("Cannot convert value [%s] to long", v));
return defValue;
}
}
-
+
/**
- * 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 o or 0
+ * @Deprecated use getDouble() instead
+ * @param v the value to convert
+ * @return the double value
*/
- public static long getLong(Object v)
+ @Deprecated
+ public static double toDouble(Object v)
{
- return getLong(v, 0);
+ return valueUtils.toDouble(v);
}
/**
- * converts an object to a double. If conversion is not possible, an error
is thrown
- * @param v the value to convert
- * @return the double 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
*/
- public static double toDouble(Object v)
+ public static double getDouble(Object v)
{
return valueUtils.toDouble(v);
}
@@ -292,7 +308,7 @@ public final class ObjectUtils
* If the object value supplied is null or if conversion is not possible
then defValue is returned.
* @param v the object value to convert
* @param defValue the default value
- * @return the Long value of o or defValue
+ * @return the Long value of v or defValue
*/
public static double getDouble(Object v, double defValue)
{
@@ -301,7 +317,7 @@ public final class ObjectUtils
return defValue;
try
{ // Try to convert
- return toDouble(v);
+ return valueUtils.toDouble(v);
} catch (NumberFormatException e) {
log.error(String.format("Cannot convert value [%s] to double", v));
return defValue;
@@ -309,23 +325,24 @@ public final class ObjectUtils
}
/**
- * 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 o or 0
+ * @Deprecated use getDecimal() instead
+ * @param v the value to convert
+ * @return the decimal value
*/
- public static double getDouble(Object v)
+ @Deprecated
+ public static BigDecimal toDecimal(Object v)
{
- return getDouble(v, 0.0);
+ return valueUtils.toDecimal(v);
}
/**
- * converts an object to a decimal. If conversion is not possible, an
error is thrown
- * @param v the value to convert
- * @return the decimal value
+ * 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 toDecimal(Object v)
+ public static BigDecimal getDecimal(Object v)
{
return valueUtils.toDecimal(v);
}
@@ -345,24 +362,12 @@ public final class ObjectUtils
return defValue;
try
{ // Try to convert
- return toDecimal(v);
+ return valueUtils.toDecimal(v);
} catch (NumberFormatException e) {
log.error(String.format("Cannot convert value [%s] to BigDecimal",
v));
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 o or 0
- */
- public static BigDecimal getDecimal(Object v)
- {
- return getDecimal(v, BigDecimal.ZERO);
- }
/**
* Converts an object value to a boolean.
@@ -376,7 +381,7 @@ public final class ObjectUtils
*/
public static boolean getBoolean(Object v, boolean defValue)
{
- return valueUtils.getBoolean(v, defValue);
+ return valueUtils.toBoolean(v, defValue);
}
/**
@@ -387,7 +392,7 @@ public final class ObjectUtils
*/
public static boolean getBoolean(Object v)
{
- return getBoolean(v, false);
+ return valueUtils.toBoolean(v, false);
}
/**
@@ -399,7 +404,7 @@ public final class ObjectUtils
*/
public static <T extends Enum<?>> T getEnum(Class<T> enumType, Object
value)
{
- return valueUtils.getEnum(enumType, value);
+ return valueUtils.toEnum(enumType, value);
}
/**
@@ -411,7 +416,7 @@ public final class ObjectUtils
*/
public static <T extends Enum<?>> T getEnumByName(Class<T> enumType,
String name)
{
- return valueUtils.getEnumByName(enumType, name);
+ return valueUtils.toEnumByName(enumType, name);
}
/**
@@ -422,7 +427,7 @@ public final class ObjectUtils
*/
public static Object getEnumValue(Enum<?> enumValue, boolean isNumeric)
{
- return valueUtils.getEnumValue(enumValue, isNumeric);
+ return valueUtils.enumToValue(enumValue, isNumeric);
}
/**
@@ -432,7 +437,7 @@ public final class ObjectUtils
*/
public static String getString(Enum<?> enumValue)
{
- return valueUtils.getString(enumValue);
+ return valueUtils.enumToString(enumValue);
}
/**
@@ -442,14 +447,14 @@ public final class ObjectUtils
*/
public static String getString(Object value)
{
- return valueUtils.getString(value);
+ return valueUtils.toString(value);
}
/**
* Converts an object value to a Date.
* <P>
* @param v the object to convert
- * @return the Date value of o or null
+ * @return the Date value of v or null
*/
public static Date getDate(Object v)
{
@@ -464,7 +469,7 @@ public final class ObjectUtils
* Converts an object value to a LocalDate.
* <P>
* @param v the object to convert
- * @return the LocalDate value of o or null
+ * @return the LocalDate value of v or null
*/
public static LocalDate getLocalDate(Object v)
{
@@ -480,7 +485,7 @@ public final class ObjectUtils
* Converts an object value to a LocalDateTime.
* <P>
* @param v the object to convert
- * @return the LocalDateTime value of o or null
+ * @return the LocalDateTime value of v or null
*/
public static LocalDateTime getLocalDateTime(Object v)
{
@@ -492,6 +497,22 @@ public final class ObjectUtils
}
}
+ /**
+ * Converts an object value to a LocalDateTime.
+ * <P>
+ * @param v the object to convert
+ * @return the LocalDateTime value of v or null
+ */
+ public static Timestamp getTimestamp(Object v)
+ {
+ try {
+ // DateTimeFormatter.ISO_LOCAL_DATE_TIME
+ return valueUtils.toTimestamp(v);
+ } catch (DateTimeParseException e) {
+ throw new ValueConversionException(Timestamp.class, v, e);
+ }
+ }
+
/**
* Formats a given date object to a standard ISO date string.
* The format is "yyyy-MM-dd hh:mm:ss"
@@ -512,14 +533,14 @@ public final class ObjectUtils
* @param c the class type to convert to
* @param v the object to convert
*
- * @return the converted value of o or null
+ * @return the converted value of v or null
*
* @throws ClassCastException if the object is not null and is not
assignable to the type T.
*/
public static <T> T convert(Class<T> c, Object v)
throws ClassCastException
{
- return valueUtils.convert(c, v);
+ return valueUtils.convertToJava(c, v);
}
/**
@@ -531,7 +552,7 @@ public final class ObjectUtils
*/
public static Object convertValue(DataType type, Object value)
{
- return valueUtils.convertValue(type, value);
+ return valueUtils.convertToData(type, value);
}
/**
diff --git a/empire-db/src/main/java/org/apache/empire/commons/ValueUtils.java
b/empire-db/src/main/java/org/apache/empire/commons/ValueUtils.java
index 421a13e1..999a982c 100644
--- a/empire-db/src/main/java/org/apache/empire/commons/ValueUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/commons/ValueUtils.java
@@ -100,7 +100,7 @@ public class ValueUtils
if (o instanceof DBValueExpr)
return isEmpty(((DBValueExpr)o).getValue());
if (o instanceof Enum)
- return isEmpty(getString((Enum<?>)o));
+ return isEmpty(enumToString((Enum<?>)o));
// not empty
return false;
}
@@ -185,16 +185,16 @@ public class ValueUtils
if (o2 instanceof Number)
return ((Enum<?>)o1).ordinal()==((Number)o2).intValue();
// Compare Strings
- String strVal = getString((Enum<?>)o1);
- return StringUtils.compareEqual(strVal, getString(o2));
+ String strVal = enumToString((Enum<?>)o1);
+ return StringUtils.compareEqual(strVal, toString(o2));
}
else if (o2 instanceof Enum<?>)
{ // Special enum handling
if (o1 instanceof Number)
return ((Enum<?>)o2).ordinal()==((Number)o1).intValue();
// Compare Strings
- String strVal = getString((Enum<?>)o2);
- return StringUtils.compareEqual(strVal, getString(o1));
+ String strVal = enumToString((Enum<?>)o2);
+ return StringUtils.compareEqual(strVal, toString(o1));
}
// Compare Strings
if (o1 instanceof String)
@@ -267,7 +267,7 @@ public class ValueUtils
if (v instanceof Number)
return ((Number)v).intValue();
if (v instanceof Enum)
- return toInteger(getEnumValue(((Enum<?>)v), true));
+ return toInteger(enumToValue(((Enum<?>)v), true));
// Try to convert
String str = v.toString();
return Integer.parseInt(str);
@@ -285,7 +285,7 @@ public class ValueUtils
if (v instanceof Number)
return ((Number)v).longValue();
if (v instanceof Enum)
- return toLong(getEnumValue(((Enum<?>)v), true));
+ return toLong(enumToValue(((Enum<?>)v), true));
// Try to convert
String str = v.toString();
return Long.parseLong(str);
@@ -300,11 +300,11 @@ public class ValueUtils
{
// Get Double value
if (isEmpty(v))
- return 0.0;
+ return 0.0d;
if (v instanceof Number)
return ((Number)v).doubleValue();
if (v instanceof Enum)
- return toDouble(getEnumValue(((Enum<?>)v), true));
+ return toDouble(enumToValue(((Enum<?>)v), true));
// parse String for Integer value
String val = v.toString();
return Double.parseDouble(val);
@@ -335,7 +335,7 @@ public class ValueUtils
return BigDecimal.valueOf(((Number)v).doubleValue());
}
if (v instanceof Enum)
- return toDecimal(getEnumValue(((Enum<?>)v), true));
+ return toDecimal(enumToValue(((Enum<?>)v), true));
// parse String for Integer value
// Last-Chance > Try a string conversion
return new BigDecimal(v.toString());
@@ -351,7 +351,7 @@ public class ValueUtils
* @param defValue the default value
* @return the boolean value or defValue if v is null or empty
*/
- public boolean getBoolean(Object v, boolean defValue)
+ public boolean toBoolean(Object v, boolean defValue)
{
// Get Boolean value
if (isEmpty(v))
@@ -378,7 +378,7 @@ public class ValueUtils
* @return the enum
*/
@SuppressWarnings("unchecked")
- public <T extends Enum<?>> T getEnum(Class<T> enumType, Object value)
+ public <T extends Enum<?>> T toEnum(Class<T> enumType, Object value)
{ // check for null
if (isEmpty(value))
return null;
@@ -432,7 +432,7 @@ public class ValueUtils
* @param name the enum name
* @return the enum
*/
- public <T extends Enum<?>> T getEnumByName(Class<T> enumType, String name)
+ public <T extends Enum<?>> T toEnumByName(Class<T> enumType, String name)
{ // check for null
if (isEmpty(name))
return null;
@@ -451,13 +451,13 @@ public class ValueUtils
* @param isNumeric flag if number or string is required
* @return the number or string representing this enum
*/
- public Object getEnumValue(Enum<?> enumValue, boolean isNumeric)
+ public Object enumToValue(Enum<?> enumValue, boolean isNumeric)
{
// convert
if (enumValue instanceof EnumValue)
return ((EnumValue)enumValue).toValue(isNumeric);
// default
- return (isNumeric ? enumValue.ordinal() : getString(enumValue));
+ return (isNumeric ? enumValue.ordinal() : enumToString(enumValue));
}
/**
@@ -465,7 +465,7 @@ public class ValueUtils
* @param enumValue the enum
* @return the corresponding string value
*/
- public String getString(Enum<?> enumValue)
+ public String enumToString(Enum<?> enumValue)
{
// convert
if (enumValue instanceof EnumValue)
@@ -482,7 +482,7 @@ public class ValueUtils
* @param value the value to convert
* @return the corresponding string value
*/
- public String getString(Object value)
+ public String toString(Object value)
{
if (value==null)
return null;
@@ -492,7 +492,7 @@ public class ValueUtils
throw new InvalidValueException(value);
// convert
if (value instanceof Enum<?>)
- return getString((Enum<?>)value);
+ return enumToString((Enum<?>)value);
if (value instanceof Date)
return formatDate((Date)value, true);
// default
@@ -518,7 +518,7 @@ public class ValueUtils
* Converts an object value to a Date.
* <P>
* @param v the object to convert
- * @return the Date value of o or null
+ * @return the Date value of v or null
* @throws ParseException
*/
public Date toDate(Object v)
@@ -552,7 +552,7 @@ public class ValueUtils
* Converts an object value to a Date.
* <P>
* @param v the object to convert
- * @return the LocalDate value of o or null
+ * @return the LocalDate value of v or null
*/
public LocalDate toLocalDate(Object v)
{
@@ -579,7 +579,7 @@ public class ValueUtils
* Converts an object value to a Date.
* <P>
* @param v the object to convert
- * @return the LocalDateTime value of o or null
+ * @return the LocalDateTime value of v or null
*/
public LocalDateTime toLocalDateTime(Object v)
{
@@ -602,6 +602,33 @@ public class ValueUtils
return LocalDateTime.parse(str);
}
+ /**
+ * Converts an object value to a Timestamp.
+ * <P>
+ * @param v the object to convert
+ * @return the Timestamp or null
+ */
+ public Timestamp toTimestamp(Object v)
+ {
+ // Get DateTime value
+ if (isEmpty(v))
+ return null;
+ if (v instanceof java.sql.Timestamp)
+ return ((java.sql.Timestamp)v);
+ if (v instanceof java.time.LocalDate)
+ return Timestamp.valueOf(((LocalDate)v).atStartOfDay());
+ if (v instanceof java.time.LocalDateTime)
+ return Timestamp.valueOf((LocalDateTime)v);
+ if (v instanceof java.sql.Date)
+ return
Timestamp.valueOf(((java.sql.Date)v).toLocalDate().atStartOfDay());
+ if (v instanceof java.util.Date)
+ return Timestamp.valueOf(DateUtils.toLocalDateTime((Date)v));
+ // Convert from String
+ // DateTimeFormatter.ISO_LOCAL_DATE_TIME
+ String str = v.toString();
+ return Timestamp.valueOf(str);
+ }
+
/**
* Formats a given date object to a standard ISO date string.
* The format is "yyyy-MM-dd hh:mm:ss"
@@ -633,7 +660,7 @@ public class ValueUtils
* @throws ClassCastException if the object is not null and is not
assignable to the type T.
*/
@SuppressWarnings("unchecked")
- public <T> T convert(Class<T> c, Object v)
+ public <T> T convertToJava(Class<T> c, Object v)
throws ClassCastException
{
if (v==null || c.isInstance(v))
@@ -648,11 +675,11 @@ public class ValueUtils
// Convert
if (c.isEnum())
{ // convert to enum
- Object ev = getEnum((Class<? extends Enum<?>>)c, v);
+ Object ev = toEnum((Class<? extends Enum<?>>)c, v);
return (T)ev;
}
if (c.isAssignableFrom(Boolean.class))
- return c.cast(getBoolean(v, false));
+ return c.cast(toBoolean(v, false));
if (c.isAssignableFrom(Integer.class))
return c.cast(isEmpty(v) ? 0 : toInteger(v));
if (c.isAssignableFrom(Long.class))
@@ -662,7 +689,7 @@ public class ValueUtils
if(c.isAssignableFrom(BigDecimal.class))
return c.cast(isEmpty(v) ? BigDecimal.ZERO : toDecimal(v));
if (c.isAssignableFrom(String.class))
- return c.cast(getString(v));
+ return c.cast(toString(v));
// other
return c.cast(v);
}
@@ -674,7 +701,7 @@ public class ValueUtils
* @param value the value to convert
* @return the value to be used in SQL statements
*/
- public Object convertValue(DataType dataType, Object value)
+ public Object convertToData(DataType dataType, Object value)
{
// check null
if (value == null)
@@ -703,7 +730,7 @@ public class ValueUtils
case BLOB:
return value; // unchanged
case BOOL:
- return getBoolean(value, false);
+ return toBoolean(value, false);
case DATE:
case DATETIME:
case TIMESTAMP:
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 4e615941..959889ee 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.toDecimal(value);
+ value = ObjectUtils.getDecimal(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.toDouble(value);
+ value = ObjectUtils.getDouble(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.toLong(value);
+ value = ObjectUtils.getLong(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.toDecimal(n);
+ BigDecimal dv = ObjectUtils.getDecimal(n);
int prec = dv.precision();
int scale = dv.scale();
// check precision and scale