一、前言
关于drools-core源码包中的org.drools.util.ArrayUtils数组工具类,数组内对象包含关系判断contains、数组间的复制copyOf、两对象间深度内存对比deepEquals等。
二、源码说明
package org.drools.util;@b@@b@import [Ljava.lang.Object;;@b@import java.lang.reflect.Array;@b@import java.util.Arrays;@b@@b@public class ArrayUtils@b@{@b@ public static final int INDEX_NOT_FOUND = -1;@b@@b@ public static boolean contains(Object[] array, Object objectToFind)@b@ {@b@ return (indexOf(array, objectToFind) != -1);@b@ }@b@@b@ public static int indexOf(Object[] array, Object objectToFind)@b@ {@b@ return indexOf(array, objectToFind, 0);@b@ }@b@@b@ public static int indexOf(Object[] array, Object objectToFind, int startIndex)@b@ {@b@ int i;@b@ if (array == null)@b@ return -1;@b@@b@ if (startIndex < 0)@b@ startIndex = 0;@b@@b@ if (objectToFind == null)@b@ for (i = startIndex; i < array.length; ++i)@b@ if (array[i] == null)@b@ return i;@b@@b@@b@ else@b@ for (i = startIndex; i < array.length; ++i)@b@ if (objectToFind.equals(array[i]))@b@ return i;@b@@b@@b@@b@ return -1;@b@ }@b@@b@ public static int hashCode(Object[] array) {@b@ int PRIME = 31;@b@ if (array == null) return 0;@b@ int result = 1;@b@ for (int index = 0; index < array.length; ++index)@b@ result = 31 * result + ((array[index] == null) ? 0 : array[index].hashCode());@b@@b@ return result;@b@ }@b@@b@ public static Object[] copyOf(Object[] original, int newLength, Class newType)@b@ {@b@ Object[] arr = (newType == [Ljava.lang.Object.class) ? new Object[newLength] : (Object[])(Object[])Array.newInstance(newType.getComponentType(), newLength);@b@@b@ int len = (original.length < newLength) ? original.length : newLength;@b@ System.arraycopy(original, 0, arr, 0, len);@b@@b@ return arr;@b@ }@b@@b@ public static boolean deepEquals(Object[] a1, Object[] a2)@b@ {@b@ if (a1 == a2) return true;@b@ if ((a1 == null) || (a2 == null)) return false;@b@ int len = a1.length;@b@ if (len != a2.length) return false;@b@ for (int i = 0; i < len; ++i) {@b@ Object e1 = a1[i];@b@ Object e2 = a2[i];@b@ if (e1 == e2) break label465:@b@ if (e1 == null) return false;@b@ boolean eq = ((e1 instanceof double[]) && (e2 instanceof double[])) ? equals((double[])(double[])e1, (double[])(double[])e2) : ((e1 instanceof float[]) && (e2 instanceof float[])) ? equals((float[])(float[])e1, (float[])(float[])e2) : ((e1 instanceof boolean[]) && (e2 instanceof boolean[])) ? equals((boolean[])(boolean[])e1, (boolean[])(boolean[])e2) : ((e1 instanceof char[]) && (e2 instanceof char[])) ? equals((char[])(char[])e1, (char[])(char[])e2) : ((e1 instanceof long[]) && (e2 instanceof long[])) ? equals((long[])(long[])e1, (long[])(long[])e2) : ((e1 instanceof int[]) && (e2 instanceof int[])) ? equals((int[])(int[])e1, (int[])(int[])e2) : ((e1 instanceof short[]) && (e2 instanceof short[])) ? equals((short[])(short[])e1, (short[])(short[])e2) : ((e1 instanceof byte[]) && (e2 instanceof byte[])) ? equals((byte[])(byte[])e1, (byte[])(byte[])e2) : ((e1 instanceof Object[]) && (e2 instanceof Object[])) ? deepEquals((Object[])(Object[])e1, (Object[])(Object[])e2) : ((e1.getClass() != e2.getClass()) || (!(e1.getClass().isArray())) || (!(e2.getClass().isArray()))) ? e1.equals(e2) : e1.equals(e2);@b@@b@ if (!(eq)) return false;@b@ }@b@ label465: return true;@b@ }@b@@b@ public static boolean equals(long[] a, long[] a2)@b@ {@b@ return Arrays.equals(a, a2);@b@ }@b@@b@ public static boolean equals(int[] a, int[] a2)@b@ {@b@ return Arrays.equals(a, a2);@b@ }@b@@b@ public static boolean equals(short[] a, short[] a2)@b@ {@b@ return Arrays.equals(a, a2);@b@ }@b@@b@ public static boolean equals(char[] a, char[] a2)@b@ {@b@ return Arrays.equals(a, a2);@b@ }@b@@b@ public static boolean equals(byte[] a, byte[] a2)@b@ {@b@ return Arrays.equals(a, a2);@b@ }@b@@b@ public static boolean equals(boolean[] a, boolean[] a2)@b@ {@b@ return Arrays.equals(a, a2);@b@ }@b@@b@ public static boolean equals(double[] a, double[] a2)@b@ {@b@ return Arrays.equals(a, a2);@b@ }@b@@b@ public static boolean equals(float[] a, float[] a2)@b@ {@b@ return Arrays.equals(a, a2);@b@ }@b@@b@ public static boolean equals(Object[] a, Object[] a2)@b@ {@b@ return Arrays.equals(a, a2);@b@ }@b@}