|
From: <pat...@us...> - 2011-01-06 16:37:15
|
Revision: 1188
http://cishell.svn.sourceforge.net/cishell/?rev=1188&view=rev
Author: pataphil
Date: 2011-01-06 16:37:08 +0000 (Thu, 06 Jan 2011)
Log Message:
-----------
* Added NumberUtilities.interpretObjectAsNumber
* Minor cleaned-up in NumberUtilities and TableUtilities
* Reviewed by Joseph
Modified Paths:
--------------
trunk/core/org.cishell.utilities/src/org/cishell/utilities/NumberUtilities.java
trunk/core/org.cishell.utilities/src/org/cishell/utilities/TableUtilities.java
Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/NumberUtilities.java
===================================================================
--- trunk/core/org.cishell.utilities/src/org/cishell/utilities/NumberUtilities.java 2011-01-03 19:46:56 UTC (rev 1187)
+++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/NumberUtilities.java 2011-01-06 16:37:08 UTC (rev 1188)
@@ -2,25 +2,120 @@
import java.text.DecimalFormat;
import java.text.Format;
+import java.text.NumberFormat;
+import java.text.ParseException;
public class NumberUtilities {
public static final String UNROUNDED_DECIMAL_PATTERN =
"#.############################";
public static final String NOT_A_NUMBER_PREFIX = "NOT A NUMBER";
+
+ public static final String EMPTY_CELL_MESSAGE = "An empty number cell was found.";
+
+ public static Number interpretObjectAsNumber(Object object)
+ throws NumberFormatException, ParseException {
+ if (object instanceof Number) {
+ Number number = (Number) object;
+
+ return number;
+ } else if (object instanceof short[]) {
+ short[] objectAsShortArray = (short[]) object;
+
+ if (objectAsShortArray.length == 0) {
+ throw new NumberFormatException(EMPTY_CELL_MESSAGE);
+ } else {
+ return new Short(objectAsShortArray[0]);
+ }
+ } else if (object instanceof Short[]) {
+ Short[] objectAsShortArray = (Short[]) object;
+
+ if (objectAsShortArray.length == 0) {
+ throw new NumberFormatException(EMPTY_CELL_MESSAGE);
+ } else {
+ return objectAsShortArray[0];
+ }
+ } else if (object instanceof int[]) {
+ int[] objectAsIntArray = (int[]) object;
+
+ if (objectAsIntArray.length == 0) {
+ throw new NumberFormatException(EMPTY_CELL_MESSAGE);
+ } else {
+ return new Integer(objectAsIntArray[0]);
+ }
+ } else if (object instanceof Integer[]) {
+ Integer[] objectAsIntegerArray = (Integer[]) object;
+
+ if (objectAsIntegerArray.length == 0) {
+ throw new NumberFormatException(EMPTY_CELL_MESSAGE);
+ } else {
+ return objectAsIntegerArray[0];
+ }
+ } else if (object instanceof long[]) {
+ long[] objectAsLongArray = (long[]) object;
+
+ if (objectAsLongArray.length == 0) {
+ throw new NumberFormatException(EMPTY_CELL_MESSAGE);
+ } else {
+ return new Long(objectAsLongArray[0]);
+ }
+ } else if (object instanceof Long[]) {
+ Long[] objectAsLongArray = (Long[]) object;
+
+ if (objectAsLongArray.length == 0) {
+ throw new NumberFormatException(EMPTY_CELL_MESSAGE);
+ } else {
+ return objectAsLongArray[0];
+ }
+ } else if (object instanceof float[]) {
+ float[] objectAsFloatArray = (float[]) object;
+
+ if (objectAsFloatArray.length == 0) {
+ throw new NumberFormatException(EMPTY_CELL_MESSAGE);
+ } else {
+ return new Float(objectAsFloatArray[0]);
+ }
+ } else if (object instanceof Float[]) {
+ Float[] objectAsFloatArray = (Float[]) object;
+
+ if (objectAsFloatArray.length == 0) {
+ throw new NumberFormatException(EMPTY_CELL_MESSAGE);
+ } else {
+ return objectAsFloatArray[0];
+ }
+ } else if (object instanceof double[]) {
+ double[] objectAsDoubleArray = (double[]) object;
+
+ if (objectAsDoubleArray.length == 0) {
+ throw new NumberFormatException(EMPTY_CELL_MESSAGE);
+ } else {
+ return new Double(objectAsDoubleArray[0]);
+ }
+ } else if (object instanceof Double[]) {
+ Double[] objectAsDoubleArray = (Double[]) object;
+
+ if (objectAsDoubleArray.length == 0) {
+ throw new NumberFormatException(EMPTY_CELL_MESSAGE);
+ } else {
+ return objectAsDoubleArray[0];
+ }
+ }
+
+ NumberFormat numberFormat = NumberFormat.getInstance();
+
+ return numberFormat.parse(object.toString());
+ }
public static Double interpretObjectAsDouble(Object object)
throws NumberFormatException {
- final String EMPTY_CELL_MESSAGE = "An empty number cell was found.";
-
// TODO: These if's are a result of a "bug" in Prefuse's.
// CSV Table Reader, which interprets a column as being an array type
// if it has empty cells.
if (object instanceof Number) {
- Number number = (Number)object;
+ Number number = (Number) object;
return new Double(number.doubleValue());
} else if (object instanceof short[]) {
- short[] objectAsShortArray = (short[])object;
+ short[] objectAsShortArray = (short[]) object;
if (objectAsShortArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
@@ -28,7 +123,7 @@
return new Double(objectAsShortArray[0]);
}
} else if (object instanceof Short[]) {
- Short[] objectAsShortArray = (Short[])object;
+ Short[] objectAsShortArray = (Short[]) object;
if (objectAsShortArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
@@ -36,7 +131,7 @@
return new Double(objectAsShortArray[0].doubleValue());
}
} else if (object instanceof int[]) {
- int[] objectAsIntArray = (int[])object;
+ int[] objectAsIntArray = (int[]) object;
if (objectAsIntArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
@@ -44,7 +139,7 @@
return new Double(objectAsIntArray[0]);
}
} else if (object instanceof Integer[]) {
- Integer[] objectAsIntegerArray = (Integer[])object;
+ Integer[] objectAsIntegerArray = (Integer[]) object;
if (objectAsIntegerArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
@@ -52,7 +147,7 @@
return new Double(objectAsIntegerArray[0].doubleValue());
}
} else if (object instanceof long[]) {
- long[] objectAsLongArray = (long[])object;
+ long[] objectAsLongArray = (long[]) object;
if (objectAsLongArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
@@ -60,7 +155,7 @@
return new Double(objectAsLongArray[0]);
}
} else if (object instanceof Long[]) {
- Long[] objectAsLongArray = (Long[])object;
+ Long[] objectAsLongArray = (Long[]) object;
if (objectAsLongArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
@@ -68,7 +163,7 @@
return new Double(objectAsLongArray[0].doubleValue());
}
} else if (object instanceof float[]) {
- float[] objectAsFloatArray = (float[])object;
+ float[] objectAsFloatArray = (float[]) object;
if (objectAsFloatArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
@@ -76,7 +171,7 @@
return new Double(objectAsFloatArray[0]);
}
} else if (object instanceof Float[]) {
- Float[] objectAsFloatArray = (Float[])object;
+ Float[] objectAsFloatArray = (Float[]) object;
if (objectAsFloatArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
@@ -84,7 +179,7 @@
return new Double(objectAsFloatArray[0].doubleValue());
}
} else if (object instanceof double[]) {
- double[] objectAsDoubleArray = (double[])object;
+ double[] objectAsDoubleArray = (double[]) object;
if (objectAsDoubleArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
@@ -92,7 +187,7 @@
return new Double(objectAsDoubleArray[0]);
}
} else if (object instanceof Double[]) {
- Double[] objectAsDoubleArray = (Double[])object;
+ Double[] objectAsDoubleArray = (Double[]) object;
if (objectAsDoubleArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/TableUtilities.java
===================================================================
--- trunk/core/org.cishell.utilities/src/org/cishell/utilities/TableUtilities.java 2011-01-03 19:46:56 UTC (rev 1187)
+++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/TableUtilities.java 2011-01-06 16:37:08 UTC (rev 1188)
@@ -11,6 +11,50 @@
import prefuse.util.collections.IntIterator;
public class TableUtilities {
+ public static final Class<?>[] POSSIBLE_NUMBER_CLASSES = {
+ byte.class,
+ byte[].class,
+ Byte.class,
+ Byte[].class,
+ short.class,
+ short[].class,
+ Short.class,
+ Short[].class,
+ int.class,
+ int[].class,
+ Integer.class,
+ Integer[].class,
+ long.class,
+ long[].class,
+ Long.class,
+ Long[].class,
+ float.class,
+ float[].class,
+ Float.class,
+ Float[].class,
+ double.class,
+ double[].class,
+ Double.class,
+ Double[].class
+ };
+
+ public static final Class<?>[] POSSIBLE_INTEGER_CLASSES = {
+ int.class,
+ Integer.class,
+ int[].class,
+ Integer[].class
+ };
+
+ public static final Class<?>[] POSSIBLE_DATE_CLASSES = {
+ Date.class,
+ int.class,
+ Integer.class,
+ String.class,
+ int[].class,
+ Integer[].class,
+ String[].class,
+ };
+
public static Table copyTable(Table oldTable) {
Schema oldSchema = oldTable.getSchema();
Table newTable = oldSchema.instantiate();
@@ -87,61 +131,17 @@
public static String[] getValidNumberColumnNamesInTable(Table table)
throws ColumnNotFoundException {
- Class<?>[] possibleNumberClasses = {
- byte.class,
- byte[].class,
- Byte.class,
- Byte[].class,
- short.class,
- short[].class,
- Short.class,
- Short[].class,
- int.class,
- int[].class,
- Integer.class,
- Integer[].class,
- long.class,
- long[].class,
- Long.class,
- Long[].class,
- float.class,
- float[].class,
- Float.class,
- Float[].class,
- double.class,
- double[].class,
- Double.class,
- Double[].class
- };
-
- return filterSchemaColumnNamesByClasses(table.getSchema(), possibleNumberClasses);
+ return filterSchemaColumnNamesByClasses(table.getSchema(), POSSIBLE_NUMBER_CLASSES);
}
public static String[] getValidIntegerColumnNamesInTable(Table table)
throws ColumnNotFoundException {
- Class<?>[] possibleIntegerClasses = {
- int.class,
- Integer.class,
- int[].class,
- Integer[].class
- };
-
- return filterSchemaColumnNamesByClasses(table.getSchema(), possibleIntegerClasses);
+ return filterSchemaColumnNamesByClasses(table.getSchema(), POSSIBLE_INTEGER_CLASSES);
}
public static String[] getValidDateColumnNamesInTable(Table table)
throws ColumnNotFoundException {
- Class<?>[] possibleDateClasses = {
- Date.class,
- int.class,
- Integer.class,
- String.class,
- int[].class,
- Integer[].class,
- String[].class,
- };
-
- return filterSchemaColumnNamesByClasses(table.getSchema(), possibleDateClasses);
+ return filterSchemaColumnNamesByClasses(table.getSchema(), POSSIBLE_DATE_CLASSES);
}
public static String[] filterSchemaColumnNamesByClasses(
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|