PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/org.apache.commons.lang/source-bundle/org/apache/commons/lang/ArrayUtils.java

https://github.com/tomsontom/emf-databinding-example
Java | 4416 lines | 1583 code | 193 blank | 2640 comment | 655 complexity | 15e37024df5cb4109f45a0ea93273c82 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.commons.lang;
  18. import java.lang.reflect.Array;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import org.apache.commons.lang.builder.EqualsBuilder;
  22. import org.apache.commons.lang.builder.HashCodeBuilder;
  23. import org.apache.commons.lang.builder.ToStringBuilder;
  24. import org.apache.commons.lang.builder.ToStringStyle;
  25. /**
  26. * <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and
  27. * primitive wrapper arrays (like <code>Integer[]</code>).</p>
  28. *
  29. * <p>This class tries to handle <code>null</code> input gracefully.
  30. * An exception will not be thrown for a <code>null</code>
  31. * array input. However, an Object array that contains a <code>null</code>
  32. * element may throw an exception. Each method documents its behaviour.</p>
  33. *
  34. * @author Stephen Colebourne
  35. * @author Moritz Petersen
  36. * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
  37. * @author Nikolay Metchev
  38. * @author Matthew Hawthorne
  39. * @author Tim O'Brien
  40. * @author Pete Gieser
  41. * @author Gary Gregory
  42. * @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
  43. * @author Maarten Coene
  44. * @since 2.0
  45. * @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $
  46. */
  47. public class ArrayUtils {
  48. /**
  49. * An empty immutable <code>Object</code> array.
  50. */
  51. public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
  52. /**
  53. * An empty immutable <code>Class</code> array.
  54. */
  55. public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
  56. /**
  57. * An empty immutable <code>String</code> array.
  58. */
  59. public static final String[] EMPTY_STRING_ARRAY = new String[0];
  60. /**
  61. * An empty immutable <code>long</code> array.
  62. */
  63. public static final long[] EMPTY_LONG_ARRAY = new long[0];
  64. /**
  65. * An empty immutable <code>Long</code> array.
  66. */
  67. public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0];
  68. /**
  69. * An empty immutable <code>int</code> array.
  70. */
  71. public static final int[] EMPTY_INT_ARRAY = new int[0];
  72. /**
  73. * An empty immutable <code>Integer</code> array.
  74. */
  75. public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];
  76. /**
  77. * An empty immutable <code>short</code> array.
  78. */
  79. public static final short[] EMPTY_SHORT_ARRAY = new short[0];
  80. /**
  81. * An empty immutable <code>Short</code> array.
  82. */
  83. public static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0];
  84. /**
  85. * An empty immutable <code>byte</code> array.
  86. */
  87. public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
  88. /**
  89. * An empty immutable <code>Byte</code> array.
  90. */
  91. public static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0];
  92. /**
  93. * An empty immutable <code>double</code> array.
  94. */
  95. public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
  96. /**
  97. * An empty immutable <code>Double</code> array.
  98. */
  99. public static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0];
  100. /**
  101. * An empty immutable <code>float</code> array.
  102. */
  103. public static final float[] EMPTY_FLOAT_ARRAY = new float[0];
  104. /**
  105. * An empty immutable <code>Float</code> array.
  106. */
  107. public static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0];
  108. /**
  109. * An empty immutable <code>boolean</code> array.
  110. */
  111. public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
  112. /**
  113. * An empty immutable <code>Boolean</code> array.
  114. */
  115. public static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0];
  116. /**
  117. * An empty immutable <code>char</code> array.
  118. */
  119. public static final char[] EMPTY_CHAR_ARRAY = new char[0];
  120. /**
  121. * An empty immutable <code>Character</code> array.
  122. */
  123. public static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0];
  124. /**
  125. * The index value when an element is not found in a list or array: <code>-1</code>.
  126. * This value is returned by methods in this class and can also be used in comparisons with values returned by
  127. * various method from {@link java.util.List}.
  128. */
  129. public static final int INDEX_NOT_FOUND = -1;
  130. /**
  131. * <p>ArrayUtils instances should NOT be constructed in standard programming.
  132. * Instead, the class should be used as <code>ArrayUtils.clone(new int[] {2})</code>.</p>
  133. *
  134. * <p>This constructor is public to permit tools that require a JavaBean instance
  135. * to operate.</p>
  136. */
  137. public ArrayUtils() {
  138. super();
  139. }
  140. // Basic methods handling multi-dimensional arrays
  141. //-----------------------------------------------------------------------
  142. /**
  143. * <p>Outputs an array as a String, treating <code>null</code> as an empty array.</p>
  144. *
  145. * <p>Multi-dimensional arrays are handled correctly, including
  146. * multi-dimensional primitive arrays.</p>
  147. *
  148. * <p>The format is that of Java source code, for example <code>{a,b}</code>.</p>
  149. *
  150. * @param array the array to get a toString for, may be <code>null</code>
  151. * @return a String representation of the array, '{}' if null array input
  152. */
  153. public static String toString(Object array) {
  154. return toString(array, "{}");
  155. }
  156. /**
  157. * <p>Outputs an array as a String handling <code>null</code>s.</p>
  158. *
  159. * <p>Multi-dimensional arrays are handled correctly, including
  160. * multi-dimensional primitive arrays.</p>
  161. *
  162. * <p>The format is that of Java source code, for example <code>{a,b}</code>.</p>
  163. *
  164. * @param array the array to get a toString for, may be <code>null</code>
  165. * @param stringIfNull the String to return if the array is <code>null</code>
  166. * @return a String representation of the array
  167. */
  168. public static String toString(Object array, String stringIfNull) {
  169. if (array == null) {
  170. return stringIfNull;
  171. }
  172. return new ToStringBuilder(array, ToStringStyle.SIMPLE_STYLE).append(array).toString();
  173. }
  174. /**
  175. * <p>Get a hashCode for an array handling multi-dimensional arrays correctly.</p>
  176. *
  177. * <p>Multi-dimensional primitive arrays are also handled correctly by this method.</p>
  178. *
  179. * @param array the array to get a hashCode for, may be <code>null</code>
  180. * @return a hashCode for the array, zero if null array input
  181. */
  182. public static int hashCode(Object array) {
  183. return new HashCodeBuilder().append(array).toHashCode();
  184. }
  185. /**
  186. * <p>Compares two arrays, using equals(), handling multi-dimensional arrays
  187. * correctly.</p>
  188. *
  189. * <p>Multi-dimensional primitive arrays are also handled correctly by this method.</p>
  190. *
  191. * @param array1 the left hand array to compare, may be <code>null</code>
  192. * @param array2 the right hand array to compare, may be <code>null</code>
  193. * @return <code>true</code> if the arrays are equal
  194. */
  195. public static boolean isEquals(Object array1, Object array2) {
  196. return new EqualsBuilder().append(array1, array2).isEquals();
  197. }
  198. // To map
  199. //-----------------------------------------------------------------------
  200. /**
  201. * <p>Converts the given array into a {@link java.util.Map}. Each element of the array
  202. * must be either a {@link java.util.Map.Entry} or an Array, containing at least two
  203. * elements, where the first element is used as key and the second as
  204. * value.</p>
  205. *
  206. * <p>This method can be used to initialize:</p>
  207. * <pre>
  208. * // Create a Map mapping colors.
  209. * Map colorMap = MapUtils.toMap(new String[][] {{
  210. * {"RED", "#FF0000"},
  211. * {"GREEN", "#00FF00"},
  212. * {"BLUE", "#0000FF"}});
  213. * </pre>
  214. *
  215. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  216. *
  217. * @param array an array whose elements are either a {@link java.util.Map.Entry} or
  218. * an Array containing at least two elements, may be <code>null</code>
  219. * @return a <code>Map</code> that was created from the array
  220. * @throws IllegalArgumentException if one element of this Array is
  221. * itself an Array containing less then two elements
  222. * @throws IllegalArgumentException if the array contains elements other
  223. * than {@link java.util.Map.Entry} and an Array
  224. */
  225. public static Map toMap(Object[] array) {
  226. if (array == null) {
  227. return null;
  228. }
  229. final Map map = new HashMap((int) (array.length * 1.5));
  230. for (int i = 0; i < array.length; i++) {
  231. Object object = array[i];
  232. if (object instanceof Map.Entry) {
  233. Map.Entry entry = (Map.Entry) object;
  234. map.put(entry.getKey(), entry.getValue());
  235. } else if (object instanceof Object[]) {
  236. Object[] entry = (Object[]) object;
  237. if (entry.length < 2) {
  238. throw new IllegalArgumentException("Array element " + i + ", '"
  239. + object
  240. + "', has a length less than 2");
  241. }
  242. map.put(entry[0], entry[1]);
  243. } else {
  244. throw new IllegalArgumentException("Array element " + i + ", '"
  245. + object
  246. + "', is neither of type Map.Entry nor an Array");
  247. }
  248. }
  249. return map;
  250. }
  251. // Clone
  252. //-----------------------------------------------------------------------
  253. /**
  254. * <p>Shallow clones an array returning a typecast result and handling
  255. * <code>null</code>.</p>
  256. *
  257. * <p>The objects in the array are not cloned, thus there is no special
  258. * handling for multi-dimensional arrays.</p>
  259. *
  260. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  261. *
  262. * @param array the array to shallow clone, may be <code>null</code>
  263. * @return the cloned array, <code>null</code> if <code>null</code> input
  264. */
  265. public static Object[] clone(Object[] array) {
  266. if (array == null) {
  267. return null;
  268. }
  269. return (Object[]) array.clone();
  270. }
  271. /**
  272. * <p>Clones an array returning a typecast result and handling
  273. * <code>null</code>.</p>
  274. *
  275. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  276. *
  277. * @param array the array to clone, may be <code>null</code>
  278. * @return the cloned array, <code>null</code> if <code>null</code> input
  279. */
  280. public static long[] clone(long[] array) {
  281. if (array == null) {
  282. return null;
  283. }
  284. return (long[]) array.clone();
  285. }
  286. /**
  287. * <p>Clones an array returning a typecast result and handling
  288. * <code>null</code>.</p>
  289. *
  290. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  291. *
  292. * @param array the array to clone, may be <code>null</code>
  293. * @return the cloned array, <code>null</code> if <code>null</code> input
  294. */
  295. public static int[] clone(int[] array) {
  296. if (array == null) {
  297. return null;
  298. }
  299. return (int[]) array.clone();
  300. }
  301. /**
  302. * <p>Clones an array returning a typecast result and handling
  303. * <code>null</code>.</p>
  304. *
  305. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  306. *
  307. * @param array the array to clone, may be <code>null</code>
  308. * @return the cloned array, <code>null</code> if <code>null</code> input
  309. */
  310. public static short[] clone(short[] array) {
  311. if (array == null) {
  312. return null;
  313. }
  314. return (short[]) array.clone();
  315. }
  316. /**
  317. * <p>Clones an array returning a typecast result and handling
  318. * <code>null</code>.</p>
  319. *
  320. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  321. *
  322. * @param array the array to clone, may be <code>null</code>
  323. * @return the cloned array, <code>null</code> if <code>null</code> input
  324. */
  325. public static char[] clone(char[] array) {
  326. if (array == null) {
  327. return null;
  328. }
  329. return (char[]) array.clone();
  330. }
  331. /**
  332. * <p>Clones an array returning a typecast result and handling
  333. * <code>null</code>.</p>
  334. *
  335. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  336. *
  337. * @param array the array to clone, may be <code>null</code>
  338. * @return the cloned array, <code>null</code> if <code>null</code> input
  339. */
  340. public static byte[] clone(byte[] array) {
  341. if (array == null) {
  342. return null;
  343. }
  344. return (byte[]) array.clone();
  345. }
  346. /**
  347. * <p>Clones an array returning a typecast result and handling
  348. * <code>null</code>.</p>
  349. *
  350. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  351. *
  352. * @param array the array to clone, may be <code>null</code>
  353. * @return the cloned array, <code>null</code> if <code>null</code> input
  354. */
  355. public static double[] clone(double[] array) {
  356. if (array == null) {
  357. return null;
  358. }
  359. return (double[]) array.clone();
  360. }
  361. /**
  362. * <p>Clones an array returning a typecast result and handling
  363. * <code>null</code>.</p>
  364. *
  365. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  366. *
  367. * @param array the array to clone, may be <code>null</code>
  368. * @return the cloned array, <code>null</code> if <code>null</code> input
  369. */
  370. public static float[] clone(float[] array) {
  371. if (array == null) {
  372. return null;
  373. }
  374. return (float[]) array.clone();
  375. }
  376. /**
  377. * <p>Clones an array returning a typecast result and handling
  378. * <code>null</code>.</p>
  379. *
  380. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  381. *
  382. * @param array the array to clone, may be <code>null</code>
  383. * @return the cloned array, <code>null</code> if <code>null</code> input
  384. */
  385. public static boolean[] clone(boolean[] array) {
  386. if (array == null) {
  387. return null;
  388. }
  389. return (boolean[]) array.clone();
  390. }
  391. // Subarrays
  392. //-----------------------------------------------------------------------
  393. /**
  394. * <p>Produces a new array containing the elements between
  395. * the start and end indices.</p>
  396. *
  397. * <p>The start index is inclusive, the end index exclusive.
  398. * Null array input produces null output.</p>
  399. *
  400. * <p>The component type of the subarray is always the same as
  401. * that of the input array. Thus, if the input is an array of type
  402. * <code>Date</code>, the following usage is envisaged:</p>
  403. *
  404. * <pre>
  405. * Date[] someDates = (Date[])ArrayUtils.subarray(allDates, 2, 5);
  406. * </pre>
  407. *
  408. * @param array the array
  409. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  410. * is promoted to 0, overvalue (&gt;array.length) results
  411. * in an empty array.
  412. * @param endIndexExclusive elements up to endIndex-1 are present in the
  413. * returned subarray. Undervalue (&lt; startIndex) produces
  414. * empty array, overvalue (&gt;array.length) is demoted to
  415. * array length.
  416. * @return a new array containing the elements between
  417. * the start and end indices.
  418. * @since 2.1
  419. */
  420. public static Object[] subarray(Object[] array, int startIndexInclusive, int endIndexExclusive) {
  421. if (array == null) {
  422. return null;
  423. }
  424. if (startIndexInclusive < 0) {
  425. startIndexInclusive = 0;
  426. }
  427. if (endIndexExclusive > array.length) {
  428. endIndexExclusive = array.length;
  429. }
  430. int newSize = endIndexExclusive - startIndexInclusive;
  431. Class type = array.getClass().getComponentType();
  432. if (newSize <= 0) {
  433. return (Object[]) Array.newInstance(type, 0);
  434. }
  435. Object[] subarray = (Object[]) Array.newInstance(type, newSize);
  436. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  437. return subarray;
  438. }
  439. /**
  440. * <p>Produces a new <code>long</code> array containing the elements
  441. * between the start and end indices.</p>
  442. *
  443. * <p>The start index is inclusive, the end index exclusive.
  444. * Null array input produces null output.</p>
  445. *
  446. * @param array the array
  447. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  448. * is promoted to 0, overvalue (&gt;array.length) results
  449. * in an empty array.
  450. * @param endIndexExclusive elements up to endIndex-1 are present in the
  451. * returned subarray. Undervalue (&lt; startIndex) produces
  452. * empty array, overvalue (&gt;array.length) is demoted to
  453. * array length.
  454. * @return a new array containing the elements between
  455. * the start and end indices.
  456. * @since 2.1
  457. */
  458. public static long[] subarray(long[] array, int startIndexInclusive, int endIndexExclusive) {
  459. if (array == null) {
  460. return null;
  461. }
  462. if (startIndexInclusive < 0) {
  463. startIndexInclusive = 0;
  464. }
  465. if (endIndexExclusive > array.length) {
  466. endIndexExclusive = array.length;
  467. }
  468. int newSize = endIndexExclusive - startIndexInclusive;
  469. if (newSize <= 0) {
  470. return EMPTY_LONG_ARRAY;
  471. }
  472. long[] subarray = new long[newSize];
  473. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  474. return subarray;
  475. }
  476. /**
  477. * <p>Produces a new <code>int</code> array containing the elements
  478. * between the start and end indices.</p>
  479. *
  480. * <p>The start index is inclusive, the end index exclusive.
  481. * Null array input produces null output.</p>
  482. *
  483. * @param array the array
  484. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  485. * is promoted to 0, overvalue (&gt;array.length) results
  486. * in an empty array.
  487. * @param endIndexExclusive elements up to endIndex-1 are present in the
  488. * returned subarray. Undervalue (&lt; startIndex) produces
  489. * empty array, overvalue (&gt;array.length) is demoted to
  490. * array length.
  491. * @return a new array containing the elements between
  492. * the start and end indices.
  493. * @since 2.1
  494. */
  495. public static int[] subarray(int[] array, int startIndexInclusive, int endIndexExclusive) {
  496. if (array == null) {
  497. return null;
  498. }
  499. if (startIndexInclusive < 0) {
  500. startIndexInclusive = 0;
  501. }
  502. if (endIndexExclusive > array.length) {
  503. endIndexExclusive = array.length;
  504. }
  505. int newSize = endIndexExclusive - startIndexInclusive;
  506. if (newSize <= 0) {
  507. return EMPTY_INT_ARRAY;
  508. }
  509. int[] subarray = new int[newSize];
  510. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  511. return subarray;
  512. }
  513. /**
  514. * <p>Produces a new <code>short</code> array containing the elements
  515. * between the start and end indices.</p>
  516. *
  517. * <p>The start index is inclusive, the end index exclusive.
  518. * Null array input produces null output.</p>
  519. *
  520. * @param array the array
  521. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  522. * is promoted to 0, overvalue (&gt;array.length) results
  523. * in an empty array.
  524. * @param endIndexExclusive elements up to endIndex-1 are present in the
  525. * returned subarray. Undervalue (&lt; startIndex) produces
  526. * empty array, overvalue (&gt;array.length) is demoted to
  527. * array length.
  528. * @return a new array containing the elements between
  529. * the start and end indices.
  530. * @since 2.1
  531. */
  532. public static short[] subarray(short[] array, int startIndexInclusive, int endIndexExclusive) {
  533. if (array == null) {
  534. return null;
  535. }
  536. if (startIndexInclusive < 0) {
  537. startIndexInclusive = 0;
  538. }
  539. if (endIndexExclusive > array.length) {
  540. endIndexExclusive = array.length;
  541. }
  542. int newSize = endIndexExclusive - startIndexInclusive;
  543. if (newSize <= 0) {
  544. return EMPTY_SHORT_ARRAY;
  545. }
  546. short[] subarray = new short[newSize];
  547. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  548. return subarray;
  549. }
  550. /**
  551. * <p>Produces a new <code>char</code> array containing the elements
  552. * between the start and end indices.</p>
  553. *
  554. * <p>The start index is inclusive, the end index exclusive.
  555. * Null array input produces null output.</p>
  556. *
  557. * @param array the array
  558. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  559. * is promoted to 0, overvalue (&gt;array.length) results
  560. * in an empty array.
  561. * @param endIndexExclusive elements up to endIndex-1 are present in the
  562. * returned subarray. Undervalue (&lt; startIndex) produces
  563. * empty array, overvalue (&gt;array.length) is demoted to
  564. * array length.
  565. * @return a new array containing the elements between
  566. * the start and end indices.
  567. * @since 2.1
  568. */
  569. public static char[] subarray(char[] array, int startIndexInclusive, int endIndexExclusive) {
  570. if (array == null) {
  571. return null;
  572. }
  573. if (startIndexInclusive < 0) {
  574. startIndexInclusive = 0;
  575. }
  576. if (endIndexExclusive > array.length) {
  577. endIndexExclusive = array.length;
  578. }
  579. int newSize = endIndexExclusive - startIndexInclusive;
  580. if (newSize <= 0) {
  581. return EMPTY_CHAR_ARRAY;
  582. }
  583. char[] subarray = new char[newSize];
  584. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  585. return subarray;
  586. }
  587. /**
  588. * <p>Produces a new <code>byte</code> array containing the elements
  589. * between the start and end indices.</p>
  590. *
  591. * <p>The start index is inclusive, the end index exclusive.
  592. * Null array input produces null output.</p>
  593. *
  594. * @param array the array
  595. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  596. * is promoted to 0, overvalue (&gt;array.length) results
  597. * in an empty array.
  598. * @param endIndexExclusive elements up to endIndex-1 are present in the
  599. * returned subarray. Undervalue (&lt; startIndex) produces
  600. * empty array, overvalue (&gt;array.length) is demoted to
  601. * array length.
  602. * @return a new array containing the elements between
  603. * the start and end indices.
  604. * @since 2.1
  605. */
  606. public static byte[] subarray(byte[] array, int startIndexInclusive, int endIndexExclusive) {
  607. if (array == null) {
  608. return null;
  609. }
  610. if (startIndexInclusive < 0) {
  611. startIndexInclusive = 0;
  612. }
  613. if (endIndexExclusive > array.length) {
  614. endIndexExclusive = array.length;
  615. }
  616. int newSize = endIndexExclusive - startIndexInclusive;
  617. if (newSize <= 0) {
  618. return EMPTY_BYTE_ARRAY;
  619. }
  620. byte[] subarray = new byte[newSize];
  621. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  622. return subarray;
  623. }
  624. /**
  625. * <p>Produces a new <code>double</code> array containing the elements
  626. * between the start and end indices.</p>
  627. *
  628. * <p>The start index is inclusive, the end index exclusive.
  629. * Null array input produces null output.</p>
  630. *
  631. * @param array the array
  632. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  633. * is promoted to 0, overvalue (&gt;array.length) results
  634. * in an empty array.
  635. * @param endIndexExclusive elements up to endIndex-1 are present in the
  636. * returned subarray. Undervalue (&lt; startIndex) produces
  637. * empty array, overvalue (&gt;array.length) is demoted to
  638. * array length.
  639. * @return a new array containing the elements between
  640. * the start and end indices.
  641. * @since 2.1
  642. */
  643. public static double[] subarray(double[] array, int startIndexInclusive, int endIndexExclusive) {
  644. if (array == null) {
  645. return null;
  646. }
  647. if (startIndexInclusive < 0) {
  648. startIndexInclusive = 0;
  649. }
  650. if (endIndexExclusive > array.length) {
  651. endIndexExclusive = array.length;
  652. }
  653. int newSize = endIndexExclusive - startIndexInclusive;
  654. if (newSize <= 0) {
  655. return EMPTY_DOUBLE_ARRAY;
  656. }
  657. double[] subarray = new double[newSize];
  658. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  659. return subarray;
  660. }
  661. /**
  662. * <p>Produces a new <code>float</code> array containing the elements
  663. * between the start and end indices.</p>
  664. *
  665. * <p>The start index is inclusive, the end index exclusive.
  666. * Null array input produces null output.</p>
  667. *
  668. * @param array the array
  669. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  670. * is promoted to 0, overvalue (&gt;array.length) results
  671. * in an empty array.
  672. * @param endIndexExclusive elements up to endIndex-1 are present in the
  673. * returned subarray. Undervalue (&lt; startIndex) produces
  674. * empty array, overvalue (&gt;array.length) is demoted to
  675. * array length.
  676. * @return a new array containing the elements between
  677. * the start and end indices.
  678. * @since 2.1
  679. */
  680. public static float[] subarray(float[] array, int startIndexInclusive, int endIndexExclusive) {
  681. if (array == null) {
  682. return null;
  683. }
  684. if (startIndexInclusive < 0) {
  685. startIndexInclusive = 0;
  686. }
  687. if (endIndexExclusive > array.length) {
  688. endIndexExclusive = array.length;
  689. }
  690. int newSize = endIndexExclusive - startIndexInclusive;
  691. if (newSize <= 0) {
  692. return EMPTY_FLOAT_ARRAY;
  693. }
  694. float[] subarray = new float[newSize];
  695. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  696. return subarray;
  697. }
  698. /**
  699. * <p>Produces a new <code>boolean</code> array containing the elements
  700. * between the start and end indices.</p>
  701. *
  702. * <p>The start index is inclusive, the end index exclusive.
  703. * Null array input produces null output.</p>
  704. *
  705. * @param array the array
  706. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  707. * is promoted to 0, overvalue (&gt;array.length) results
  708. * in an empty array.
  709. * @param endIndexExclusive elements up to endIndex-1 are present in the
  710. * returned subarray. Undervalue (&lt; startIndex) produces
  711. * empty array, overvalue (&gt;array.length) is demoted to
  712. * array length.
  713. * @return a new array containing the elements between
  714. * the start and end indices.
  715. * @since 2.1
  716. */
  717. public static boolean[] subarray(boolean[] array, int startIndexInclusive, int endIndexExclusive) {
  718. if (array == null) {
  719. return null;
  720. }
  721. if (startIndexInclusive < 0) {
  722. startIndexInclusive = 0;
  723. }
  724. if (endIndexExclusive > array.length) {
  725. endIndexExclusive = array.length;
  726. }
  727. int newSize = endIndexExclusive - startIndexInclusive;
  728. if (newSize <= 0) {
  729. return EMPTY_BOOLEAN_ARRAY;
  730. }
  731. boolean[] subarray = new boolean[newSize];
  732. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  733. return subarray;
  734. }
  735. // Is same length
  736. //-----------------------------------------------------------------------
  737. /**
  738. * <p>Checks whether two arrays are the same length, treating
  739. * <code>null</code> arrays as length <code>0</code>.
  740. *
  741. * <p>Any multi-dimensional aspects of the arrays are ignored.</p>
  742. *
  743. * @param array1 the first array, may be <code>null</code>
  744. * @param array2 the second array, may be <code>null</code>
  745. * @return <code>true</code> if length of arrays matches, treating
  746. * <code>null</code> as an empty array
  747. */
  748. public static boolean isSameLength(Object[] array1, Object[] array2) {
  749. if ((array1 == null && array2 != null && array2.length > 0) ||
  750. (array2 == null && array1 != null && array1.length > 0) ||
  751. (array1 != null && array2 != null && array1.length != array2.length)) {
  752. return false;
  753. }
  754. return true;
  755. }
  756. /**
  757. * <p>Checks whether two arrays are the same length, treating
  758. * <code>null</code> arrays as length <code>0</code>.</p>
  759. *
  760. * @param array1 the first array, may be <code>null</code>
  761. * @param array2 the second array, may be <code>null</code>
  762. * @return <code>true</code> if length of arrays matches, treating
  763. * <code>null</code> as an empty array
  764. */
  765. public static boolean isSameLength(long[] array1, long[] array2) {
  766. if ((array1 == null && array2 != null && array2.length > 0) ||
  767. (array2 == null && array1 != null && array1.length > 0) ||
  768. (array1 != null && array2 != null && array1.length != array2.length)) {
  769. return false;
  770. }
  771. return true;
  772. }
  773. /**
  774. * <p>Checks whether two arrays are the same length, treating
  775. * <code>null</code> arrays as length <code>0</code>.</p>
  776. *
  777. * @param array1 the first array, may be <code>null</code>
  778. * @param array2 the second array, may be <code>null</code>
  779. * @return <code>true</code> if length of arrays matches, treating
  780. * <code>null</code> as an empty array
  781. */
  782. public static boolean isSameLength(int[] array1, int[] array2) {
  783. if ((array1 == null && array2 != null && array2.length > 0) ||
  784. (array2 == null && array1 != null && array1.length > 0) ||
  785. (array1 != null && array2 != null && array1.length != array2.length)) {
  786. return false;
  787. }
  788. return true;
  789. }
  790. /**
  791. * <p>Checks whether two arrays are the same length, treating
  792. * <code>null</code> arrays as length <code>0</code>.</p>
  793. *
  794. * @param array1 the first array, may be <code>null</code>
  795. * @param array2 the second array, may be <code>null</code>
  796. * @return <code>true</code> if length of arrays matches, treating
  797. * <code>null</code> as an empty array
  798. */
  799. public static boolean isSameLength(short[] array1, short[] array2) {
  800. if ((array1 == null && array2 != null && array2.length > 0) ||
  801. (array2 == null && array1 != null && array1.length > 0) ||
  802. (array1 != null && array2 != null && array1.length != array2.length)) {
  803. return false;
  804. }
  805. return true;
  806. }
  807. /**
  808. * <p>Checks whether two arrays are the same length, treating
  809. * <code>null</code> arrays as length <code>0</code>.</p>
  810. *
  811. * @param array1 the first array, may be <code>null</code>
  812. * @param array2 the second array, may be <code>null</code>
  813. * @return <code>true</code> if length of arrays matches, treating
  814. * <code>null</code> as an empty array
  815. */
  816. public static boolean isSameLength(char[] array1, char[] array2) {
  817. if ((array1 == null && array2 != null && array2.length > 0) ||
  818. (array2 == null && array1 != null && array1.length > 0) ||
  819. (array1 != null && array2 != null && array1.length != array2.length)) {
  820. return false;
  821. }
  822. return true;
  823. }
  824. /**
  825. * <p>Checks whether two arrays are the same length, treating
  826. * <code>null</code> arrays as length <code>0</code>.</p>
  827. *
  828. * @param array1 the first array, may be <code>null</code>
  829. * @param array2 the second array, may be <code>null</code>
  830. * @return <code>true</code> if length of arrays matches, treating
  831. * <code>null</code> as an empty array
  832. */
  833. public static boolean isSameLength(byte[] array1, byte[] array2) {
  834. if ((array1 == null && array2 != null && array2.length > 0) ||
  835. (array2 == null && array1 != null && array1.length > 0) ||
  836. (array1 != null && array2 != null && array1.length != array2.length)) {
  837. return false;
  838. }
  839. return true;
  840. }
  841. /**
  842. * <p>Checks whether two arrays are the same length, treating
  843. * <code>null</code> arrays as length <code>0</code>.</p>
  844. *
  845. * @param array1 the first array, may be <code>null</code>
  846. * @param array2 the second array, may be <code>null</code>
  847. * @return <code>true</code> if length of arrays matches, treating
  848. * <code>null</code> as an empty array
  849. */
  850. public static boolean isSameLength(double[] array1, double[] array2) {
  851. if ((array1 == null && array2 != null && array2.length > 0) ||
  852. (array2 == null && array1 != null && array1.length > 0) ||
  853. (array1 != null && array2 != null && array1.length != array2.length)) {
  854. return false;
  855. }
  856. return true;
  857. }
  858. /**
  859. * <p>Checks whether two arrays are the same length, treating
  860. * <code>null</code> arrays as length <code>0</code>.</p>
  861. *
  862. * @param array1 the first array, may be <code>null</code>
  863. * @param array2 the second array, may be <code>null</code>
  864. * @return <code>true</code> if length of arrays matches, treating
  865. * <code>null</code> as an empty array
  866. */
  867. public static boolean isSameLength(float[] array1, float[] array2) {
  868. if ((array1 == null && array2 != null && array2.length > 0) ||
  869. (array2 == null && array1 != null && array1.length > 0) ||
  870. (array1 != null && array2 != null && array1.length != array2.length)) {
  871. return false;
  872. }
  873. return true;
  874. }
  875. /**
  876. * <p>Checks whether two arrays are the same length, treating
  877. * <code>null</code> arrays as length <code>0</code>.</p>
  878. *
  879. * @param array1 the first array, may be <code>null</code>
  880. * @param array2 the second array, may be <code>null</code>
  881. * @return <code>true</code> if length of arrays matches, treating
  882. * <code>null</code> as an empty array
  883. */
  884. public static boolean isSameLength(boolean[] array1, boolean[] array2) {
  885. if ((array1 == null && array2 != null && array2.length > 0) ||
  886. (array2 == null && array1 != null && array1.length > 0) ||
  887. (array1 != null && array2 != null && array1.length != array2.length)) {
  888. return false;
  889. }
  890. return true;
  891. }
  892. //-----------------------------------------------------------------------
  893. /**
  894. * <p>Returns the length of the specified array.
  895. * This method can deal with <code>Object</code> arrays and with primitive arrays.</p>
  896. *
  897. * <p>If the input array is <code>null</code>, <code>0</code> is returned.</p>
  898. *
  899. * <pre>
  900. * ArrayUtils.getLength(null) = 0
  901. * ArrayUtils.getLength([]) = 0
  902. * ArrayUtils.getLength([null]) = 1
  903. * ArrayUtils.getLength([true, false]) = 2
  904. * ArrayUtils.getLength([1, 2, 3]) = 3
  905. * ArrayUtils.getLength(["a", "b", "c"]) = 3
  906. * </pre>
  907. *
  908. * @param array the array to retrieve the length from, may be null
  909. * @return The length of the array, or <code>0</code> if the array is <code>null</code>
  910. * @throws IllegalArgumentException if the object arguement is not an array.
  911. * @since 2.1
  912. */
  913. public static int getLength(Object array) {
  914. if (array == null) {
  915. return 0;
  916. }
  917. return Array.getLength(array);
  918. }
  919. /**
  920. * <p>Checks whether two arrays are the same type taking into account
  921. * multi-dimensional arrays.</p>
  922. *
  923. * @param array1 the first array, must not be <code>null</code>
  924. * @param array2 the second array, must not be <code>null</code>
  925. * @return <code>true</code> if type of arrays matches
  926. * @throws IllegalArgumentException if either array is <code>null</code>
  927. */
  928. public static boolean isSameType(Object array1, Object array2) {
  929. if (array1 == null || array2 == null) {
  930. throw new IllegalArgumentException("The Array must not be null");
  931. }
  932. return array1.getClass().getName().equals(array2.getClass().getName());
  933. }
  934. // Reverse
  935. //-----------------------------------------------------------------------
  936. /**
  937. * <p>Reverses the order of the given array.</p>
  938. *
  939. * <p>There is no special handling for multi-dimensional arrays.</p>
  940. *
  941. * <p>This method does nothing for a <code>null</code> input array.</p>
  942. *
  943. * @param array the array to reverse, may be <code>null</code>
  944. */
  945. public static void reverse(Object[] array) {
  946. if (array == null) {
  947. return;
  948. }
  949. int i = 0;
  950. int j = array.length - 1;
  951. Object tmp;
  952. while (j > i) {
  953. tmp = array[j];
  954. array[j] = array[i];
  955. array[i] = tmp;
  956. j--;
  957. i++;
  958. }
  959. }
  960. /**
  961. * <p>Reverses the order of the given array.</p>
  962. *
  963. * <p>This method does nothing for a <code>null</code> input array.</p>
  964. *
  965. * @param array the array to reverse, may be <code>null</code>
  966. */
  967. public static void reverse(long[] array) {
  968. if (array == null) {
  969. return;
  970. }
  971. int i = 0;
  972. int j = array.length - 1;
  973. long tmp;
  974. while (j > i) {
  975. tmp = array[j];
  976. array[j] = array[i];
  977. array[i] = tmp;
  978. j--;
  979. i++;
  980. }
  981. }
  982. /**
  983. * <p>Reverses the order of the given array.</p>
  984. *
  985. * <p>This method does nothing for a <code>null</code> input array.</p>
  986. *
  987. * @param array the array to reverse, may be <code>null</code>
  988. */
  989. public static void reverse(int[] array) {
  990. if (array == null) {
  991. return;
  992. }
  993. int i = 0;
  994. int j = array.length - 1;
  995. int tmp;
  996. while (j > i) {
  997. tmp = array[j];
  998. array[j] = array[i];
  999. array[i] = tmp;
  1000. j--;
  1001. i++;
  1002. }
  1003. }
  1004. /**
  1005. * <p>Reverses the order of the given array.</p>
  1006. *
  1007. * <p>This method does nothing for a <code>null</code> input array.</p>
  1008. *
  1009. * @param array the array to reverse, may be <code>null</code>
  1010. */
  1011. public static void reverse(short[] array) {
  1012. if (array == null) {
  1013. return;
  1014. }
  1015. int i = 0;
  1016. int j = array.length - 1;
  1017. short tmp;
  1018. while (j > i) {
  1019. tmp = array[j];
  1020. array[j] = array[i];
  1021. array[i] = tmp;
  1022. j--;
  1023. i++;
  1024. }
  1025. }
  1026. /**
  1027. * <p>Reverses the order of the given array.</p>
  1028. *
  1029. * <p>This method does nothing for a <code>null</code> input array.</p>
  1030. *
  1031. * @param array the array to reverse, may be <code>null</code>
  1032. */
  1033. public static void reverse(char[] array) {
  1034. if (array == null) {
  1035. return;
  1036. }
  1037. int i = 0;
  1038. int j = array.length - 1;
  1039. char tmp;
  1040. while (j > i) {
  1041. tmp = array[j];
  1042. array[j] = array[i];
  1043. array[i] = tmp;
  1044. j--;
  1045. i++;
  1046. }
  1047. }
  1048. /**
  1049. * <p>Reverses the order of the given array.</p>
  1050. *
  1051. * <p>This method does nothing for a <code>null</code> input array.</p>
  1052. *
  1053. * @param array the array to reverse, may be <code>null</code>
  1054. */
  1055. public static void reverse(byte[] array) {
  1056. if (array == null) {
  1057. return;
  1058. }
  1059. int i = 0;
  1060. int j = array.length - 1;
  1061. byte tmp;
  1062. while (j > i) {
  1063. tmp = array[j];
  1064. array[j] = array[i];
  1065. array[i] = tmp;
  1066. j--;
  1067. i++;
  1068. }
  1069. }
  1070. /**
  1071. * <p>Reverses the order of the given array.</p>
  1072. *
  1073. * <p>This method does nothing for a <code>null</code> input array.</p>
  1074. *
  1075. * @param array the array to reverse, may be <code>null</code>
  1076. */
  1077. public static void reverse(double[] array) {
  1078. if (array == null) {
  1079. return;
  1080. }
  1081. int i = 0;
  1082. int j = array.length - 1;
  1083. double tmp;
  1084. while (j > i) {
  1085. tmp = array[j];
  1086. array[j] = array[i];
  1087. array[i] = tmp;
  1088. j--;
  1089. i++;
  1090. }
  1091. }
  1092. /**
  1093. * <p>Reverses the order of the given array.</p>
  1094. *
  1095. * <p>This method does nothing for a <code>null</code> input array.</p>
  1096. *
  1097. * @param array the array to reverse, may be <code>null</code>
  1098. */
  1099. public static void reverse(float[] array) {
  1100. if (array == null) {
  1101. return;
  1102. }
  1103. int i = 0;
  1104. int j = array.length - 1;
  1105. float tmp;
  1106. while (j > i) {
  1107. tmp = array[j];
  1108. array[j] = array[i];
  1109. array[i] = tmp;
  1110. j--;
  1111. i++;
  1112. }
  1113. }
  1114. /**
  1115. * <p>Reverses the order of the given array.</p>
  1116. *
  1117. * <p>This method does nothing for a <code>null</code> input array.</p>
  1118. *
  1119. * @param array the array to reverse, may be <code>null</code>
  1120. */
  1121. public static void reverse(boolean[] array) {
  1122. if (array == null) {
  1123. return;
  1124. }
  1125. int i = 0;
  1126. int j = array.length - 1;
  1127. boolean tmp;
  1128. while (j > i) {
  1129. tmp = array[j];
  1130. array[j] = array[i];
  1131. array[i] = tmp;
  1132. j--;
  1133. i++;
  1134. }
  1135. }
  1136. // IndexOf search
  1137. // ----------------------------------------------------------------------
  1138. // Object IndexOf
  1139. //-----------------------------------------------------------------------
  1140. /**
  1141. * <p>Finds the index of the given object in the array.</p>
  1142. *
  1143. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1144. *
  1145. * @param array the array to search through for the object, may be <code>null</code>
  1146. * @param objectToFind the object to find, may be <code>null</code>
  1147. * @return the index of the object within the array,
  1148. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1149. */
  1150. public static int indexOf(Object[] array, Object objectToFind) {
  1151. return indexOf(array, objectToFind, 0);
  1152. }
  1153. /**
  1154. * <p>Finds the index of the given object in the array starting at the given index.</p>
  1155. *
  1156. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1157. *
  1158. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1159. * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
  1160. *
  1161. * @param array the array to search through for the object, may be <code>null</code>
  1162. * @param objectToFind the object to find, may be <code>null</code>
  1163. * @param startIndex the index to start searching at
  1164. * @return the index of the object within the array starting at the index,
  1165. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1166. */
  1167. public static int indexOf(Object[] array, Object objectToFind, int startIndex) {
  1168. if (array == null) {
  1169. return INDEX_NOT_FOUND;
  1170. }
  1171. if (startIndex < 0) {
  1172. startIndex = 0;
  1173. }
  1174. if (objectToFind == null) {
  1175. for (int i = startIndex; i < array.length; i++) {
  1176. if (array[i] == null) {
  1177. return i;
  1178. }
  1179. }
  1180. } else {
  1181. for (int i = startIndex; i < array.length; i++) {
  1182. if (objectToFind.equals(array[i])) {
  1183. return i;
  1184. }
  1185. }
  1186. }
  1187. return INDEX_NOT_FOUND;
  1188. }
  1189. /**
  1190. * <p>Finds the last index of the given object within the array.</p>
  1191. *
  1192. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1193. *
  1194. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  1195. * @param objectToFind the object to find, may be <code>null</code>
  1196. * @return the last index of the object within the array,
  1197. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1198. */
  1199. public static int lastIndexOf(Object[] array, Object objectToFind) {
  1200. return lastIndexOf(array, objectToFind, Integer.MAX_VALUE);
  1201. }
  1202. /**
  1203. * <p>Finds the last index of the given object in the array starting at the given index.</p>
  1204. *
  1205. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1206. *
  1207. * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than
  1208. * the array length will search from the end of the array.</p>
  1209. *
  1210. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1211. * @param objectToFind the object to find, may be <code>null</code>
  1212. * @param startIndex the start index to travers backwards from
  1213. * @return the last index of the object within the array,
  1214. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1215. */
  1216. public static int lastIndexOf(Object[] array, Object objectToFind, int startIndex) {
  1217. if (array == null) {
  1218. return INDEX_NOT_FOUND;
  1219. }
  1220. if (startIndex < 0) {
  1221. return INDEX_NOT_FOUND;
  1222. } else if (startIndex >= array.length) {
  1223. startIndex = array.length - 1;
  1224. }
  1225. if (objectToFind == null) {
  1226. for (int i = startIndex; i >= 0; i--) {
  1227. if (array[i] == null) {
  1228. return i;
  1229. }
  1230. }
  1231. } else {
  1232. for (int i = startIndex; i >= 0; i--) {
  1233. if (objectToFind.equals(array[i])) {
  1234. return i;
  1235. }
  1236. }
  1237. }
  1238. return INDEX_NOT_FOUND;
  1239. }
  1240. /**
  1241. * <p>Checks if the object is in the given array.</p>
  1242. *
  1243. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  1244. *
  1245. * @param array the array to search through
  1246. * @param objectToFind the object to find
  1247. * @return <code>true</code> if the array contains the object
  1248. */
  1249. public static boolean contains(Object[] array, Object objectToFind) {
  1250. return indexOf(array, objectToFind) != INDEX_NOT_FOUND;
  1251. }
  1252. // long IndexOf
  1253. //-----------------------------------------------------------------------
  1254. /**
  1255. * <p>Finds the index of the given value in the array.</p>
  1256. *
  1257. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1258. *
  1259. * @param array the array to search through for the object, may be <code>null</code>
  1260. * @param valueToFind the value to find
  1261. * @return the index of the value within the array,
  1262. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1263. */
  1264. public static int indexOf(long[] array, long valueToFind) {
  1265. return indexOf(array, valueToFind, 0);
  1266. }
  1267. /**
  1268. * <p>Finds the index of the given value in the array starting at the given index.</p>
  1269. *
  1270. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1271. *
  1272. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1273. * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
  1274. *
  1275. * @param array the array to search through for the object, may be <code>null</code>
  1276. * @param valueToFind the value to find
  1277. * @param startIndex the index to start searching at
  1278. * @return the index of the value within the array,
  1279. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1280. */
  1281. public static int indexOf(long[] array, long valueToFind, int startIndex) {
  1282. if (array == null) {
  1283. return INDEX_NOT_FOUND;
  1284. }
  1285. if (startIndex < 0) {
  1286. startIndex = 0;
  1287. }
  1288. for (int i = startIndex; i < array.length; i++) {
  1289. if (valueToFind == array[i]) {
  1290. return i;
  1291. }
  1292. }
  1293. return INDEX_NOT_FOUND;
  1294. }

Large files files are truncated, but you can click here to view the full file