You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@empire-db.apache.org by do...@apache.org on 2011/12/09 22:29:43 UTC
svn commit: r1212646 - in
/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire:
commons/ObjectUtils.java db/DBRecordData.java
Author: doebele
Date: Fri Dec 9 21:29:42 2011
New Revision: 1212646
URL: http://svn.apache.org/viewvc?rev=1212646&view=rev
Log:
EMPIREDB-126
Added getters for BigDecimal to DBRecordData for convenience.
Modified:
incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java
Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java?rev=1212646&r1=1212645&r2=1212646&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java (original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java Fri Dec 9 21:29:42 2011
@@ -18,6 +18,8 @@
*/
package org.apache.empire.commons;
+import java.math.BigDecimal;
+import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
@@ -248,6 +250,56 @@ public final class ObjectUtils
{
return getDouble(v, 0.0);
}
+
+ /**
+ * Converts an object value to a BigDecimal.
+ * <P>
+ * 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 BigDecimal value of v or defValue
+ */
+ public static BigDecimal getDecimal(Object v, BigDecimal defValue)
+ {
+ // Get Double value
+ if (v==null)
+ return defValue;
+ if (v instanceof BigDecimal)
+ return ((BigDecimal)v);
+ // Find a suitable converter
+ if (v instanceof Number)
+ { // check other number types
+ if (v instanceof BigInteger)
+ return new BigDecimal((BigInteger)v);
+ if (v instanceof Integer)
+ return BigDecimal.valueOf(((Number)v).intValue());
+ if (v instanceof Long)
+ return BigDecimal.valueOf(((Number)v).longValue());
+ // Default: convert via double
+ return BigDecimal.valueOf(((Number)v).doubleValue());
+ }
+ // parse String for Integer value
+ try
+ { // Last-Chance > Try a string conversion
+ return new BigDecimal(v.toString());
+ } catch (Exception e)
+ {
+ log.warn(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.
Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java?rev=1212646&r1=1212645&r2=1212646&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java (original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java Fri Dec 9 21:29:42 2011
@@ -19,6 +19,7 @@
package org.apache.empire.db;
// XML
import java.lang.reflect.InvocationTargetException;
+import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
@@ -140,8 +141,8 @@ public abstract class DBRecordData exten
public double getDouble(int index)
{
// Get Double value
- Object o = getValue(index);
- return ObjectUtils.getDouble(o);
+ Object v = getValue(index);
+ return ObjectUtils.getDouble(v);
}
/**
@@ -155,6 +156,32 @@ public abstract class DBRecordData exten
{
return getDouble(getFieldIndex(column));
}
+
+ /**
+ * Returns a data value identified by the column index.
+ * The data value is converted to double if necessary.
+ *
+ * @param index index of the column
+ * @return the value
+ */
+ public BigDecimal getDecimal(int index)
+ {
+ // Get Double value
+ Object v = getValue(index);
+ return ObjectUtils.getDecimal(v);
+ }
+
+ /**
+ * Returns a data value for the desired column.
+ * The data value is converted to BigDecimal if necessary.
+ *
+ * @param column identifying the column
+ * @return the value
+ */
+ public final BigDecimal getDecimal(ColumnExpr column)
+ {
+ return getDecimal(getFieldIndex(column));
+ }
/**
* Returns a data value identified by the column index.