0,0 → 1,88 |
package ak.itru.util; |
|
import java.math.BigDecimal; |
import java.util.Date; |
import java.text.DateFormat; |
import java.text.NumberFormat; |
import java.text.DecimalFormat; |
import java.text.ParseException; |
|
public abstract class StringConverter |
{ |
public static boolean isEmpty(Object o) |
{ |
if(o == null) |
return true; |
else if((o instanceof String) && ((String)o).equals("")) |
return true; |
else |
return false; |
} |
|
public static String preparse(Object o) |
{ |
if(o instanceof String) { |
String s = (String)o; |
if(s == null || s.equals("")) |
return null; |
else |
return s; |
} |
else { |
throw new ClassCastException("String is expected, but it is " + o.getClass()); |
} |
} |
|
public static Long parseLong(Object o) |
throws NumberFormatException |
{ |
String s = preparse(o); |
return (s == null) ? null : new Long(s); |
} |
|
public static Integer parseInteger(Object o) |
throws NumberFormatException |
{ |
String s = preparse(o); |
return (s == null) ? null : new Integer(s); |
} |
|
public static BigDecimal parseCurrency(Object o) |
throws NumberFormatException, ParseException |
{ |
String s = preparse(o); |
return (s == null) |
? null : new BigDecimal(NumberFormat.getInstance().parse(s).doubleValue()); |
} |
|
public static String toCurrency(Number n) |
{ |
if(n == null) |
return null; |
else |
return (new DecimalFormat("0.00")).format(n); |
} |
|
public static Date parseDate(Object o) |
throws NumberFormatException, ParseException |
{ |
String s = preparse(o); |
return (s == null) ? null : DateFormat.getDateInstance().parse(s); |
} |
|
public static String toDate(Date d) |
{ |
if(d == null) |
return null; |
else |
return DateFormat.getDateInstance().format(d); |
} |
|
public static String toString(Object o) |
{ |
if(o == null) |
return null; |
else |
return o.toString(); |
} |
} |