一、前言
通过定义ObjectUtils对象工具类,实现Throwable抛出异常检查过滤(isCheckedException/isCompatibleWithThrowsClause)、对象数组追加元素(addObjectToArray)、对象转数组(toObjectArray)、对象值是否相等(nullSafeEquals)、对象及常见数据类型hashCode自定义(nullSafeHashCode)、对象及常见数据类型转字符串类型(nullSafeToString)等。
二、代码示例
import java.lang.reflect.Array;@b@import java.util.Arrays;@b@@b@public abstract class ObjectUtils@b@{@b@ private static final int INITIAL_HASH = 7;@b@ private static final int MULTIPLIER = 31;@b@ private static final String EMPTY_STRING = "";@b@ private static final String NULL_STRING = "null";@b@ private static final String ARRAY_START = "{";@b@ private static final String ARRAY_END = "}";@b@ private static final String EMPTY_ARRAY = "{}";@b@ private static final String ARRAY_ELEMENT_SEPARATOR = ", ";@b@@b@ public static boolean isCheckedException(Throwable ex)@b@ {@b@ return ((!(ex instanceof RuntimeException)) && (!(ex instanceof Error)));@b@ }@b@@b@ public static boolean isCompatibleWithThrowsClause(Throwable ex, Class[] declaredExceptions)@b@ {@b@ if (!(isCheckedException(ex)))@b@ return true;@b@@b@ if (declaredExceptions != null) {@b@ int i = 0;@b@ while (i < declaredExceptions.length) {@b@ if (declaredExceptions[i].isAssignableFrom(ex.getClass()))@b@ return true;@b@@b@ ++i;@b@ }@b@ }@b@ return false;@b@ }@b@@b@ public static boolean isArray(Object obj)@b@ {@b@ return ((obj != null) && (obj.getClass().isArray()));@b@ }@b@@b@ public static boolean isEmpty(Object[] array)@b@ {@b@ return ((array == null) || (array.length == 0));@b@ }@b@@b@ public static boolean containsElement(Object[] array, Object element)@b@ {@b@ if (array == null)@b@ return false;@b@@b@ for (Object arrayEle : array)@b@ if (nullSafeEquals(arrayEle, element))@b@ return true;@b@@b@@b@ return false;@b@ }@b@@b@ public static Object[] addObjectToArray(Object[] array, Object obj)@b@ {@b@ Class compType = Object.class;@b@ if (array != null) {@b@ compType = array.getClass().getComponentType();@b@ }@b@ else if (obj != null)@b@ compType = obj.getClass();@b@@b@ int newArrLength = (array != null) ? array.length + 1 : 1;@b@ Object[] newArr = (Object[])Array.newInstance(compType, newArrLength);@b@ if (array != null)@b@ System.arraycopy(array, 0, newArr, 0, array.length);@b@@b@ newArr[(newArr.length - 1)] = obj;@b@ return newArr;@b@ }@b@@b@ public static Object[] toObjectArray(Object source)@b@ {@b@ if (source instanceof Object[])@b@ return ((Object[])source);@b@@b@ if (source == null)@b@ return new Object[0];@b@@b@ if (!(source.getClass().isArray()))@b@ throw new IllegalArgumentException("Source is not an array: " + source);@b@@b@ int length = Array.getLength(source);@b@ if (length == 0)@b@ return new Object[0];@b@@b@ Class wrapperType = Array.get(source, 0).getClass();@b@ Object[] newArray = (Object[])Array.newInstance(wrapperType, length);@b@ for (int i = 0; i < length; ++i)@b@ newArray[i] = Array.get(source, i);@b@@b@ return newArray;@b@ }@b@@b@ public static boolean nullSafeEquals(Object o1, Object o2)@b@ {@b@ if (o1 == o2)@b@ return true;@b@@b@ if ((o1 == null) || (o2 == null))@b@ return false;@b@@b@ if (o1.equals(o2))@b@ return true;@b@@b@ if ((o1.getClass().isArray()) && (o2.getClass().isArray())) {@b@ if ((o1 instanceof Object[]) && (o2 instanceof Object[]))@b@ return Arrays.equals((Object[])o1, (Object[])o2);@b@@b@ if ((o1 instanceof boolean[]) && (o2 instanceof boolean[]))@b@ return Arrays.equals((boolean[])o1, (boolean[])o2);@b@@b@ if ((o1 instanceof byte[]) && (o2 instanceof byte[]))@b@ return Arrays.equals((byte[])o1, (byte[])o2);@b@@b@ if ((o1 instanceof char[]) && (o2 instanceof char[]))@b@ return Arrays.equals((char[])o1, (char[])o2);@b@@b@ if ((o1 instanceof double[]) && (o2 instanceof double[]))@b@ return Arrays.equals((double[])o1, (double[])o2);@b@@b@ if ((o1 instanceof float[]) && (o2 instanceof float[]))@b@ return Arrays.equals((float[])o1, (float[])o2);@b@@b@ if ((o1 instanceof int[]) && (o2 instanceof int[]))@b@ return Arrays.equals((int[])o1, (int[])o2);@b@@b@ if ((o1 instanceof long[]) && (o2 instanceof long[]))@b@ return Arrays.equals((long[])o1, (long[])o2);@b@@b@ if ((o1 instanceof short[]) && (o2 instanceof short[]))@b@ return Arrays.equals((short[])o1, (short[])o2);@b@ }@b@@b@ return false;@b@ }@b@@b@ public static int nullSafeHashCode(Object obj)@b@ {@b@ if (obj == null)@b@ return 0;@b@@b@ if (obj.getClass().isArray()) {@b@ if (obj instanceof Object[])@b@ return nullSafeHashCode((Object[])obj);@b@@b@ if (obj instanceof boolean[])@b@ return nullSafeHashCode((boolean[])obj);@b@@b@ if (obj instanceof byte[])@b@ return nullSafeHashCode((byte[])obj);@b@@b@ if (obj instanceof char[])@b@ return nullSafeHashCode((char[])obj);@b@@b@ if (obj instanceof double[])@b@ return nullSafeHashCode((double[])obj);@b@@b@ if (obj instanceof float[])@b@ return nullSafeHashCode((float[])obj);@b@@b@ if (obj instanceof int[])@b@ return nullSafeHashCode((int[])obj);@b@@b@ if (obj instanceof long[])@b@ return nullSafeHashCode((long[])obj);@b@@b@ if (obj instanceof short[])@b@ return nullSafeHashCode((short[])obj);@b@ }@b@@b@ return obj.hashCode();@b@ }@b@@b@ public static int nullSafeHashCode(Object[] array)@b@ {@b@ if (array == null)@b@ return 0;@b@@b@ int hash = 7;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; ++i)@b@ hash = 31 * hash + nullSafeHashCode(array[i]);@b@@b@ return hash;@b@ }@b@@b@ public static int nullSafeHashCode(boolean[] array)@b@ {@b@ if (array == null)@b@ return 0;@b@@b@ int hash = 7;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; ++i)@b@ hash = 31 * hash + hashCode(array[i]);@b@@b@ return hash;@b@ }@b@@b@ public static int nullSafeHashCode(byte[] array)@b@ {@b@ if (array == null)@b@ return 0;@b@@b@ int hash = 7;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; ++i)@b@ hash = 31 * hash + array[i];@b@@b@ return hash;@b@ }@b@@b@ public static int nullSafeHashCode(char[] array)@b@ {@b@ if (array == null)@b@ return 0;@b@@b@ int hash = 7;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; ++i)@b@ hash = 31 * hash + array[i];@b@@b@ return hash;@b@ }@b@@b@ public static int nullSafeHashCode(double[] array)@b@ {@b@ if (array == null)@b@ return 0;@b@@b@ int hash = 7;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; ++i)@b@ hash = 31 * hash + hashCode(array[i]);@b@@b@ return hash;@b@ }@b@@b@ public static int nullSafeHashCode(float[] array)@b@ {@b@ if (array == null)@b@ return 0;@b@@b@ int hash = 7;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; ++i)@b@ hash = 31 * hash + hashCode(array[i]);@b@@b@ return hash;@b@ }@b@@b@ public static int nullSafeHashCode(int[] array)@b@ {@b@ if (array == null)@b@ return 0;@b@@b@ int hash = 7;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; ++i)@b@ hash = 31 * hash + array[i];@b@@b@ return hash;@b@ }@b@@b@ public static int nullSafeHashCode(long[] array)@b@ {@b@ if (array == null)@b@ return 0;@b@@b@ int hash = 7;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; ++i)@b@ hash = 31 * hash + hashCode(array[i]);@b@@b@ return hash;@b@ }@b@@b@ public static int nullSafeHashCode(short[] array)@b@ {@b@ if (array == null)@b@ return 0;@b@@b@ int hash = 7;@b@ int arraySize = array.length;@b@ for (int i = 0; i < arraySize; ++i)@b@ hash = 31 * hash + array[i];@b@@b@ return hash;@b@ }@b@@b@ public static int hashCode(boolean bool)@b@ {@b@ return ((bool) ? 1231 : 1237);@b@ }@b@@b@ public static int hashCode(double dbl)@b@ {@b@ long bits = Double.doubleToLongBits(dbl);@b@ return hashCode(bits);@b@ }@b@@b@ public static int hashCode(float flt)@b@ {@b@ return Float.floatToIntBits(flt);@b@ }@b@@b@ public static int hashCode(long lng)@b@ {@b@ return (int)(lng ^ lng >>> 32);@b@ }@b@@b@ public static String identityToString(Object obj)@b@ {@b@ if (obj == null)@b@ return "";@b@@b@ return obj.getClass().getName() + "@" + getIdentityHexString(obj);@b@ }@b@@b@ public static String getIdentityHexString(Object obj)@b@ {@b@ return Integer.toHexString(System.identityHashCode(obj));@b@ }@b@@b@ public static String getDisplayString(Object obj)@b@ {@b@ if (obj == null)@b@ return "";@b@@b@ return nullSafeToString(obj);@b@ }@b@@b@ public static String nullSafeClassName(Object obj)@b@ {@b@ return ((obj != null) ? obj.getClass().getName() : "null");@b@ }@b@@b@ public static String nullSafeToString(Object obj)@b@ {@b@ if (obj == null)@b@ return "null";@b@@b@ if (obj instanceof String)@b@ return ((String)obj);@b@@b@ if (obj instanceof Object[])@b@ return nullSafeToString((Object[])obj);@b@@b@ if (obj instanceof boolean[])@b@ return nullSafeToString((boolean[])obj);@b@@b@ if (obj instanceof byte[])@b@ return nullSafeToString((byte[])obj);@b@@b@ if (obj instanceof char[])@b@ return nullSafeToString((char[])obj);@b@@b@ if (obj instanceof double[])@b@ return nullSafeToString((double[])obj);@b@@b@ if (obj instanceof float[])@b@ return nullSafeToString((float[])obj);@b@@b@ if (obj instanceof int[])@b@ return nullSafeToString((int[])obj);@b@@b@ if (obj instanceof long[])@b@ return nullSafeToString((long[])obj);@b@@b@ if (obj instanceof short[])@b@ return nullSafeToString((short[])obj);@b@@b@ String str = obj.toString();@b@ return ((str != null) ? str : "");@b@ }@b@@b@ public static String nullSafeToString(Object[] array)@b@ {@b@ if (array == null)@b@ return "null";@b@@b@ int length = array.length;@b@ if (length == 0)@b@ return "{}";@b@@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; ++i) {@b@ if (i == 0) {@b@ sb.append("{");@b@ }@b@ else@b@ sb.append(", ");@b@@b@ sb.append(String.valueOf(array[i]));@b@ }@b@ sb.append("}");@b@ return sb.toString();@b@ }@b@@b@ public static String nullSafeToString(boolean[] array)@b@ {@b@ if (array == null)@b@ return "null";@b@@b@ int length = array.length;@b@ if (length == 0)@b@ return "{}";@b@@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; ++i) {@b@ if (i == 0) {@b@ sb.append("{");@b@ }@b@ else {@b@ sb.append(", ");@b@ }@b@@b@ sb.append(array[i]);@b@ }@b@ sb.append("}");@b@ return sb.toString();@b@ }@b@@b@ public static String nullSafeToString(byte[] array)@b@ {@b@ if (array == null)@b@ return "null";@b@@b@ int length = array.length;@b@ if (length == 0)@b@ return "{}";@b@@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; ++i) {@b@ if (i == 0) {@b@ sb.append("{");@b@ }@b@ else@b@ sb.append(", ");@b@@b@ sb.append(array[i]);@b@ }@b@ sb.append("}");@b@ return sb.toString();@b@ }@b@@b@ public static String nullSafeToString(char[] array)@b@ {@b@ if (array == null)@b@ return "null";@b@@b@ int length = array.length;@b@ if (length == 0)@b@ return "{}";@b@@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; ++i) {@b@ if (i == 0) {@b@ sb.append("{");@b@ }@b@ else@b@ sb.append(", ");@b@@b@ sb.append("'").append(array[i]).append("'");@b@ }@b@ sb.append("}");@b@ return sb.toString();@b@ }@b@@b@ public static String nullSafeToString(double[] array)@b@ {@b@ if (array == null)@b@ return "null";@b@@b@ int length = array.length;@b@ if (length == 0)@b@ return "{}";@b@@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; ++i) {@b@ if (i == 0) {@b@ sb.append("{");@b@ }@b@ else {@b@ sb.append(", ");@b@ }@b@@b@ sb.append(array[i]);@b@ }@b@ sb.append("}");@b@ return sb.toString();@b@ }@b@@b@ public static String nullSafeToString(float[] array)@b@ {@b@ if (array == null)@b@ return "null";@b@@b@ int length = array.length;@b@ if (length == 0)@b@ return "{}";@b@@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; ++i) {@b@ if (i == 0) {@b@ sb.append("{");@b@ }@b@ else {@b@ sb.append(", ");@b@ }@b@@b@ sb.append(array[i]);@b@ }@b@ sb.append("}");@b@ return sb.toString();@b@ }@b@@b@ public static String nullSafeToString(int[] array)@b@ {@b@ if (array == null)@b@ return "null";@b@@b@ int length = array.length;@b@ if (length == 0)@b@ return "{}";@b@@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; ++i) {@b@ if (i == 0) {@b@ sb.append("{");@b@ }@b@ else@b@ sb.append(", ");@b@@b@ sb.append(array[i]);@b@ }@b@ sb.append("}");@b@ return sb.toString();@b@ }@b@@b@ public static String nullSafeToString(long[] array)@b@ {@b@ if (array == null)@b@ return "null";@b@@b@ int length = array.length;@b@ if (length == 0)@b@ return "{}";@b@@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; ++i) {@b@ if (i == 0) {@b@ sb.append("{");@b@ }@b@ else@b@ sb.append(", ");@b@@b@ sb.append(array[i]);@b@ }@b@ sb.append("}");@b@ return sb.toString();@b@ }@b@@b@ public static String nullSafeToString(short[] array)@b@ {@b@ if (array == null)@b@ return "null";@b@@b@ int length = array.length;@b@ if (length == 0)@b@ return "{}";@b@@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < length; ++i) {@b@ if (i == 0) {@b@ sb.append("{");@b@ }@b@ else@b@ sb.append(", ");@b@@b@ sb.append(array[i]);@b@ }@b@ sb.append("}");@b@ return sb.toString();@b@ }@b@}