/machinelearning/5.0/drools-core/src/main/java/org/drools/util/ArrayUtils.java

https://github.com/etirelli/droolsjbpm-contributed-experiments · Java · 237 lines · 132 code · 23 blank · 82 comment · 43 complexity · 776a31b9b2d77035202ea5d882ebbbc8 MD5 · raw file

  1. package org.drools.util;
  2. import java.lang.reflect.Array;
  3. /*
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership.
  7. * The ASF licenses this file to You under the Apache License, Version 2.0
  8. * (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /**
  20. * Taken from commons lang
  21. *
  22. * <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and
  23. * primitive wrapper arrays (like <code>Integer[]</code>).</p>
  24. *
  25. * <p>This class tries to handle <code>null</code> input gracefully.
  26. * An exception will not be thrown for a <code>null</code>
  27. * array input. However, an Object array that contains a <code>null</code>
  28. * element may throw an exception. Each method documents its behaviour.</p>
  29. *
  30. * @author Stephen Colebourne
  31. * @author Moritz Petersen
  32. * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
  33. * @author Nikolay Metchev
  34. * @author Matthew Hawthorne
  35. * @author Tim O'Brien
  36. * @author Pete Gieser
  37. * @author Gary Gregory
  38. * @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
  39. * @author Maarten Coene
  40. * @since 2.0
  41. * @version $Id$
  42. */
  43. public class ArrayUtils {
  44. // Taken from commons ArrayUtils
  45. public static final int INDEX_NOT_FOUND = -1;
  46. /**
  47. * <p>Checks if the object is in the given array.</p>
  48. *
  49. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  50. *
  51. * @param array the array to search through
  52. * @param objectToFind the object to find
  53. * @return <code>true</code> if the array contains the object
  54. */
  55. public static boolean contains(Object[] array,
  56. Object objectToFind) {
  57. return indexOf( array,
  58. objectToFind ) != INDEX_NOT_FOUND;
  59. }
  60. // IndexOf search
  61. // ----------------------------------------------------------------------
  62. // Object IndexOf
  63. //-----------------------------------------------------------------------
  64. /**
  65. * <p>Finds the index of the given object in the array.</p>
  66. *
  67. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  68. *
  69. * @param array the array to search through for the object, may be <code>null</code>
  70. * @param objectToFind the object to find, may be <code>null</code>
  71. * @return the index of the object within the array,
  72. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  73. */
  74. public static int indexOf(Object[] array,
  75. Object objectToFind) {
  76. return indexOf( array,
  77. objectToFind,
  78. 0 );
  79. }
  80. /**
  81. * <p>Finds the index of the given object in the array starting at the given index.</p>
  82. *
  83. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  84. *
  85. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  86. * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
  87. *
  88. * @param array the array to search through for the object, may be <code>null</code>
  89. * @param objectToFind the object to find, may be <code>null</code>
  90. * @param startIndex the index to start searching at
  91. * @return the index of the object within the array starting at the index,
  92. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  93. */
  94. public static int indexOf(Object[] array,
  95. Object objectToFind,
  96. int startIndex) {
  97. if ( array == null ) {
  98. return INDEX_NOT_FOUND;
  99. }
  100. if ( startIndex < 0 ) {
  101. startIndex = 0;
  102. }
  103. if ( objectToFind == null ) {
  104. for ( int i = startIndex; i < array.length; i++ ) {
  105. if ( array[i] == null ) {
  106. return i;
  107. }
  108. }
  109. } else {
  110. for ( int i = startIndex; i < array.length; i++ ) {
  111. if ( objectToFind.equals( array[i] ) ) {
  112. return i;
  113. }
  114. }
  115. }
  116. return INDEX_NOT_FOUND;
  117. }
  118. public static int hashCode(Object[] array) {
  119. final int PRIME = 31;
  120. if ( array == null ) return 0;
  121. int result = 1;
  122. for ( int index = 0; index < array.length; index++ ) {
  123. result = PRIME * result + (array[index] == null ? 0 : array[index].hashCode());
  124. }
  125. return result;
  126. }
  127. public static Object[] copyOf(Object[] original,
  128. int newLength,
  129. Class newType) {
  130. Object[] arr = (newType == Object[].class) ? new Object[newLength] : (Object[]) Array.newInstance( newType.getComponentType(),
  131. newLength );
  132. int len = (original.length < newLength ? original.length : newLength);
  133. System.arraycopy( original,
  134. 0,
  135. arr,
  136. 0,
  137. len );
  138. return arr;
  139. }
  140. /**
  141. * @since 1.5
  142. */
  143. public static boolean deepEquals(Object[] a1,
  144. Object[] a2) {
  145. if ( a1 == a2 ) return true;
  146. if ( a1 == null || a2 == null ) return false;
  147. int len = a1.length;
  148. if ( len != a2.length ) return false;
  149. for ( int i = 0; i < len; i++ ) {
  150. Object e1 = a1[i];
  151. Object e2 = a2[i];
  152. if ( e1 == e2 ) continue;
  153. if ( e1 == null ) return false;
  154. boolean eq = (e1.getClass() != e2.getClass() || !e1.getClass().isArray() || !e2.getClass().isArray() ) ? e1.equals( e2 ) : (e1 instanceof Object[] && e2 instanceof Object[]) ? deepEquals( (Object[]) e1,
  155. (Object[]) e2 ) : (e1 instanceof byte[] && e2 instanceof byte[]) ? equals( (byte[]) e1,
  156. (byte[]) e2 ) : (e1 instanceof short[] && e2 instanceof short[]) ? equals( (short[]) e1,
  157. (short[]) e2 ) : (e1 instanceof int[] && e2 instanceof int[]) ? equals( (int[]) e1,
  158. (int[]) e2 ) : (e1 instanceof long[] && e2 instanceof long[]) ? equals( (long[]) e1,
  159. (long[]) e2 ) : (e1 instanceof char[] && e2 instanceof char[]) ? equals( (char[]) e1,
  160. (char[]) e2 ) : (e1 instanceof boolean[] && e2 instanceof boolean[]) ? equals( (boolean[]) e1,
  161. (boolean[]) e2 ) : (e1 instanceof float[] && e2 instanceof float[]) ? equals( (float[]) e1,
  162. (float[]) e2 ) : (e1 instanceof double[] && e2 instanceof double[]) ? equals( (double[]) e1,
  163. (double[]) e2 ) : e1.equals( e2 );
  164. if ( !eq ) return false;
  165. }
  166. return true;
  167. }
  168. // Equality Testing
  169. public static boolean equals(long[] a,
  170. long[] a2) {
  171. return java.util.Arrays.equals( a,
  172. a2 );
  173. }
  174. public static boolean equals(int[] a,
  175. int[] a2) {
  176. return java.util.Arrays.equals( a,
  177. a2 );
  178. }
  179. public static boolean equals(short[] a,
  180. short a2[]) {
  181. return java.util.Arrays.equals( a,
  182. a2 );
  183. }
  184. public static boolean equals(char[] a,
  185. char[] a2) {
  186. return java.util.Arrays.equals( a,
  187. a2 );
  188. }
  189. public static boolean equals(byte[] a,
  190. byte[] a2) {
  191. return java.util.Arrays.equals( a,
  192. a2 );
  193. }
  194. public static boolean equals(boolean[] a,
  195. boolean[] a2) {
  196. return java.util.Arrays.equals( a,
  197. a2 );
  198. }
  199. public static boolean equals(double[] a,
  200. double[] a2) {
  201. return java.util.Arrays.equals( a,
  202. a2 );
  203. }
  204. public static boolean equals(float[] a,
  205. float[] a2) {
  206. return java.util.Arrays.equals( a,
  207. a2 );
  208. }
  209. public static boolean equals(Object[] a,
  210. Object[] a2) {
  211. return java.util.Arrays.equals( a,
  212. a2 );
  213. }
  214. }