一、前言
关于empire-db源码包中org.apache.empire.commons.ObjectUtils字符串工具类,对对象类型判断、基本数据类型转换处理等。
二、源码说明
package org.apache.empire.commons;@b@@b@import java.io.Serializable;@b@import java.math.BigDecimal;@b@import java.math.BigInteger;@b@import java.text.SimpleDateFormat;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.Date;@b@import java.util.List;@b@import java.util.Locale;@b@import org.apache.commons.beanutils.MethodUtils;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public final class ObjectUtils@b@{@b@ public static final NoValue NO_VALUE = new NoValue(null);@b@ private static final Logger log = LoggerFactory.getLogger(ObjectUtils.class);@b@ private static final String DATE_FORMAT = "yyyy-MM-dd";@b@ private static final String DATETIME_FORMAT = "yyyy-MM-dd hh:mm:ss";@b@@b@ public static boolean isEmpty(Object o)@b@ {@b@ if ((o == null) || (o == NO_VALUE))@b@ return true;@b@@b@ return ((o instanceof String) && (((String)o).length() == 0));@b@ }@b@@b@ public static boolean compareEqual(Object o1, Object o2)@b@ {@b@ if (o1 == o2)@b@ return true;@b@@b@ if (isEmpty(o1))@b@ return isEmpty(o2);@b@ if (isEmpty(o2))@b@ return isEmpty(o1);@b@@b@ if (o1.getClass().equals(o2.getClass()))@b@ {@b@ if (o1 instanceof Comparable)@b@ return (((Comparable)o1).compareTo(o2) == 0);@b@@b@ return o1.equals(o2);@b@ }@b@@b@ if ((o1.equals(o2)) || (o2.equals(o1)))@b@ return true;@b@@b@ if ((o1 instanceof Number) && (o2 instanceof Number))@b@ {@b@ double d1 = ((Number)o1).doubleValue();@b@ double d2 = ((Number)o2).doubleValue();@b@ return (d1 == d2);@b@ }@b@@b@ return o1.toString().equals(o2.toString());@b@ }@b@@b@ public static <T> T coalesce(T preferred, T alternative)@b@ {@b@ return ((preferred != null) ? preferred : alternative);@b@ }@b@@b@ public static int toInteger(Object v)@b@ {@b@ if (isEmpty(v))@b@ return 0;@b@ if (v instanceof Number)@b@ return ((Number)v).intValue();@b@@b@ String str = v.toString();@b@ return Integer.parseInt(str);@b@ }@b@@b@ public static int getInteger(Object v, int defValue)@b@ {@b@ if (isEmpty(v))@b@ return defValue;@b@@b@ try@b@ {@b@ return toInteger(v);@b@ }@b@ catch (Exception e) {@b@ log.warn(String.format("Cannot convert value [%s] to int!", new Object[] { v })); }@b@ return defValue;@b@ }@b@@b@ public static int getInteger(Object v)@b@ {@b@ return getInteger(v, 0);@b@ }@b@@b@ public static long toLong(Object v)@b@ {@b@ if (isEmpty(v))@b@ return 0L;@b@ if (v instanceof Number)@b@ return ((Number)v).longValue();@b@@b@ String str = v.toString();@b@ return Long.parseLong(str);@b@ }@b@@b@ public static long getLong(Object v, long defValue)@b@ {@b@ if (isEmpty(v))@b@ return defValue;@b@@b@ try@b@ {@b@ return toLong(v);@b@ }@b@ catch (Exception e) {@b@ log.warn(String.format("Cannot convert value [%s] to long!", new Object[] { v })); }@b@ return defValue;@b@ }@b@@b@ public static long getLong(Object v)@b@ {@b@ return getLong(v, 0L);@b@ }@b@@b@ public static double toDouble(Object v)@b@ {@b@ if (isEmpty(v))@b@ return 0.0D;@b@ if (v instanceof Number)@b@ return ((Number)v).doubleValue();@b@@b@ String val = v.toString();@b@ return Double.parseDouble(val);@b@ }@b@@b@ public static double getDouble(Object v, double defValue)@b@ {@b@ if (isEmpty(v))@b@ return defValue;@b@ try@b@ {@b@ return toDouble(v);@b@ }@b@ catch (Exception e) {@b@ log.warn(String.format("Cannot convert value [%s] to double!", new Object[] { v })); }@b@ return defValue;@b@ }@b@@b@ public static double getDouble(Object v)@b@ {@b@ return getDouble(v, 0.0D);@b@ }@b@@b@ public static BigDecimal toDecimal(Object v)@b@ {@b@ if (isEmpty(v))@b@ return null;@b@ if (v instanceof BigDecimal)@b@ return ((BigDecimal)v);@b@@b@ if (v instanceof Number)@b@ {@b@ if (v instanceof BigInteger)@b@ return new BigDecimal((BigInteger)v);@b@ if (v instanceof Integer)@b@ return BigDecimal.valueOf(((Number)v).intValue());@b@ if (v instanceof Long)@b@ return BigDecimal.valueOf(((Number)v).longValue());@b@@b@ return BigDecimal.valueOf(((Number)v).doubleValue());@b@ }@b@@b@ return new BigDecimal(v.toString());@b@ }@b@@b@ public static BigDecimal getDecimal(Object v, BigDecimal defValue)@b@ {@b@ if (isEmpty(v))@b@ return defValue;@b@ try@b@ {@b@ return toDecimal(v);@b@ }@b@ catch (Exception e) {@b@ log.warn(String.format("Cannot convert value [%s] to BigDecimal!", new Object[] { v })); }@b@ return defValue;@b@ }@b@@b@ public static BigDecimal getDecimal(Object v)@b@ {@b@ return getDecimal(v, BigDecimal.ZERO);@b@ }@b@@b@ public static boolean getBoolean(Object v)@b@ {@b@ if (isEmpty(v))@b@ return false;@b@ if (v instanceof Boolean)@b@ return ((Boolean)v).booleanValue();@b@ if (v instanceof Number)@b@ return (((Number)v).intValue() != 0);@b@@b@ String val = v.toString();@b@ return ((val.equalsIgnoreCase("Y")) || (val.equalsIgnoreCase("true")));@b@ }@b@@b@ public static Date getDate(Object v, Locale locale)@b@ {@b@ if (isEmpty(v))@b@ return null;@b@ if (v instanceof Date)@b@ return ((Date)v);@b@@b@ if (v instanceof Number)@b@ {@b@ long l = ((Number)v).longValue();@b@ if (l == 0L) {@b@ return DateUtils.getDateNow();@b@ }@b@@b@ return new Date(l);@b@ }@b@@b@ return DateUtils.parseDate(v.toString(), locale);@b@ }@b@@b@ public static Date getDate(Object v)@b@ {@b@ if (isEmpty(v))@b@ return null;@b@ if (v instanceof Date)@b@ return ((Date)v);@b@ try@b@ {@b@ String str = v.toString();@b@ if (str.length() > 10)@b@ return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(str);@b@@b@ return new SimpleDateFormat("yyyy-MM-dd").parse(str);@b@ }@b@ catch (Exception e) {@b@ log.error("Cannot convert value to date!", e); }@b@ return null;@b@ }@b@@b@ public static String formatDate(Date date, boolean withTime)@b@ {@b@ if (withTime)@b@ return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);@b@@b@ return new SimpleDateFormat("yyyy-MM-dd").format(date);@b@ }@b@@b@ public static <T> T convert(Class<T> c, Object v)@b@ throws ClassCastException@b@ {@b@ if ((v == null) || (c.isInstance(v)))@b@ return v;@b@@b@ if (c.isPrimitive())@b@ {@b@ c = MethodUtils.getPrimitiveWrapper(c);@b@ }@b@@b@ if (c.isAssignableFrom(Boolean.class))@b@ return c.cast(Boolean.valueOf(getBoolean(v)));@b@ if (c.isAssignableFrom(Integer.class))@b@ return c.cast(Integer.valueOf(getInteger(v)));@b@ if (c.isAssignableFrom(Long.class))@b@ return c.cast(Long.valueOf(getLong(v)));@b@ if (c.isAssignableFrom(Double.class))@b@ return c.cast(Double.valueOf(getDouble(v)));@b@ if (c.isAssignableFrom(String.class))@b@ return c.cast(v.toString());@b@@b@ return c.cast(v);@b@ }@b@@b@ public static boolean isAssignmentCompatible(Class<?> target, Class<?> source)@b@ {@b@ if (target.isAssignableFrom(source))@b@ return true;@b@@b@ if (source.isPrimitive())@b@ {@b@ source = MethodUtils.getPrimitiveWrapper(source);@b@ if (source == null)@b@ return false;@b@ if (target.isAssignableFrom(source))@b@ return true;@b@ }@b@@b@ if (target.isPrimitive())@b@ {@b@ target = MethodUtils.getPrimitiveWrapper(target);@b@ if (target == null)@b@ return false;@b@ if (target.isAssignableFrom(source))@b@ return true;@b@ }@b@@b@ Class numberClass = Number.class;@b@ if ((numberClass.isAssignableFrom(target)) && (numberClass.isAssignableFrom(source)))@b@ {@b@ return true;@b@ }@b@@b@ return ((source == Character.class) && (target == String.class));@b@ }@b@@b@ public static <T> List<T> convert(Class<T> t, Collection<? extends T> source)@b@ {@b@ List target = new ArrayList();@b@ target.addAll(source);@b@ return target;@b@ }@b@@b@ public static String[] toStringArray(Object[] objArray, String defValue)@b@ {@b@ if (objArray == null)@b@ return null;@b@ String[] strArray = new String[objArray.length];@b@ for (int i = 0; i < objArray.length; ++i)@b@ {@b@ if (objArray[i] != null)@b@ strArray[i] = objArray[i].toString();@b@ else@b@ strArray[i] = defValue;@b@ }@b@ return strArray;@b@ }@b@@b@ public static <T> boolean contains(T[] array, T item)@b@ {@b@ if (array == null)@b@ return false;@b@ for (int i = 0; i < array.length; ++i)@b@ {@b@ if (array[i] == item)@b@ return true;@b@ if ((array[i] != null) && (array[i].equals(item)))@b@ return true;@b@ }@b@ return false;@b@ }@b@@b@ private static final class NoValue@b@ implements Serializable@b@ {@b@ private static final long serialVersionUID = 1L;@b@@b@ public String toString()@b@ {@b@ return "[NO-VALUE]";@b@ }@b@ }@b@}