一、前言
基于springframework框架org.springframework.util.ObjectUtils对象处理工具类,可以对象数组间判断isArray、空判断isEmpty、添加对象至数组addObjectToArray、对象数组转换toObjectArray、数据类型是否相等nullSafeEquals等等。
二、源码说明
package org.springframework.util;@b@@b@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@ Class[] arrayOfClass;@b@ int j;@b@ if (!(isCheckedException(ex)))@b@ return true;@b@@b@ if (declaredExceptions != null) {@b@ arrayOfClass = declaredExceptions; int i = arrayOfClass.length; for (j = 0; j < i; ++j) { Class declaredException = arrayOfClass[j];@b@ if (declaredException.isInstance(ex))@b@ return true;@b@ }@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@ Object[] arrayOfObject = array; int i = arrayOfObject.length; for (int j = 0; j < i; ++j) { Object arrayEle = arrayOfObject[j];@b@ if (nullSafeEquals(arrayEle, element))@b@ return true;@b@ }@b@@b@ return false;@b@ }@b@@b@ public static boolean containsConstant(Enum<?>[] enumValues, String constant)@b@ {@b@ return containsConstant(enumValues, constant, false);@b@ }@b@@b@ public static boolean containsConstant(Enum<?>[] enumValues, String constant, boolean caseSensitive)@b@ {@b@ Enum[] arrayOfEnum = enumValues; int i = arrayOfEnum.length; for (int j = 0; j < i; ++j) { Enum candidate = arrayOfEnum[j];@b@ if (caseSensitive)@b@ if (!(candidate@b@ .toString().equals(constant))) break label55;@b@ else if (!(candidate@b@ .toString().equalsIgnoreCase(constant))) break label55;@b@ return true;@b@ }@b@@b@ label55: return false;@b@ }@b@@b@ public static <E extends Enum<?>> E caseInsensitiveValueOf(E[] enumValues, String constant)@b@ {@b@ Enum[] arrayOfEnum = enumValues; int i = arrayOfEnum.length; for (int j = 0; j < i; ++j) { Enum candidate = arrayOfEnum[j];@b@ if (candidate.toString().equalsIgnoreCase(constant))@b@ return candidate;@b@@b@ }@b@@b@ throw new IllegalArgumentException(String.format("constant [%s] does not exist in enum type %s", new Object[] { constant, enumValues@b@ .getClass().getComponentType().getName() }));@b@ }@b@@b@ public static <A, O extends A> A[] addObjectToArray(A[] array, O 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@@b@ Object[] newArr = (Object[])(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[])(Object[])source);@b@@b@ if (source == null)@b@ return new Object[0];@b@@b@ if (!(source.getClass().isArray()))@b@ throw new IllegalArgumentException(new StringBuilder().append("Source is not an array: ").append(source).toString());@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[])(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[])(Object[])o1, (Object[])(Object[])o2);@b@@b@ if ((o1 instanceof boolean[]) && (o2 instanceof boolean[]))@b@ return Arrays.equals((boolean[])(boolean[])o1, (boolean[])(boolean[])o2);@b@@b@ if ((o1 instanceof byte[]) && (o2 instanceof byte[]))@b@ return Arrays.equals((byte[])(byte[])o1, (byte[])(byte[])o2);@b@@b@ if ((o1 instanceof char[]) && (o2 instanceof char[]))@b@ return Arrays.equals((char[])(char[])o1, (char[])(char[])o2);@b@@b@ if ((o1 instanceof double[]) && (o2 instanceof double[]))@b@ return Arrays.equals((double[])(double[])o1, (double[])(double[])o2);@b@@b@ if ((o1 instanceof float[]) && (o2 instanceof float[]))@b@ return Arrays.equals((float[])(float[])o1, (float[])(float[])o2);@b@@b@ if ((o1 instanceof int[]) && (o2 instanceof int[]))@b@ return Arrays.equals((int[])(int[])o1, (int[])(int[])o2);@b@@b@ if ((o1 instanceof long[]) && (o2 instanceof long[]))@b@ return Arrays.equals((long[])(long[])o1, (long[])(long[])o2);@b@@b@ if ((o1 instanceof short[]) && (o2 instanceof short[]))@b@ return Arrays.equals((short[])(short[])o1, (short[])(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[])(Object[])obj);@b@@b@ if (obj instanceof boolean[])@b@ return nullSafeHashCode((boolean[])(boolean[])obj);@b@@b@ if (obj instanceof byte[])@b@ return nullSafeHashCode((byte[])(byte[])obj);@b@@b@ if (obj instanceof char[])@b@ return nullSafeHashCode((char[])(char[])obj);@b@@b@ if (obj instanceof double[])@b@ return nullSafeHashCode((double[])(double[])obj);@b@@b@ if (obj instanceof float[])@b@ return nullSafeHashCode((float[])(float[])obj);@b@@b@ if (obj instanceof int[])@b@ return nullSafeHashCode((int[])(int[])obj);@b@@b@ if (obj instanceof long[])@b@ return nullSafeHashCode((long[])(long[])obj);@b@@b@ if (obj instanceof short[])@b@ return nullSafeHashCode((short[])(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@ Object[] arrayOfObject = array; int i = arrayOfObject.length; for (int j = 0; j < i; ++j) { Object element = arrayOfObject[j];@b@ hash = 31 * hash + nullSafeHashCode(element);@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@ boolean[] arrayOfBoolean = array; int i = arrayOfBoolean.length; for (int j = 0; j < i; ++j) { boolean element = arrayOfBoolean[j];@b@ hash = 31 * hash + hashCode(element);@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@ byte[] arrayOfByte = array; int i = arrayOfByte.length; for (int j = 0; j < i; ++j) { byte element = arrayOfByte[j];@b@ hash = 31 * hash + element;@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@ char[] arrayOfChar = array; int i = arrayOfChar.length; for (int j = 0; j < i; ++j) { char element = arrayOfChar[j];@b@ hash = 31 * hash + element;@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@ double[] arrayOfDouble = array; int i = arrayOfDouble.length; for (int j = 0; j < i; ++j) { double element = arrayOfDouble[j];@b@ hash = 31 * hash + hashCode(element);@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@ float[] arrayOfFloat = array; int i = arrayOfFloat.length; for (int j = 0; j < i; ++j) { float element = arrayOfFloat[j];@b@ hash = 31 * hash + hashCode(element);@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[] arrayOfInt = array; int i = arrayOfInt.length; for (int j = 0; j < i; ++j) { int element = arrayOfInt[j];@b@ hash = 31 * hash + element;@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@ long[] arrayOfLong = array; int i = arrayOfLong.length; for (int j = 0; j < i; ++j) { long element = arrayOfLong[j];@b@ hash = 31 * hash + hashCode(element);@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@ short[] arrayOfShort = array; int i = arrayOfShort.length; for (int j = 0; j < i; ++j) { short element = arrayOfShort[j];@b@ hash = 31 * hash + element;@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@ return hashCode(Double.doubleToLongBits(dbl));@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 new StringBuilder().append(obj.getClass().getName()).append("@").append(getIdentityHexString(obj)).toString();@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[])(Object[])obj);@b@@b@ if (obj instanceof boolean[])@b@ return nullSafeToString((boolean[])(boolean[])obj);@b@@b@ if (obj instanceof byte[])@b@ return nullSafeToString((byte[])(byte[])obj);@b@@b@ if (obj instanceof char[])@b@ return nullSafeToString((char[])(char[])obj);@b@@b@ if (obj instanceof double[])@b@ return nullSafeToString((double[])(double[])obj);@b@@b@ if (obj instanceof float[])@b@ return nullSafeToString((float[])(float[])obj);@b@@b@ if (obj instanceof int[])@b@ return nullSafeToString((int[])(int[])obj);@b@@b@ if (obj instanceof long[])@b@ return nullSafeToString((long[])(long[])obj);@b@@b@ if (obj instanceof short[])@b@ return nullSafeToString((short[])(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@}