Permalink
Browse files

Added a utility to parse an object as an Integer.

Related to SCISQUARED-462.

Reviewed by Joseph.
  • Loading branch information...
1 parent 981676d commit 1e83327e1717251e6b4d39d03900547ac2b22184 dmcoe committed Apr 23, 2012
View
8 core/org.cishell.utilities/src/org/cishell/utilities/IntegerParserWithDefault.java
@@ -3,6 +3,14 @@
public class IntegerParserWithDefault {
public static final Integer DEFAULT = null;
+ /**
+ * Please see {@link NumberUtilities} for alternatives, specifically
+ * {@link NumberUtilities#interpretObjectAsInteger(Object)}.
+ *
+ * @param target
+ * @return
+ */
+ @Deprecated
public static Integer parse(String target) {
try {
return Integer.parseInt(target);
View
197 core/org.cishell.utilities/src/org/cishell/utilities/NumberUtilities.java
@@ -6,8 +6,7 @@
import java.text.ParseException;
public class NumberUtilities {
- public static final String UNROUNDED_DECIMAL_PATTERN =
- "#.############################";
+ 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.";
@@ -16,225 +15,206 @@ 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]);
}
+ 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];
}
+ 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]);
}
+ 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];
}
+ 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]);
}
+ 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];
}
+ 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]);
}
+ 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];
}
+ 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]);
}
+ 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];
}
+ return objectAsDoubleArray[0];
}
-
+
NumberFormat numberFormat = NumberFormat.getInstance();
-
+
return numberFormat.parse(object.toString());
}
-
+
public static Double interpretObjectAsDouble(Object object)
throws NumberFormatException {
// 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;
-
+
return new Double(number.doubleValue());
} else if (object instanceof short[]) {
short[] objectAsShortArray = (short[]) object;
-
+
if (objectAsShortArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- } else {
- return new Double(objectAsShortArray[0]);
}
+ return new Double(objectAsShortArray[0]);
} else if (object instanceof Short[]) {
Short[] objectAsShortArray = (Short[]) object;
-
+
if (objectAsShortArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- } else {
- return new Double(objectAsShortArray[0].doubleValue());
}
+ return new Double(objectAsShortArray[0].doubleValue());
} else if (object instanceof int[]) {
int[] objectAsIntArray = (int[]) object;
-
+
if (objectAsIntArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- } else {
- return new Double(objectAsIntArray[0]);
}
+ return new Double(objectAsIntArray[0]);
} else if (object instanceof Integer[]) {
Integer[] objectAsIntegerArray = (Integer[]) object;
-
+
if (objectAsIntegerArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- } else {
- return new Double(objectAsIntegerArray[0].doubleValue());
}
+ return new Double(objectAsIntegerArray[0].doubleValue());
} else if (object instanceof long[]) {
long[] objectAsLongArray = (long[]) object;
-
+
if (objectAsLongArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- } else {
- return new Double(objectAsLongArray[0]);
}
+ return new Double(objectAsLongArray[0]);
} else if (object instanceof Long[]) {
Long[] objectAsLongArray = (Long[]) object;
-
+
if (objectAsLongArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- } else {
- return new Double(objectAsLongArray[0].doubleValue());
}
+ return new Double(objectAsLongArray[0].doubleValue());
} else if (object instanceof float[]) {
float[] objectAsFloatArray = (float[]) object;
-
+
if (objectAsFloatArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- } else {
- return new Double(objectAsFloatArray[0]);
}
+ return new Double(objectAsFloatArray[0]);
} else if (object instanceof Float[]) {
Float[] objectAsFloatArray = (Float[]) object;
-
+
if (objectAsFloatArray.length == 0) {
throw new NumberFormatException(EMPTY_CELL_MESSAGE);
- } else {
- return new Double(objectAsFloatArray[0].doubleValue());
}
+ return new Double(objectAsFloatArray[0].doubleValue());
} 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]);
}
+ 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];
}
+ return objectAsDoubleArray[0];
}
-
+
String objectAsString = object.toString();
-
+
return new Double(objectAsString);
}
-
+
// TODO: Make the plot/csv converter use these versions.
public static String convertToDecimalNotation(double number) {
String numberAsString = new Double(number).toString();
-
+
return convertToDecimalNotation(numberAsString);
}
-
- /*
- * If numberAsString holds a number in scientific notation,
- * convert it to decimal notation.
+
+ /*
+ * If numberAsString holds a number in scientific notation, convert it to
+ * decimal notation.
*/
public static String convertToDecimalNotation(String numberAsString) {
// Check for a scientific notation delimiter.
- if (numberAsString.indexOf("E") != -1 || numberAsString.indexOf("e") != -1) {
- Format format =
- new DecimalFormat(UNROUNDED_DECIMAL_PATTERN);
-
+ if (numberAsString.indexOf("E") != -1
+ || numberAsString.indexOf("e") != -1) {
+ Format format = new DecimalFormat(UNROUNDED_DECIMAL_PATTERN);
+
try {
return format.format(new Double(numberAsString));
} catch (NumberFormatException numberFormatException) {
return NOT_A_NUMBER_PREFIX + " (" + numberAsString + ")";
}
- } else {
- return numberAsString;
}
+ return numberAsString;
}
- public static double roundToNDecimalPlaces(double original, int decimalPlaceCount) {
- String formatString = "#." + StringUtilities.multiply("#", decimalPlaceCount);
+ public static double roundToNDecimalPlaces(double original,
+ int decimalPlaceCount) {
+ String formatString = "#."
+ + StringUtilities.multiply("#", decimalPlaceCount);
DecimalFormat format = new DecimalFormat(formatString);
return Double.valueOf(format.format(original));
}
-
+
public static boolean isEven(long number) {
return ((number % 2) == 0);
}
@@ -258,4 +238,53 @@ public static boolean isOdd(float target) {
public static boolean isOdd(double target) {
return !isEven(target);
}
+
+ /**
+ * The default return value to use for {@link #interpretObjectAsInteger}.
+ */
+ public static final Integer INTERPRET_OBJECT_AS_INTEGER_DEFAULT = null;
+
+ /**
+ * Try to interpret an {@link Object} as an {@link Integer}. <br />
+ * If you wish to use a custom default, see
+ * {@link #interpretObjectAsInteger(Object, Integer)}.
+ *
+ * @return The {@link Integer} if the {@code target} can be parsed,
+ * {@link NumberUtilities#INTERPRET_OBJECT_AS_INTEGER_DEFAULT}
+ * otherwise.
+ */
+ public static Integer interpretObjectAsInteger(Object object) {
+ return interpretObjectAsInteger(object,
+ INTERPRET_OBJECT_AS_INTEGER_DEFAULT);
+ }
+
+ /**
+ * Try to interpret an {@link Object} as an {@link Integer}.
+ *
+ * @param defaultValue
+ * The value to return if the {@code target} can not be parsed.
+ *
+ * @return The {@link Integer} if the {@code target} can be parsed,
+ * {@code defaultValue} otherwise.
+ */
+ public static Integer interpretObjectAsInteger(Object object,
+ Integer defaultValue) {
+ if (object instanceof Integer) {
+ return (Integer) object;
+ } else if (object instanceof Number) {
+ return ((Number) object).intValue();
+ } else if (object instanceof String) {
+ try {
+ return Integer.valueOf((String) object);
+ } catch (NumberFormatException e) {
+ return defaultValue;
+ }
+ } else {
+ try {
+ return Integer.valueOf(String.valueOf(object));
+ } catch (NumberFormatException e) {
+ return defaultValue;
+ }
+ }
+ }
}

0 comments on commit 1e83327

Please sign in to comment.