/*
* @(#)NumberUtils.java 0.0.1 99/07/31
*
* Copyright (c) 1999 by Willie Wheeler. All rights reserved.
*/
package lang;
/**
* Contains various static utility methods for checking and
* converting Number objects.
*
* @version 0.0.1 07/31/99
* @author Willie Wheeler
*/
public class NumberUtils {
/**
* Indicates whether the passed object can be converted into a
* Double.
*
* @param o an object
* @return true if the object is either a Number
* or a parseable String, and false
* otherwise
* @see #asPrimDouble
* @see #asClassDouble
*/
public static boolean checkDouble(Object o) {
if (o instanceof Number) { return true; }
if (o instanceof String) {
try { Double.parseDouble((String)o); }
catch (NumberFormatException e) { return false; }
return true;
}
return false;
}
/**
* Indicates whether the passed object can be converted into an
* Integer.
*
* @param o an object
* @return true if the object is either a Number
* or a parseable String, and false
* otherwise
* @see #asPrimInt
* @see #asClassInt
*/
public static boolean checkInt(Object o) {
if (o instanceof Number) { return true; }
if (o instanceof String) {
try { Integer.parseInt((String)o); }
catch (NumberFormatException e) { return false; }
return true;
}
return false;
}
/**
* Converts the passed object into the primitive double
* equivalent.
*
* @param o an object
* @return the double equivalent
* @exception ClassCastException if the object is neither a
* Number or a String
* @exception NumberFormatException if the object is an
* unparseable String
*/
public static double asPrimDouble(Object o) {
if (o instanceof Number) { return ((Number)o).doubleValue(); }
return Double.parseDouble((String)o);
}
/**
* Converts the passed object into the primitive int equivalent.
*
* @param o an object
* @return the int equivalent
* @exception ClassCastException if the object is neither a
* Number or a String
* @exception NumberFormatException if the object is an
* unparseable String
*/
public static int asPrimInt(Object o) {
if (o instanceof Number) { return ((Number)o).intValue(); }
return Integer.parseInt((String)o);
}
/**
* Converts the passed object into the class Double equivalent.
*
* @param o an object
* @return the Double equivalent
* @exception ClassCastException if the object is neither a
* Number or a String
* @exception NumberFormatException if the object is an
* unparseable String
*/
public static Double asClassDouble(Object o) {
if (o instanceof Double) { return (Double)o; }
try { return new Double(asPrimDouble(o)); }
catch (Exception e) { return null; }
}
/**
* Converts the passed object into the class Integer equivalent.
*
* @param o an object
* @return the Integer equivalent
* @exception ClassCastException if the object is neither a
* Number or a String
* @exception NumberFormatException if the object is an
* unparseable String
*/
public static Integer asClassInt(Object o) {
if (o instanceof Integer) { return (Integer)o; }
try { return new Integer(asPrimInt(o)); }
catch (Exception e) { return null; }
}
}