001: package org.drools.util;
002:
003: import java.lang.reflect.Array;
004:
005: /*
006: * Licensed to the Apache Software Foundation (ASF) under one or more
007: * contributor license agreements. See the NOTICE file distributed with
008: * this work for additional information regarding copyright ownership.
009: * The ASF licenses this file to You under the Apache License, Version 2.0
010: * (the "License"); you may not use this file except in compliance with
011: * the License. You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: /**
023: * Taken from commons lang
024: *
025: * <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and
026: * primitive wrapper arrays (like <code>Integer[]</code>).</p>
027: *
028: * <p>This class tries to handle <code>null</code> input gracefully.
029: * An exception will not be thrown for a <code>null</code>
030: * array input. However, an Object array that contains a <code>null</code>
031: * element may throw an exception. Each method documents its behaviour.</p>
032: *
033: * @author Stephen Colebourne
034: * @author Moritz Petersen
035: * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
036: * @author Nikolay Metchev
037: * @author Matthew Hawthorne
038: * @author Tim O'Brien
039: * @author Pete Gieser
040: * @author Gary Gregory
041: * @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
042: * @author Maarten Coene
043: * @since 2.0
044: * @version $Id$
045: */
046: public class ArrayUtils {
047: // Taken from commons ArrayUtils
048:
049: public static final int INDEX_NOT_FOUND = -1;
050:
051: /**
052: * <p>Checks if the object is in the given array.</p>
053: *
054: * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
055: *
056: * @param array the array to search through
057: * @param objectToFind the object to find
058: * @return <code>true</code> if the array contains the object
059: */
060: public static boolean contains(Object[] array, Object objectToFind) {
061: return indexOf(array, objectToFind) != INDEX_NOT_FOUND;
062: }
063:
064: // IndexOf search
065: // ----------------------------------------------------------------------
066:
067: // Object IndexOf
068: //-----------------------------------------------------------------------
069: /**
070: * <p>Finds the index of the given object in the array.</p>
071: *
072: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
073: *
074: * @param array the array to search through for the object, may be <code>null</code>
075: * @param objectToFind the object to find, may be <code>null</code>
076: * @return the index of the object within the array,
077: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
078: */
079: public static int indexOf(Object[] array, Object objectToFind) {
080: return indexOf(array, objectToFind, 0);
081: }
082:
083: /**
084: * <p>Finds the index of the given object in the array starting at the given index.</p>
085: *
086: * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
087: *
088: * <p>A negative startIndex is treated as zero. A startIndex larger than the array
089: * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
090: *
091: * @param array the array to search through for the object, may be <code>null</code>
092: * @param objectToFind the object to find, may be <code>null</code>
093: * @param startIndex the index to start searching at
094: * @return the index of the object within the array starting at the index,
095: * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
096: */
097: public static int indexOf(Object[] array, Object objectToFind,
098: int startIndex) {
099: if (array == null) {
100: return INDEX_NOT_FOUND;
101: }
102: if (startIndex < 0) {
103: startIndex = 0;
104: }
105: if (objectToFind == null) {
106: for (int i = startIndex; i < array.length; i++) {
107: if (array[i] == null) {
108: return i;
109: }
110: }
111: } else {
112: for (int i = startIndex; i < array.length; i++) {
113: if (objectToFind.equals(array[i])) {
114: return i;
115: }
116: }
117: }
118: return INDEX_NOT_FOUND;
119: }
120:
121: public static int hashCode(Object[] array) {
122: final int PRIME = 31;
123: if (array == null)
124: return 0;
125: int result = 1;
126: for (int index = 0; index < array.length; index++) {
127: result = PRIME
128: * result
129: + (array[index] == null ? 0 : array[index]
130: .hashCode());
131: }
132: return result;
133: }
134:
135: public static Object[] copyOf(Object[] original, int newLength,
136: Class newType) {
137: Object[] arr = (newType == Object[].class) ? new Object[newLength]
138: : (Object[]) Array.newInstance(newType
139: .getComponentType(), newLength);
140: int len = (original.length < newLength ? original.length
141: : newLength);
142: System.arraycopy(original, 0, arr, 0, len);
143: return arr;
144: }
145:
146: /**
147: * @since 1.5
148: */
149: public static boolean deepEquals(Object[] a1, Object[] a2) {
150: if (a1 == a2)
151: return true;
152: if (a1 == null || a2 == null)
153: return false;
154: int len = a1.length;
155: if (len != a2.length)
156: return false;
157: for (int i = 0; i < len; i++) {
158: Object e1 = a1[i];
159: Object e2 = a2[i];
160: if (e1 == e2)
161: continue;
162: if (e1 == null)
163: return false;
164: boolean eq = (e1.getClass() != e2.getClass()
165: || !e1.getClass().isArray() || !e2.getClass()
166: .isArray()) ? e1.equals(e2)
167: : (e1 instanceof Object[] && e2 instanceof Object[]) ? deepEquals(
168: (Object[]) e1, (Object[]) e2)
169: : (e1 instanceof byte[] && e2 instanceof byte[]) ? equals(
170: (byte[]) e1, (byte[]) e2)
171: : (e1 instanceof short[] && e2 instanceof short[]) ? equals(
172: (short[]) e1, (short[]) e2)
173: : (e1 instanceof int[] && e2 instanceof int[]) ? equals(
174: (int[]) e1,
175: (int[]) e2)
176: : (e1 instanceof long[] && e2 instanceof long[]) ? equals(
177: (long[]) e1,
178: (long[]) e2)
179: : (e1 instanceof char[] && e2 instanceof char[]) ? equals(
180: (char[]) e1,
181: (char[]) e2)
182: : (e1 instanceof boolean[] && e2 instanceof boolean[]) ? equals(
183: (boolean[]) e1,
184: (boolean[]) e2)
185: : (e1 instanceof float[] && e2 instanceof float[]) ? equals(
186: (float[]) e1,
187: (float[]) e2)
188: : (e1 instanceof double[] && e2 instanceof double[]) ? equals(
189: (double[]) e1,
190: (double[]) e2)
191: : e1
192: .equals(e2);
193:
194: if (!eq)
195: return false;
196: }
197: return true;
198: }
199:
200: // Equality Testing
201:
202: public static boolean equals(long[] a, long[] a2) {
203: return java.util.Arrays.equals(a, a2);
204: }
205:
206: public static boolean equals(int[] a, int[] a2) {
207: return java.util.Arrays.equals(a, a2);
208: }
209:
210: public static boolean equals(short[] a, short a2[]) {
211: return java.util.Arrays.equals(a, a2);
212: }
213:
214: public static boolean equals(char[] a, char[] a2) {
215: return java.util.Arrays.equals(a, a2);
216: }
217:
218: public static boolean equals(byte[] a, byte[] a2) {
219: return java.util.Arrays.equals(a, a2);
220: }
221:
222: public static boolean equals(boolean[] a, boolean[] a2) {
223: return java.util.Arrays.equals(a, a2);
224: }
225:
226: public static boolean equals(double[] a, double[] a2) {
227: return java.util.Arrays.equals(a, a2);
228: }
229:
230: public static boolean equals(float[] a, float[] a2) {
231: return java.util.Arrays.equals(a, a2);
232: }
233:
234: public static boolean equals(Object[] a, Object[] a2) {
235: return java.util.Arrays.equals(a, a2);
236: }
237:
238: }
|