1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowledgement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgement may appear in the software itself,
  24. * if and wherever such third-party acknowledgements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.commons.lang;
  55. import java.util.HashMap;
  56. import java.util.Map;
  57. import org.apache.commons.lang.builder.EqualsBuilder;
  58. import org.apache.commons.lang.builder.HashCodeBuilder;
  59. import org.apache.commons.lang.builder.ToStringBuilder;
  60. import org.apache.commons.lang.builder.ToStringStyle;
  61. /**
  62. * <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and primitive wrapper arrays
  63. * (like <code>Integer[]</code>).</p>
  64. *
  65. * <p>This class tries to handle <code>null</code> input gracefully.
  66. * An exception will not be thrown for a <code>null</code>
  67. * array input. However, an Object array that contains a <code>null</code>
  68. * element may throw an exception. Each method documents its behaviour.</p>
  69. *
  70. * @author Stephen Colebourne
  71. * @author Moritz Petersen
  72. * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
  73. * @author Nikolay Metchev
  74. * @author Matthew Hawthorne
  75. * @author Tim O'Brien
  76. * @author Pete Gieser
  77. * @author Gary Gregory
  78. * @since 2.0
  79. * @version $Id: ArrayUtils.java,v 1.25 2003/08/22 17:25:33 ggregory Exp $
  80. */
  81. public class ArrayUtils {
  82. /**
  83. * An empty immutable <code>Object</code> array.
  84. */
  85. public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
  86. /**
  87. * An empty immutable <code>Class</code> array.
  88. */
  89. public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
  90. /**
  91. * An empty immutable <code>String</code> array.
  92. */
  93. public static final String[] EMPTY_STRING_ARRAY = new String[0];
  94. /**
  95. * An empty immutable <code>long</code> array.
  96. */
  97. public static final long[] EMPTY_LONG_ARRAY = new long[0];
  98. /**
  99. * An empty immutable <code>Long</code> array.
  100. */
  101. public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0];
  102. /**
  103. * An empty immutable <code>int</code> array.
  104. */
  105. public static final int[] EMPTY_INT_ARRAY = new int[0];
  106. /**
  107. * An empty immutable <code>Integer</code> array.
  108. */
  109. public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];
  110. /**
  111. * An empty immutable <code>short</code> array.
  112. */
  113. public static final short[] EMPTY_SHORT_ARRAY = new short[0];
  114. /**
  115. * An empty immutable <code>Short</code> array.
  116. */
  117. public static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0];
  118. /**
  119. * An empty immutable <code>byte</code> array.
  120. */
  121. public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
  122. /**
  123. * An empty immutable <code>Byte</code> array.
  124. */
  125. public static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0];
  126. /**
  127. * An empty immutable <code>double</code> array.
  128. */
  129. public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
  130. /**
  131. * An empty immutable <code>Double</code> array.
  132. */
  133. public static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0];
  134. /**
  135. * An empty immutable <code>float</code> array.
  136. */
  137. public static final float[] EMPTY_FLOAT_ARRAY = new float[0];
  138. /**
  139. * An empty immutable <code>Float</code> array.
  140. */
  141. public static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0];
  142. /**
  143. * An empty immutable <code>boolean</code> array.
  144. */
  145. public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
  146. /**
  147. * An empty immutable <code>Boolean</code> array.
  148. */
  149. public static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0];
  150. /**
  151. * An empty immutable <code>char</code> array.
  152. */
  153. public static final char[] EMPTY_CHAR_ARRAY = new char[0];
  154. /**
  155. * An empty immutable <code>Character</code> array.
  156. */
  157. public static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0];
  158. /**
  159. * <p>ArrayUtils instances should NOT be constructed in standard programming.
  160. * Instead, the class should be used as <code>ArrayUtils.clone(new int[] {2})</code>.</p>
  161. *
  162. * <p>This constructor is public to permit tools that require a JavaBean instance
  163. * to operate.</p>
  164. */
  165. public ArrayUtils() {
  166. }
  167. // Basic methods handling multi-dimensional arrays
  168. //-----------------------------------------------------------------------
  169. /**
  170. * <p>Outputs an array as a String, treating <code>null</code> as an empty array.</p>
  171. *
  172. * <p>Multi-dimensional arrays are handled correctly, including
  173. * multi-dimensional primitive arrays.</p>
  174. *
  175. * <p>The format is that of Java source code, for example <code>{a,b}</code>.</p>
  176. *
  177. * @param array the array to get a toString for, may be <code>null</code>
  178. * @return a String representation of the array, '{}' if null array input
  179. */
  180. public static String toString(final Object array) {
  181. return toString(array, "{}");
  182. }
  183. /**
  184. * <p>Outputs an array as a String handling <code>null</code>s.</p>
  185. *
  186. * <p>Multi-dimensional arrays are handled correctly, including
  187. * multi-dimensional primitive arrays.</p>
  188. *
  189. * <p>The format is that of Java source code, for example <code>{a,b}</code>.</p>
  190. *
  191. * @param array the array to get a toString for, may be <code>null</code>
  192. * @param stringIfNull the String to return if the array is <code>null</code>
  193. * @return a String representation of the array
  194. */
  195. public static String toString(final Object array, final String stringIfNull) {
  196. if (array == null) {
  197. return stringIfNull;
  198. }
  199. return new ToStringBuilder(array, ToStringStyle.SIMPLE_STYLE).append(array).toString();
  200. }
  201. /**
  202. * <p>Get a hashCode for an array handling multi-dimensional arrays correctly.</p>
  203. *
  204. * <p>Multi-dimensional primitive arrays are also handled correctly by this method.</p>
  205. *
  206. * @param array the array to get a hashCode for, may be <code>null</code>
  207. * @return a hashCode for the array, zero if null array input
  208. */
  209. public static int hashCode(final Object array) {
  210. return new HashCodeBuilder().append(array).toHashCode();
  211. }
  212. /**
  213. * <p>Compares two arrays, using equals(), handling multi-dimensional arrays
  214. * correctly.</p>
  215. *
  216. * <p>Multi-dimensional primitive arrays are also handled correctly by this method.</p>
  217. *
  218. * @param array1 the array to get a hashCode for, may be <code>null</code>
  219. * @param array2 the array to get a hashCode for, may be <code>null</code>
  220. * @return <code>true</code> if the arrays are equal
  221. */
  222. public static boolean isEquals(final Object array1, final Object array2) {
  223. return new EqualsBuilder().append(array1, array2).isEquals();
  224. }
  225. // To map
  226. //-----------------------------------------------------------------------
  227. /**
  228. * <p>Converts the given array into a {@link java.util.Map}. Each element of the array
  229. * must be either a {@link java.util.Map.Entry} or an Array, containing at least two
  230. * elements, where the first element is used as key and the second as
  231. * value.</p>
  232. *
  233. * <p>This method can be used to initialize:</p>
  234. * <pre>
  235. * // Create a Map mapping colors.
  236. * Map colorMap = MapUtils.toMap(new String[][] {{
  237. * {"RED", "#FF0000"},
  238. * {"GREEN", "#00FF00"},
  239. * {"BLUE", "#0000FF"}});
  240. * </pre>
  241. *
  242. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  243. *
  244. * @param array an array whose elements are either a {@link java.util.Map.Entry} or
  245. * an Array containing at least two elements, may be <code>null</code>
  246. * @return a <code>Map</code> that was created from the array
  247. * @throws IllegalArgumentException if one element of this Array is
  248. * itself an Array containing less then two elements
  249. * @throws IllegalArgumentException if the array contains elements other
  250. * than {@link java.util.Map.Entry} and an Array
  251. */
  252. public static Map toMap(final Object[] array) {
  253. if (array == null) {
  254. return null;
  255. }
  256. final Map map = new HashMap((int) (array.length * 1.5));
  257. for (int i = 0; i < array.length; i++) {
  258. Object object = array[i];
  259. if (object instanceof Map.Entry) {
  260. Map.Entry entry = (Map.Entry) object;
  261. map.put(entry.getKey(), entry.getValue());
  262. } else if (object instanceof Object[]) {
  263. Object[] entry = (Object[]) object;
  264. if (entry.length < 2) {
  265. throw new IllegalArgumentException("Array element " + i + ", '"
  266. + object
  267. + "', has a length less than 2");
  268. }
  269. map.put(entry[0], entry[1]);
  270. } else {
  271. throw new IllegalArgumentException("Array element " + i + ", '"
  272. + object
  273. + "', is neither of type Map.Entry nor an Array");
  274. }
  275. }
  276. return map;
  277. }
  278. // Clone
  279. //-----------------------------------------------------------------------
  280. /**
  281. * <p>Shallow clones an array returning a typecast result and handling
  282. * <code>null</code>.</p>
  283. *
  284. * <p>The objecs in the array are not cloned, thus there is no special
  285. * handling for multi-dimensional arrays.</p>
  286. *
  287. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  288. *
  289. * @param array the array to shallow clone, may be <code>null</code>
  290. * @return the cloned array, <code>null</code> if <code>null</code> input
  291. */
  292. public static Object[] clone(final Object[] array) {
  293. if (array == null) {
  294. return null;
  295. }
  296. return (Object[]) array.clone();
  297. }
  298. /**
  299. * <p>Clones an array returning a typecast result and handling
  300. * <code>null</code>.</p>
  301. *
  302. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  303. *
  304. * @param array the array to clone, may be <code>null</code>
  305. * @return the cloned array, <code>null</code> if <code>null</code> input
  306. */
  307. public static long[] clone(final long[] array) {
  308. if (array == null) {
  309. return null;
  310. }
  311. return (long[]) array.clone();
  312. }
  313. /**
  314. * <p>Clones an array returning a typecast result and handling
  315. * <code>null</code>.</p>
  316. *
  317. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  318. *
  319. * @param array the array to clone, may be <code>null</code>
  320. * @return the cloned array, <code>null</code> if <code>null</code> input
  321. */
  322. public static int[] clone(int[] array) {
  323. if (array == null) {
  324. return null;
  325. }
  326. return (int[]) array.clone();
  327. }
  328. /**
  329. * <p>Clones an array returning a typecast result and handling
  330. * <code>null</code>.</p>
  331. *
  332. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  333. *
  334. * @param array the array to clone, may be <code>null</code>
  335. * @return the cloned array, <code>null</code> if <code>null</code> input
  336. */
  337. public static short[] clone(final short[] array) {
  338. if (array == null) {
  339. return null;
  340. }
  341. return (short[]) array.clone();
  342. }
  343. /**
  344. * <p>Clones an array returning a typecast result and handling
  345. * <code>null</code>.</p>
  346. *
  347. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  348. *
  349. * @param array the array to clone, may be <code>null</code>
  350. * @return the cloned array, <code>null</code> if <code>null</code> input
  351. */
  352. public static char[] clone(final char[] array) {
  353. if (array == null) {
  354. return null;
  355. }
  356. return (char[]) array.clone();
  357. }
  358. /**
  359. * <p>Clones an array returning a typecast result and handling
  360. * <code>null</code>.</p>
  361. *
  362. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  363. *
  364. * @param array the array to clone, may be <code>null</code>
  365. * @return the cloned array, <code>null</code> if <code>null</code> input
  366. */
  367. public static byte[] clone(final byte[] array) {
  368. if (array == null) {
  369. return null;
  370. }
  371. return (byte[]) array.clone();
  372. }
  373. /**
  374. * <p>Clones an array returning a typecast result and handling
  375. * <code>null</code>.</p>
  376. *
  377. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  378. *
  379. * @param array the array to clone, may be <code>null</code>
  380. * @return the cloned array, <code>null</code> if <code>null</code> input
  381. */
  382. public static double[] clone(final double[] array) {
  383. if (array == null) {
  384. return null;
  385. }
  386. return (double[]) array.clone();
  387. }
  388. /**
  389. * <p>Clones an array returning a typecast result and handling
  390. * <code>null</code>.</p>
  391. *
  392. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  393. *
  394. * @param array the array to clone, may be <code>null</code>
  395. * @return the cloned array, <code>null</code> if <code>null</code> input
  396. */
  397. public static float[] clone(final float[] array) {
  398. if (array == null) {
  399. return null;
  400. }
  401. return (float[]) array.clone();
  402. }
  403. /**
  404. * <p>Clones an array returning a typecast result and handling
  405. * <code>null</code>.</p>
  406. *
  407. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  408. *
  409. * @param array the array to clone, may be <code>null</code>
  410. * @return the cloned array, <code>null</code> if <code>null</code> input
  411. */
  412. public static boolean[] clone(final boolean[] array) {
  413. if (array == null) {
  414. return null;
  415. }
  416. return (boolean[]) array.clone();
  417. }
  418. // Is same length
  419. //-----------------------------------------------------------------------
  420. /**
  421. * <p>Checks whether two arrays are the same length, treating
  422. * <code>null</code> arrays as length <code>0</code>.
  423. *
  424. * <p>Any multi-dimensional aspects of the arrays are ignored.</p>
  425. *
  426. * @param array1 the first array, may be <code>null</code>
  427. * @param array2 the second array, may be <code>null</code>
  428. * @return <code>true</code> if length of arrays matches, treating
  429. * <code>null</code> as an empty array
  430. */
  431. public static boolean isSameLength(final Object[] array1, final Object[] array2) {
  432. if ((array1 == null && array2 != null && array2.length > 0) ||
  433. (array2 == null && array1 != null && array1.length > 0) ||
  434. (array1 != null && array2 != null && array1.length != array2.length)) {
  435. return false;
  436. }
  437. return true;
  438. }
  439. /**
  440. * <p>Checks whether two arrays are the same length, treating
  441. * <code>null</code> arrays as length <code>0</code>.</p>
  442. *
  443. * @param array1 the first array, may be <code>null</code>
  444. * @param array2 the second array, may be <code>null</code>
  445. * @return <code>true</code> if length of arrays matches, treating
  446. * <code>null</code> as an empty array
  447. */
  448. public static boolean isSameLength(final long[] array1, final long[] array2) {
  449. if ((array1 == null && array2 != null && array2.length > 0) ||
  450. (array2 == null && array1 != null && array1.length > 0) ||
  451. (array1 != null && array2 != null && array1.length != array2.length)) {
  452. return false;
  453. }
  454. return true;
  455. }
  456. /**
  457. * <p>Checks whether two arrays are the same length, treating
  458. * <code>null</code> arrays as length <code>0</code>.</p>
  459. *
  460. * @param array1 the first array, may be <code>null</code>
  461. * @param array2 the second array, may be <code>null</code>
  462. * @return <code>true</code> if length of arrays matches, treating
  463. * <code>null</code> as an empty array
  464. */
  465. public static boolean isSameLength(final int[] array1, final int[] array2) {
  466. if ((array1 == null && array2 != null && array2.length > 0) ||
  467. (array2 == null && array1 != null && array1.length > 0) ||
  468. (array1 != null && array2 != null && array1.length != array2.length)) {
  469. return false;
  470. }
  471. return true;
  472. }
  473. /**
  474. * <p>Checks whether two arrays are the same length, treating
  475. * <code>null</code> arrays as length <code>0</code>.</p>
  476. *
  477. * @param array1 the first array, may be <code>null</code>
  478. * @param array2 the second array, may be <code>null</code>
  479. * @return <code>true</code> if length of arrays matches, treating
  480. * <code>null</code> as an empty array
  481. */
  482. public static boolean isSameLength(final short[] array1, final short[] array2) {
  483. if ((array1 == null && array2 != null && array2.length > 0) ||
  484. (array2 == null && array1 != null && array1.length > 0) ||
  485. (array1 != null && array2 != null && array1.length != array2.length)) {
  486. return false;
  487. }
  488. return true;
  489. }
  490. /**
  491. * <p>Checks whether two arrays are the same length, treating
  492. * <code>null</code> arrays as length <code>0</code>.</p>
  493. *
  494. * @param array1 the first array, may be <code>null</code>
  495. * @param array2 the second array, may be <code>null</code>
  496. * @return <code>true</code> if length of arrays matches, treating
  497. * <code>null</code> as an empty array
  498. */
  499. public static boolean isSameLength(final char[] array1, final char[] array2) {
  500. if ((array1 == null && array2 != null && array2.length > 0) ||
  501. (array2 == null && array1 != null && array1.length > 0) ||
  502. (array1 != null && array2 != null && array1.length != array2.length)) {
  503. return false;
  504. }
  505. return true;
  506. }
  507. /**
  508. * <p>Checks whether two arrays are the same length, treating
  509. * <code>null</code> arrays as length <code>0</code>.</p>
  510. *
  511. * @param array1 the first array, may be <code>null</code>
  512. * @param array2 the second array, may be <code>null</code>
  513. * @return <code>true</code> if length of arrays matches, treating
  514. * <code>null</code> as an empty array
  515. */
  516. public static boolean isSameLength(final byte[] array1, final byte[] array2) {
  517. if ((array1 == null && array2 != null && array2.length > 0) ||
  518. (array2 == null && array1 != null && array1.length > 0) ||
  519. (array1 != null && array2 != null && array1.length != array2.length)) {
  520. return false;
  521. }
  522. return true;
  523. }
  524. /**
  525. * <p>Checks whether two arrays are the same length, treating
  526. * <code>null</code> arrays as length <code>0</code>.</p>
  527. *
  528. * @param array1 the first array, may be <code>null</code>
  529. * @param array2 the second array, may be <code>null</code>
  530. * @return <code>true</code> if length of arrays matches, treating
  531. * <code>null</code> as an empty array
  532. */
  533. public static boolean isSameLength(final double[] array1, final double[] array2) {
  534. if ((array1 == null && array2 != null && array2.length > 0) ||
  535. (array2 == null && array1 != null && array1.length > 0) ||
  536. (array1 != null && array2 != null && array1.length != array2.length)) {
  537. return false;
  538. }
  539. return true;
  540. }
  541. /**
  542. * <p>Checks whether two arrays are the same length, treating
  543. * <code>null</code> arrays as length <code>0</code>.</p>
  544. *
  545. * @param array1 the first array, may be <code>null</code>
  546. * @param array2 the second array, may be <code>null</code>
  547. * @return <code>true</code> if length of arrays matches, treating
  548. * <code>null</code> as an empty array
  549. */
  550. public static boolean isSameLength(final float[] array1, final float[] array2) {
  551. if ((array1 == null && array2 != null && array2.length > 0) ||
  552. (array2 == null && array1 != null && array1.length > 0) ||
  553. (array1 != null && array2 != null && array1.length != array2.length)) {
  554. return false;
  555. }
  556. return true;
  557. }
  558. /**
  559. * <p>Checks whether two arrays are the same length, treating
  560. * <code>null</code> arrays as length <code>0</code>.</p>
  561. *
  562. * @param array1 the first array, may be <code>null</code>
  563. * @param array2 the second array, may be <code>null</code>
  564. * @return <code>true</code> if length of arrays matches, treating
  565. * <code>null</code> as an empty array
  566. */
  567. public static boolean isSameLength(final boolean[] array1, final boolean[] array2) {
  568. if ((array1 == null && array2 != null && array2.length > 0) ||
  569. (array2 == null && array1 != null && array1.length > 0) ||
  570. (array1 != null && array2 != null && array1.length != array2.length)) {
  571. return false;
  572. }
  573. return true;
  574. }
  575. /**
  576. * <p>Checks whether two arrays are the same type taking into account
  577. * multi-dimensional arrays.</p>
  578. *
  579. * @param array1 the first array, must not be <code>null</code>
  580. * @param array2 the second array, must not be <code>null</code>
  581. * @return <code>true</code> if type of arrays matches
  582. * @throws IllegalArgumentException if either array is <code>null</code>
  583. */
  584. public static boolean isSameType(final Object array1, final Object array2) {
  585. if (array1 == null || array2 == null) {
  586. throw new IllegalArgumentException("The Array must not be null");
  587. }
  588. return array1.getClass().getName().equals(array2.getClass().getName());
  589. }
  590. // Reverse
  591. //-----------------------------------------------------------------------
  592. /**
  593. * <p>Reverses the order of the given array.</p>
  594. *
  595. * <p>There is no special handling for multi-dimensional arrays.</p>
  596. *
  597. * <p>This method does nothing if <code>null</code> array input.</p>
  598. *
  599. * @param array the array to reverse, may be <code>null</code>
  600. */
  601. public static void reverse(final Object[] array) {
  602. if (array == null) {
  603. return;
  604. }
  605. int i = 0;
  606. int j = array.length - 1;
  607. Object tmp;
  608. while (j > i) {
  609. tmp = array[j];
  610. array[j] = array[i];
  611. array[i] = tmp;
  612. j--;
  613. i++;
  614. }
  615. }
  616. /**
  617. * <p>Reverses the order of the given array.</p>
  618. *
  619. * <p>This method does nothing if <code>null</code> array input.</p>
  620. *
  621. * @param array the array to reverse, may be <code>null</code>
  622. */
  623. public static void reverse(final long[] array) {
  624. if (array == null) {
  625. return;
  626. }
  627. int i = 0;
  628. int j = array.length - 1;
  629. long tmp;
  630. while (j > i) {
  631. tmp = array[j];
  632. array[j] = array[i];
  633. array[i] = tmp;
  634. j--;
  635. i++;
  636. }
  637. }
  638. /**
  639. * <p>Reverses the order of the given array.</p>
  640. *
  641. * <p>This method does nothing if <code>null</code> array input.</p>
  642. *
  643. * @param array the array to reverse, may be <code>null</code>
  644. */
  645. public static void reverse(final int[] array) {
  646. if (array == null) {
  647. return;
  648. }
  649. int i = 0;
  650. int j = array.length - 1;
  651. int tmp;
  652. while (j > i) {
  653. tmp = array[j];
  654. array[j] = array[i];
  655. array[i] = tmp;
  656. j--;
  657. i++;
  658. }
  659. }
  660. /**
  661. * <p>Reverses the order of the given array.</p>
  662. *
  663. * <p>This method does nothing if <code>null</code> array input.</p>
  664. *
  665. * @param array the array to reverse, may be <code>null</code>
  666. */
  667. public static void reverse(final short[] array) {
  668. if (array == null) {
  669. return;
  670. }
  671. int i = 0;
  672. int j = array.length - 1;
  673. short tmp;
  674. while (j > i) {
  675. tmp = array[j];
  676. array[j] = array[i];
  677. array[i] = tmp;
  678. j--;
  679. i++;
  680. }
  681. }
  682. /**
  683. * <p>Reverses the order of the given array.</p>
  684. *
  685. * <p>This method does nothing if <code>null</code> array input.</p>
  686. *
  687. * @param array the array to reverse, may be <code>null</code>
  688. */
  689. public static void reverse(final char[] array) {
  690. if (array == null) {
  691. return;
  692. }
  693. int i = 0;
  694. int j = array.length - 1;
  695. char tmp;
  696. while (j > i) {
  697. tmp = array[j];
  698. array[j] = array[i];
  699. array[i] = tmp;
  700. j--;
  701. i++;
  702. }
  703. }
  704. /**
  705. * <p>Reverses the order of the given array.</p>
  706. *
  707. * <p>This method does nothing if <code>null</code> array input.</p>
  708. *
  709. * @param array the array to reverse, may be <code>null</code>
  710. */
  711. public static void reverse(final byte[] array) {
  712. if (array == null) {
  713. return;
  714. }
  715. int i = 0;
  716. int j = array.length - 1;
  717. byte tmp;
  718. while (j > i) {
  719. tmp = array[j];
  720. array[j] = array[i];
  721. array[i] = tmp;
  722. j--;
  723. i++;
  724. }
  725. }
  726. /**
  727. * <p>Reverses the order of the given array.</p>
  728. *
  729. * <p>This method does nothing if <code>null</code> array input.</p>
  730. *
  731. * @param array the array to reverse, may be <code>null</code>
  732. */
  733. public static void reverse(final double[] array) {
  734. if (array == null) {
  735. return;
  736. }
  737. int i = 0;
  738. int j = array.length - 1;
  739. double tmp;
  740. while (j > i) {
  741. tmp = array[j];
  742. array[j] = array[i];
  743. array[i] = tmp;
  744. j--;
  745. i++;
  746. }
  747. }
  748. /**
  749. * <p>Reverses the order of the given array.</p>
  750. *
  751. * <p>This method does nothing if <code>null</code> array input.</p>
  752. *
  753. * @param array the array to reverse, may be <code>null</code>
  754. */
  755. public static void reverse(final float[] array) {
  756. if (array == null) {
  757. return;
  758. }
  759. int i = 0;
  760. int j = array.length - 1;
  761. float tmp;
  762. while (j > i) {
  763. tmp = array[j];
  764. array[j] = array[i];
  765. array[i] = tmp;
  766. j--;
  767. i++;
  768. }
  769. }
  770. /**
  771. * <p>Reverses the order of the given array.</p>
  772. *
  773. * <p>This method does nothing if <code>null</code> array input.</p>
  774. *
  775. * @param array the array to reverse, may be <code>null</code>
  776. */
  777. public static void reverse(final boolean[] array) {
  778. if (array == null) {
  779. return;
  780. }
  781. int i = 0;
  782. int j = array.length - 1;
  783. boolean tmp;
  784. while (j > i) {
  785. tmp = array[j];
  786. array[j] = array[i];
  787. array[i] = tmp;
  788. j--;
  789. i++;
  790. }
  791. }
  792. // IndexOf search
  793. // ----------------------------------------------------------------------
  794. // Object IndexOf
  795. //-----------------------------------------------------------------------
  796. /**
  797. * <p>Find the index of the given object in the array.</p>
  798. *
  799. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  800. *
  801. * @param array the array to search through for the object, may be <code>null</code>
  802. * @param objectToFind the object to find, may be <code>null</code>
  803. * @return the index of the object within the array,
  804. * <code>-1</code> if not found or <code>null</code> array input
  805. */
  806. public static int indexOf(final Object[] array, final Object objectToFind) {
  807. return indexOf(array, objectToFind, 0);
  808. }
  809. /**
  810. * <p>Find the index of the given object in the array starting at the given index.</p>
  811. *
  812. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  813. *
  814. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  815. * length will return <code>-1</code>.</p>
  816. *
  817. * @param array the array to search through for the object, may be <code>null</code>
  818. * @param objectToFind the object to find, may be <code>null</code>
  819. * @param startIndex the index to start searching at
  820. * @return the index of the object within the array starting at the index,
  821. * <code>-1</code> if not found or <code>null</code> array input
  822. */
  823. public static int indexOf(final Object[] array, final Object objectToFind, int startIndex) {
  824. if (array == null) {
  825. return -1;
  826. }
  827. if (startIndex < 0) {
  828. startIndex = 0;
  829. }
  830. if (objectToFind == null) {
  831. for (int i = startIndex; i < array.length; i++) {
  832. if (array[i] == null) {
  833. return i;
  834. }
  835. }
  836. } else {
  837. for (int i = startIndex; i < array.length; i++) {
  838. if (objectToFind.equals(array[i])) {
  839. return i;
  840. }
  841. }
  842. }
  843. return -1;
  844. }
  845. /**
  846. * <p>Find the last index of the given object within the array.</p>
  847. *
  848. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  849. *
  850. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  851. * @param objectToFind the object to find, may be <code>null</code>
  852. * @return the last index of the object within the array,
  853. * <code>-1</code> if not found or <code>null</code> array input
  854. */
  855. public static int lastIndexOf(final Object[] array, final Object objectToFind) {
  856. return lastIndexOf(array, objectToFind, Integer.MAX_VALUE);
  857. }
  858. /**
  859. * <p>Find the last index of the given object in the array starting at the given index.</p>
  860. *
  861. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  862. *
  863. * <p>A negative startIndex will return <code>-1</code>. A startIndex larger than
  864. * the array length will search from the end of the array.</p>
  865. *
  866. * @param array the array to traverse for looking for the object, may be <code>null</code>
  867. * @param objectToFind the object to find, may be <code>null</code>
  868. * @param startIndex the start index to travers backwards from
  869. * @return the last index of the object within the array,
  870. * <code>-1</code> if not found or <code>null</code> array input
  871. */
  872. public static int lastIndexOf(final Object[] array, final Object objectToFind, int startIndex) {
  873. if (array == null) {
  874. return -1;
  875. }
  876. if (startIndex < 0) {
  877. return -1;
  878. } else if (startIndex >= array.length) {
  879. startIndex = array.length - 1;
  880. }
  881. if (objectToFind == null) {
  882. for (int i = startIndex; i >= 0; i--) {
  883. if (array[i] == null) {
  884. return i;
  885. }
  886. }
  887. } else {
  888. for (int i = startIndex; i >= 0; i--) {
  889. if (objectToFind.equals(array[i])) {
  890. return i;
  891. }
  892. }
  893. }
  894. return -1;
  895. }
  896. /**
  897. * <p>Checks if the object is in the given array.</p>
  898. *
  899. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  900. *
  901. * @param array the array to search through
  902. * @param objectToFind the object to find
  903. * @return <code>true</code> if the array contains the object
  904. */
  905. public static boolean contains(final Object[] array, final Object objectToFind) {
  906. return (indexOf(array, objectToFind) != -1);
  907. }
  908. // long IndexOf
  909. //-----------------------------------------------------------------------
  910. /**
  911. * <p>Find the index of the given value in the array.</p>
  912. *
  913. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  914. *
  915. * @param array the array to search through for the object, may be <code>null</code>
  916. * @param valueToFind the value to find
  917. * @return the index of the value within the array,
  918. * <code>-1</code> if not found or <code>null</code> array input
  919. */
  920. public static int indexOf(final long[] array, final long valueToFind) {
  921. return indexOf(array, valueToFind, 0);
  922. }
  923. /**
  924. * <p>Find the index of the given value in the array starting at the given index.</p>
  925. *
  926. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  927. *
  928. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  929. * length will return -1.</p>
  930. *
  931. * @param array the array to search through for the object, may be <code>null</code>
  932. * @param valueToFind the value to find
  933. * @param startIndex the index to start searching at
  934. * @return the index of the value within the array,
  935. * <code>-1</code> if not found or <code>null</code> array input
  936. */
  937. public static int indexOf(final long[] array, final long valueToFind, int startIndex) {
  938. if (array == null) {
  939. return -1;
  940. }
  941. if (startIndex < 0) {
  942. startIndex = 0;
  943. }
  944. for (int i = startIndex; i < array.length; i++) {
  945. if (valueToFind == array[i]) {
  946. return i;
  947. }
  948. }
  949. return -1;
  950. }
  951. /**
  952. * <p>Find the last index of the given value within the array.</p>
  953. *
  954. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  955. *
  956. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  957. * @param valueToFind the object to find
  958. * @return the last index of the value within the array,
  959. * <code>-1</code> if not found or <code>null</code> array input
  960. */
  961. public static int lastIndexOf(final long[] array, final long valueToFind) {
  962. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  963. }
  964. /**
  965. * <p>Find the last index of the given value in the array starting at the given index.</p>
  966. *
  967. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  968. *
  969. * <p>A negative startIndex will return -1. A startIndex larger than the array
  970. * length will search from the end of the array.</p>
  971. *
  972. * @param array the array to traverse for looking for the object, may be <code>null</code>
  973. * @param valueToFind the value to find
  974. * @param startIndex the start index to travers backwards from
  975. * @return the last index of the value within the array,
  976. * <code>-1</code> if not found or <code>null</code> array input
  977. */
  978. public static int lastIndexOf(final long[] array, final long valueToFind, int startIndex) {
  979. if (array == null) {
  980. return -1;
  981. }
  982. if (startIndex < 0) {
  983. return -1;
  984. } else if (startIndex >= array.length) {
  985. startIndex = array.length - 1;
  986. }
  987. for (int i = startIndex; i >= 0; i--) {
  988. if (valueToFind == array[i]) {
  989. return i;
  990. }
  991. }
  992. return -1;
  993. }
  994. /**
  995. * <p>Checks if the value is in the given array.</p>
  996. *
  997. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  998. *
  999. * @param array the array to search through
  1000. * @param valueToFind the value to find
  1001. * @return <code>true</code> if the array contains the object
  1002. */
  1003. public static boolean contains(final long[] array, final long valueToFind) {
  1004. return (indexOf(array, valueToFind) != -1);
  1005. }
  1006. // int IndexOf
  1007. //-----------------------------------------------------------------------
  1008. /**
  1009. * <p>Find the index of the given value in the array.</p>
  1010. *
  1011. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1012. *
  1013. * @param array the array to search through for the object, may be <code>null</code>
  1014. * @param valueToFind the value to find
  1015. * @return the index of the value within the array,
  1016. * <code>-1</code> if not found or <code>null</code> array input
  1017. */
  1018. public static int indexOf(final int[] array, final int valueToFind) {
  1019. return indexOf(array, valueToFind, 0);
  1020. }
  1021. /**
  1022. * <p>Find the index of the given value in the array starting at the given index.</p>
  1023. *
  1024. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1025. *
  1026. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1027. * length will return -1.</p>
  1028. *
  1029. * @param array the array to search through for the object, may be <code>null</code>
  1030. * @param valueToFind the value to find
  1031. * @param startIndex the index to start searching at
  1032. * @return the index of the value within the array,
  1033. * <code>-1</code> if not found or <code>null</code> array input
  1034. */
  1035. public static int indexOf(final int[] array, final int valueToFind, int startIndex) {
  1036. if (array == null) {
  1037. return -1;
  1038. }
  1039. if (startIndex < 0) {
  1040. startIndex = 0;
  1041. }
  1042. for (int i = startIndex; i < array.length; i++) {
  1043. if (valueToFind == array[i]) {
  1044. return i;
  1045. }
  1046. }
  1047. return -1;
  1048. }
  1049. /**
  1050. * <p>Find the last index of the given value within the array.</p>
  1051. *
  1052. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1053. *
  1054. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  1055. * @param valueToFind the object to find
  1056. * @return the last index of the value within the array,
  1057. * <code>-1</code> if not found or <code>null</code> array input
  1058. */
  1059. public static int lastIndexOf(final int[] array, final int valueToFind) {
  1060. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  1061. }
  1062. /**
  1063. * <p>Find the last index of the given value in the array starting at the given index.</p>
  1064. *
  1065. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1066. *
  1067. * <p>A negative startIndex will return -1. A startIndex larger than the array
  1068. * length will search from the end of the array.</p>
  1069. *
  1070. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1071. * @param valueToFind the value to find
  1072. * @param startIndex the start index to travers backwards from
  1073. * @return the last index of the value within the array,
  1074. * <code>-1</code> if not found or <code>null</code> array input
  1075. */
  1076. public static int lastIndexOf(final int[] array, final int valueToFind, int startIndex) {
  1077. if (array == null) {
  1078. return -1;
  1079. }
  1080. if (startIndex < 0) {
  1081. return -1;
  1082. } else if (startIndex >= array.length) {
  1083. startIndex = array.length - 1;
  1084. }
  1085. for (int i = startIndex; i >= 0; i--) {
  1086. if (valueToFind == array[i]) {
  1087. return i;
  1088. }
  1089. }
  1090. return -1;
  1091. }
  1092. /**
  1093. * <p>Checks if the value is in the given array.</p>
  1094. *
  1095. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  1096. *
  1097. * @param array the array to search through
  1098. * @param valueToFind the value to find
  1099. * @return <code>true</code> if the array contains the object
  1100. */
  1101. public static boolean contains(final int[] array, final int valueToFind) {
  1102. return (indexOf(array, valueToFind) != -1);
  1103. }
  1104. // short IndexOf
  1105. //-----------------------------------------------------------------------
  1106. /**
  1107. * <p>Find the index of the given value in the array.</p>
  1108. *
  1109. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1110. *
  1111. * @param array the array to search through for the object, may be <code>null</code>
  1112. * @param valueToFind the value to find
  1113. * @return the index of the value within the array,
  1114. * <code>-1</code> if not found or <code>null</code> array input
  1115. */
  1116. public static int indexOf(final short[] array, final short valueToFind) {
  1117. return indexOf(array, valueToFind, 0);
  1118. }
  1119. /**
  1120. * <p>Find the index of the given value in the array starting at the given index.</p>
  1121. *
  1122. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1123. *
  1124. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1125. * length will return -1.</p>
  1126. *
  1127. * @param array the array to search through for the object, may be <code>null</code>
  1128. * @param valueToFind the value to find
  1129. * @param startIndex the index to start searching at
  1130. * @return the index of the value within the array,
  1131. * <code>-1</code> if not found or <code>null</code> array input
  1132. */
  1133. public static int indexOf(final short[] array, final short valueToFind, int startIndex) {
  1134. if (array == null) {
  1135. return -1;
  1136. }
  1137. if (startIndex < 0) {
  1138. startIndex = 0;
  1139. }
  1140. for (int i = startIndex; i < array.length; i++) {
  1141. if (valueToFind == array[i]) {
  1142. return i;
  1143. }
  1144. }
  1145. return -1;
  1146. }
  1147. /**
  1148. * <p>Find the last index of the given value within the array.</p>
  1149. *
  1150. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1151. *
  1152. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  1153. * @param valueToFind the object to find
  1154. * @return the last index of the value within the array,
  1155. * <code>-1</code> if not found or <code>null</code> array input
  1156. */
  1157. public static int lastIndexOf(final short[] array, final short valueToFind) {
  1158. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  1159. }
  1160. /**
  1161. * <p>Find the last index of the given value in the array starting at the given index.</p>
  1162. *
  1163. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1164. *
  1165. * <p>A negative startIndex will return -1. A startIndex larger than the array
  1166. * length will search from the end of the array.</p>
  1167. *
  1168. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1169. * @param valueToFind the value to find
  1170. * @param startIndex the start index to travers backwards from
  1171. * @return the last index of the value within the array,
  1172. * <code>-1</code> if not found or <code>null</code> array input
  1173. */
  1174. public static int lastIndexOf(final short[] array, final short valueToFind, int startIndex) {
  1175. if (array == null) {
  1176. return -1;
  1177. }
  1178. if (startIndex < 0) {
  1179. return -1;
  1180. } else if (startIndex >= array.length) {
  1181. startIndex = array.length - 1;
  1182. }
  1183. for (int i = startIndex; i >= 0; i--) {
  1184. if (valueToFind == array[i]) {
  1185. return i;
  1186. }
  1187. }
  1188. return -1;
  1189. }
  1190. /**
  1191. * <p>Checks if the value is in the given array.</p>
  1192. *
  1193. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  1194. *
  1195. * @param array the array to search through
  1196. * @param valueToFind the value to find
  1197. * @return <code>true</code> if the array contains the object
  1198. */
  1199. public static boolean contains(final short[] array, final short valueToFind) {
  1200. return (indexOf(array, valueToFind) != -1);
  1201. }
  1202. // byte IndexOf
  1203. //-----------------------------------------------------------------------
  1204. /**
  1205. * <p>Find the index of the given value in the array.</p>
  1206. *
  1207. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1208. *
  1209. * @param array the array to search through for the object, may be <code>null</code>
  1210. * @param valueToFind the value to find
  1211. * @return the index of the value within the array,
  1212. * <code>-1</code> if not found or <code>null</code> array input
  1213. */
  1214. public static int indexOf(final byte[] array, final byte valueToFind) {
  1215. return indexOf(array, valueToFind, 0);
  1216. }
  1217. /**
  1218. * <p>Find the index of the given value in the array starting at the given index.</p>
  1219. *
  1220. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1221. *
  1222. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1223. * length will return -1.</p>
  1224. *
  1225. * @param array the array to search through for the object, may be <code>null</code>
  1226. * @param valueToFind the value to find
  1227. * @param startIndex the index to start searching at
  1228. * @return the index of the value within the array,
  1229. * <code>-1</code> if not found or <code>null</code> array input
  1230. */
  1231. public static int indexOf(final byte[] array, final byte valueToFind, int startIndex) {
  1232. if (array == null) {
  1233. return -1;
  1234. }
  1235. if (startIndex < 0) {
  1236. startIndex = 0;
  1237. }
  1238. for (int i = startIndex; i < array.length; i++) {
  1239. if (valueToFind == array[i]) {
  1240. return i;
  1241. }
  1242. }
  1243. return -1;
  1244. }
  1245. /**
  1246. * <p>Find the last index of the given value within the array.</p>
  1247. *
  1248. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1249. *
  1250. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  1251. * @param valueToFind the object to find
  1252. * @return the last index of the value within the array,
  1253. * <code>-1</code> if not found or <code>null</code> array input
  1254. */
  1255. public static int lastIndexOf(final byte[] array, final byte valueToFind) {
  1256. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  1257. }
  1258. /**
  1259. * <p>Find the last index of the given value in the array starting at the given index.</p>
  1260. *
  1261. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1262. *
  1263. * <p>A negative startIndex will return -1. A startIndex larger than the array
  1264. * length will search from the end of the array.</p>
  1265. *
  1266. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1267. * @param valueToFind the value to find
  1268. * @param startIndex the start index to travers backwards from
  1269. * @return the last index of the value within the array,
  1270. * <code>-1</code> if not found or <code>null</code> array input
  1271. */
  1272. public static int lastIndexOf(final byte[] array, final byte valueToFind, int startIndex) {
  1273. if (array == null) {
  1274. return -1;
  1275. }
  1276. if (startIndex < 0) {
  1277. return -1;
  1278. } else if (startIndex >= array.length) {
  1279. startIndex = array.length - 1;
  1280. }
  1281. for (int i = startIndex; i >= 0; i--) {
  1282. if (valueToFind == array[i]) {
  1283. return i;
  1284. }
  1285. }
  1286. return -1;
  1287. }
  1288. /**
  1289. * <p>Checks if the value is in the given array.</p>
  1290. *
  1291. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  1292. *
  1293. * @param array the array to search through
  1294. * @param valueToFind the value to find
  1295. * @return <code>true</code> if the array contains the object
  1296. */
  1297. public static boolean contains(final byte[] array, final byte valueToFind) {
  1298. return (indexOf(array, valueToFind) != -1);
  1299. }
  1300. // double IndexOf
  1301. //-----------------------------------------------------------------------
  1302. /**
  1303. * <p>Find the index of the given value in the array.</p>
  1304. *
  1305. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1306. *
  1307. * @param array the array to search through for the object, may be <code>null</code>
  1308. * @param valueToFind the value to find
  1309. * @return the index of the value within the array,
  1310. * <code>-1</code> if not found or <code>null</code> array input
  1311. */
  1312. public static int indexOf(final double[] array, final double valueToFind) {
  1313. return indexOf(array, valueToFind, 0);
  1314. }
  1315. /**
  1316. * <p>Find the index of the given value within a given tolerance in the array.
  1317. * This method will return the index of the first value which falls between the region
  1318. * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
  1319. *
  1320. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1321. *
  1322. * @param array the array to search through for the object, may be <code>null</code>
  1323. * @param valueToFind the value to find
  1324. * @param tolerance tolerance of the search
  1325. * @return the index of the value within the array,
  1326. * <code>-1</code> if not found or <code>null</code> array input
  1327. */
  1328. public static int indexOf(final double[] array, final double valueToFind, final double tolerance) {
  1329. return indexOf(array, valueToFind, 0, tolerance);
  1330. }
  1331. /**
  1332. * <p>Find the index of the given value in the array starting at the given index.</p>
  1333. *
  1334. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1335. *
  1336. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1337. * length will return -1.</p>
  1338. *
  1339. * @param array the array to search through for the object, may be <code>null</code>
  1340. * @param valueToFind the value to find
  1341. * @param startIndex the index to start searching at
  1342. * @return the index of the value within the array,
  1343. * <code>-1</code> if not found or <code>null</code> array input
  1344. */
  1345. public static int indexOf(final double[] array, final double valueToFind, int startIndex) {
  1346. if (array == null || array.length == 0) {
  1347. return -1;
  1348. }
  1349. if (startIndex < 0) {
  1350. startIndex = 0;
  1351. }
  1352. for (int i = startIndex; i < array.length; i++) {
  1353. if (valueToFind == array[i]) {
  1354. return i;
  1355. }
  1356. }
  1357. return -1;
  1358. }
  1359. /**
  1360. * <p>Find the index of the given value in the array starting at the given index.
  1361. * This method will return the index of the first value which falls between the region
  1362. * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
  1363. *
  1364. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1365. *
  1366. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1367. * length will return -1.</p>
  1368. *
  1369. * @param array the array to search through for the object, may be <code>null</code>
  1370. * @param valueToFind the value to find
  1371. * @param startIndex the index to start searching at
  1372. * @param tolerance tolerance of the search
  1373. * @return the index of the value within the array,
  1374. * <code>-1</code> if not found or <code>null</code> array input
  1375. */
  1376. public static int indexOf(final double[] array, final double valueToFind, int startIndex, double tolerance) {
  1377. if (array == null || array.length == 0) {
  1378. return -1;
  1379. }
  1380. if (startIndex < 0) {
  1381. startIndex = 0;
  1382. }
  1383. double min = valueToFind - tolerance;
  1384. double max = valueToFind + tolerance;
  1385. for (int i = startIndex; i < array.length; i++) {
  1386. if (array[i] >= min && array[i] <= max) {
  1387. return i;
  1388. }
  1389. }
  1390. return -1;
  1391. }
  1392. /**
  1393. * <p>Find the last index of the given value within the array.</p>
  1394. *
  1395. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1396. *
  1397. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  1398. * @param valueToFind the object to find
  1399. * @return the last index of the value within the array,
  1400. * <code>-1</code> if not found or <code>null</code> array input
  1401. */
  1402. public static int lastIndexOf(final double[] array, final double valueToFind) {
  1403. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  1404. }
  1405. /**
  1406. * <p>Find the last index of the given value within a given tolerance in the array.
  1407. * This method will return the index of the last value which falls between the region
  1408. * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
  1409. *
  1410. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1411. *
  1412. * @param array the array to search through for the object, may be <code>null</code>
  1413. * @param valueToFind the value to find
  1414. * @param tolerance tolerance of the search
  1415. * @return the index of the value within the array,
  1416. * <code>-1</code> if not found or <code>null</code> array input
  1417. */
  1418. public static int lastIndexOf(final double[] array, final double valueToFind, final double tolerance) {
  1419. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE, tolerance);
  1420. }
  1421. /**
  1422. * <p>Find the last index of the given value in the array starting at the given index.</p>
  1423. *
  1424. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1425. *
  1426. * <p>A negative startIndex will return -1. A startIndex larger than the array
  1427. * length will search from the end of the array.</p>
  1428. *
  1429. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1430. * @param valueToFind the value to find
  1431. * @param startIndex the start index to travers backwards from
  1432. * @return the last index of the value within the array,
  1433. * <code>-1</code> if not found or <code>null</code> array input
  1434. */
  1435. public static int lastIndexOf(final double[] array, final double valueToFind, int startIndex) {
  1436. if (array == null || array.length == 0) {
  1437. return -1;
  1438. }
  1439. if (startIndex < 0) {
  1440. return -1;
  1441. } else if (startIndex >= array.length) {
  1442. startIndex = array.length - 1;
  1443. }
  1444. for (int i = startIndex; i >= 0; i--) {
  1445. if (valueToFind == array[i]) {
  1446. return i;
  1447. }
  1448. }
  1449. return -1;
  1450. }
  1451. /**
  1452. * <p>Find the last index of the given value in the array starting at the given index.
  1453. * This method will return the index of the last value which falls between the region
  1454. * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
  1455. *
  1456. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1457. *
  1458. * <p>A negative startIndex will return -1. A startIndex larger than the array
  1459. * length will search from the end of the array.</p>
  1460. *
  1461. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1462. * @param valueToFind the value to find
  1463. * @param startIndex the start index to travers backwards from
  1464. * @param tolerance search for value within plus/minus this amount
  1465. * @return the last index of the value within the array,
  1466. * <code>-1</code> if not found or <code>null</code> array input
  1467. */
  1468. public static int lastIndexOf(final double[] array, final double valueToFind, int startIndex, double tolerance) {
  1469. if (array == null || array.length == 0) {
  1470. return -1;
  1471. }
  1472. if (startIndex < 0) {
  1473. return -1;
  1474. } else if (startIndex >= array.length) {
  1475. startIndex = array.length - 1;
  1476. }
  1477. double min = valueToFind - tolerance;
  1478. double max = valueToFind + tolerance;
  1479. for (int i = startIndex; i >= 0; i--) {
  1480. if (array[i] >= min && array[i] <= max) {
  1481. return i;
  1482. }
  1483. }
  1484. return -1;
  1485. }
  1486. /**
  1487. * <p>Checks if the value is in the given array.</p>
  1488. *
  1489. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  1490. *
  1491. * @param array the array to search through
  1492. * @param valueToFind the value to find
  1493. * @return <code>true</code> if the array contains the object
  1494. */
  1495. public static boolean contains(final double[] array, final double valueToFind) {
  1496. return (indexOf(array, valueToFind) != -1);
  1497. }
  1498. /**
  1499. * <p>Checks if a value falling within the given tolerance is in the
  1500. * given array. If the array contains a value within the inclusive range
  1501. * defined by (value - tolerance) to (value + tolerance).</p>
  1502. *
  1503. * <p>The method returns <code>false</code> if a <code>null</code> array
  1504. * is passed in.</p>
  1505. *
  1506. * @param array the array to search
  1507. * @param valueToFind the value to find
  1508. * @param tolerance the array contains the tolerance of the search
  1509. * @return true if value falling within tolerance is in array
  1510. */
  1511. public static boolean contains(final double[] array, final double valueToFind, final double tolerance) {
  1512. return (indexOf(array, valueToFind, 0, tolerance) != -1);
  1513. }
  1514. // float IndexOf
  1515. //-----------------------------------------------------------------------
  1516. /**
  1517. * <p>Find the index of the given value in the array.</p>
  1518. *
  1519. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1520. *
  1521. * @param array the array to search through for the object, may be <code>null</code>
  1522. * @param valueToFind the value to find
  1523. * @return the index of the value within the array,
  1524. * <code>-1</code> if not found or <code>null</code> array input
  1525. */
  1526. public static int indexOf(final float[] array, final float valueToFind) {
  1527. return indexOf(array, valueToFind, 0);
  1528. }
  1529. /**
  1530. * <p>Find the index of the given value in the array starting at the given index.</p>
  1531. *
  1532. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1533. *
  1534. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1535. * length will return -1.</p>
  1536. *
  1537. * @param array the array to search through for the object, may be <code>null</code>
  1538. * @param valueToFind the value to find
  1539. * @param startIndex the index to start searching at
  1540. * @return the index of the value within the array,
  1541. * <code>-1</code> if not found or <code>null</code> array input
  1542. */
  1543. public static int indexOf(final float[] array, final float valueToFind, int startIndex) {
  1544. if (array == null || array.length == 0) {
  1545. return -1;
  1546. }
  1547. if (startIndex < 0) {
  1548. startIndex = 0;
  1549. }
  1550. for (int i = startIndex; i < array.length; i++) {
  1551. if (valueToFind == array[i]) {
  1552. return i;
  1553. }
  1554. }
  1555. return -1;
  1556. }
  1557. /**
  1558. * <p>Find the last index of the given value within the array.</p>
  1559. *
  1560. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1561. *
  1562. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  1563. * @param valueToFind the object to find
  1564. * @return the last index of the value within the array,
  1565. * <code>-1</code> if not found or <code>null</code> array input
  1566. */
  1567. public static int lastIndexOf(final float[] array, final float valueToFind) {
  1568. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  1569. }
  1570. /**
  1571. * <p>Find the last index of the given value in the array starting at the given index.</p>
  1572. *
  1573. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1574. *
  1575. * <p>A negative startIndex will return -1. A startIndex larger than the array
  1576. * length will search from the end of the array.</p>
  1577. *
  1578. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1579. * @param valueToFind the value to find
  1580. * @param startIndex the start index to travers backwards from
  1581. * @return the last index of the value within the array,
  1582. * <code>-1</code> if not found or <code>null</code> array input
  1583. */
  1584. public static int lastIndexOf(final float[] array, final float valueToFind, int startIndex) {
  1585. if (array == null || array.length == 0) {
  1586. return -1;
  1587. }
  1588. if (startIndex < 0) {
  1589. return -1;
  1590. } else if (startIndex >= array.length) {
  1591. startIndex = array.length - 1;
  1592. }
  1593. for (int i = startIndex; i >= 0; i--) {
  1594. if (valueToFind == array[i]) {
  1595. return i;
  1596. }
  1597. }
  1598. return -1;
  1599. }
  1600. /**
  1601. * <p>Checks if the value is in the given array.</p>
  1602. *
  1603. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  1604. *
  1605. * @param array the array to search through
  1606. * @param valueToFind the value to find
  1607. * @return <code>true</code> if the array contains the object
  1608. */
  1609. public static boolean contains(final float[] array, final float valueToFind) {
  1610. return (indexOf(array, valueToFind) != -1);
  1611. }
  1612. // boolean IndexOf
  1613. //-----------------------------------------------------------------------
  1614. /**
  1615. * <p>Find the index of the given value in the array.</p>
  1616. *
  1617. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1618. *
  1619. * @param array the array to search through for the object, may be <code>null</code>
  1620. * @param valueToFind the value to find
  1621. * @return the index of the value within the array,
  1622. * <code>-1</code> if not found or <code>null</code> array input
  1623. */
  1624. public static int indexOf(final boolean[] array, final boolean valueToFind) {
  1625. return indexOf(array, valueToFind, 0);
  1626. }
  1627. /**
  1628. * <p>Find the index of the given value in the array starting at the given index.</p>
  1629. *
  1630. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1631. *
  1632. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1633. * length will return -1.</p>
  1634. *
  1635. * @param array the array to search through for the object, may be <code>null</code>
  1636. * @param valueToFind the value to find
  1637. * @param startIndex the index to start searching at
  1638. * @return the index of the value within the array,
  1639. * <code>-1</code> if not found or <code>null</code> array input
  1640. */
  1641. public static int indexOf(final boolean[] array, final boolean valueToFind, int startIndex) {
  1642. if (array == null || array.length == 0) {
  1643. return -1;
  1644. }
  1645. if (startIndex < 0) {
  1646. startIndex = 0;
  1647. }
  1648. for (int i = startIndex; i < array.length; i++) {
  1649. if (valueToFind == array[i]) {
  1650. return i;
  1651. }
  1652. }
  1653. return -1;
  1654. }
  1655. /**
  1656. * <p>Find the last index of the given value within the array.</p>
  1657. *
  1658. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1659. *
  1660. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  1661. * @param valueToFind the object to find
  1662. * @return the last index of the value within the array,
  1663. * <code>-1</code> if not found or <code>null</code> array input
  1664. */
  1665. public static int lastIndexOf(final boolean[] array, final boolean valueToFind) {
  1666. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  1667. }
  1668. /**
  1669. * <p>Find the last index of the given value in the array starting at the given index.</p>
  1670. *
  1671. * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
  1672. *
  1673. * <p>A negative startIndex will return -1. A startIndex larger than the array
  1674. * length will search from the end of the array.</p>
  1675. *
  1676. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1677. * @param valueToFind the value to find
  1678. * @param startIndex the start index to travers backwards from
  1679. * @return the last index of the value within the array,
  1680. * <code>-1</code> if not found or <code>null</code> array input
  1681. */
  1682. public static int lastIndexOf(final boolean[] array, final boolean valueToFind, int startIndex) {
  1683. if (array == null || array.length == 0) {
  1684. return -1;
  1685. }
  1686. if (startIndex < 0) {
  1687. return -1;
  1688. } else if (startIndex >= array.length) {
  1689. startIndex = array.length - 1;
  1690. }
  1691. for (int i = startIndex; i >= 0; i--) {
  1692. if (valueToFind == array[i]) {
  1693. return i;
  1694. }
  1695. }
  1696. return -1;
  1697. }
  1698. /**
  1699. * <p>Checks if the value is in the given array.</p>
  1700. *
  1701. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  1702. *
  1703. * @param array the array to search through
  1704. * @param valueToFind the value to find
  1705. * @return <code>true</code> if the array contains the object
  1706. */
  1707. public static boolean contains(final boolean[] array, final boolean valueToFind) {
  1708. return (indexOf(array, valueToFind) != -1);
  1709. }
  1710. // Primitive/Object array converters
  1711. // ----------------------------------------------------------------------
  1712. // Long array converters
  1713. // ----------------------------------------------------------------------
  1714. /**
  1715. * <p>Converts an array of object Longs to primitives.</p>
  1716. *
  1717. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  1718. *
  1719. * @param array a <code>Long</code> array, may be <code>null</code>
  1720. * @return a <code>long</code> array, <code>null</code> if null array input
  1721. * @throws NullPointerException if array content is <code>null</code>
  1722. */
  1723. public static long[] toPrimitive(final Long[] array) {
  1724. if (array == null) {
  1725. return null;
  1726. } else if (array.length == 0) {
  1727. return EMPTY_LONG_ARRAY;
  1728. }
  1729. final long[] result = new long[array.length];
  1730. for (int i = 0; i < array.length; i++) {
  1731. result[i] = array[i].longValue();
  1732. }
  1733. return result;
  1734. }
  1735. /**
  1736. * <p>Converts an array of object Long to primitives handling <code>null</code>.</p>
  1737. *
  1738. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  1739. *
  1740. * @param array a <code>Long</code> array, may be <code>null</code>
  1741. * @param valueForNull the value to insert if <code>null</code> found
  1742. * @return a <code>long</code> array, <code>null</code> if null array input
  1743. */
  1744. public static long[] toPrimitive(final Long[] array, final long valueForNull) {
  1745. if (array == null) {
  1746. return null;
  1747. } else if (array.length == 0) {
  1748. return EMPTY_LONG_ARRAY;
  1749. }
  1750. final long[] result = new long[array.length];
  1751. for (int i = 0; i < array.length; i++) {
  1752. Long b = array[i];
  1753. result[i] = (b == null ? valueForNull : b.longValue());
  1754. }
  1755. return result;
  1756. }
  1757. /**
  1758. * <p>Converts an array of primitive longs to objects.</p>
  1759. *
  1760. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  1761. *
  1762. * @param array a <code>long</code> array
  1763. * @return a <code>Long</code> array, <code>null</code> if null array input
  1764. */
  1765. public static Long[] toObject(final long[] array) {
  1766. if (array == null) {
  1767. return null;
  1768. } else if (array.length == 0) {
  1769. return EMPTY_LONG_OBJECT_ARRAY;
  1770. }
  1771. final Long[] result = new Long[array.length];
  1772. for (int i = 0; i < array.length; i++) {
  1773. result[i] = new Long(array[i]);
  1774. }
  1775. return result;
  1776. }
  1777. // Int array converters
  1778. // ----------------------------------------------------------------------
  1779. /**
  1780. * <p>Converts an array of object Integers to primitives.</p>
  1781. *
  1782. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  1783. *
  1784. * @param array a <code>Integer</code> array, may be <code>null</code>
  1785. * @return an <code>int</code> array, <code>null</code> if null array input
  1786. * @throws NullPointerException if array content is <code>null</code>
  1787. */
  1788. public static int[] toPrimitive(final Integer[] array) {
  1789. if (array == null) {
  1790. return null;
  1791. } else if (array.length == 0) {
  1792. return EMPTY_INT_ARRAY;
  1793. }
  1794. final int[] result = new int[array.length];
  1795. for (int i = 0; i < array.length; i++) {
  1796. result[i] = array[i].intValue();
  1797. }
  1798. return result;
  1799. }
  1800. /**
  1801. * <p>Converts an array of object Integer to primitives handling <code>null</code>.</p>
  1802. *
  1803. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  1804. *
  1805. * @param array a <code>Integer</code> array, may be <code>null</code>
  1806. * @param valueForNull the value to insert if <code>null</code> found
  1807. * @return an <code>int</code> array, <code>null</code> if null array input
  1808. */
  1809. public static int[] toPrimitive(final Integer[] array, final int valueForNull) {
  1810. if (array == null) {
  1811. return null;
  1812. } else if (array.length == 0) {
  1813. return EMPTY_INT_ARRAY;
  1814. }
  1815. final int[] result = new int[array.length];
  1816. for (int i = 0; i < array.length; i++) {
  1817. Integer b = array[i];
  1818. result[i] = (b == null ? valueForNull : b.intValue());
  1819. }
  1820. return result;
  1821. }
  1822. /**
  1823. * <p>Converts an array of primitive ints to objects.</p>
  1824. *
  1825. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  1826. *
  1827. * @param array an <code>int</code> array
  1828. * @return an <code>Integer</code> array, <code>null</code> if null array input
  1829. */
  1830. public static Integer[] toObject(final int[] array) {
  1831. if (array == null) {
  1832. return null;
  1833. } else if (array.length == 0) {
  1834. return EMPTY_INTEGER_OBJECT_ARRAY;
  1835. }
  1836. final Integer[] result = new Integer[array.length];
  1837. for (int i = 0; i < array.length; i++) {
  1838. result[i] = new Integer(array[i]);
  1839. }
  1840. return result;
  1841. }
  1842. // Short array converters
  1843. // ----------------------------------------------------------------------
  1844. /**
  1845. * <p>Converts an array of object Shorts to primitives.</p>
  1846. *
  1847. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  1848. *
  1849. * @param array a <code>Short</code> array, may be <code>null</code>
  1850. * @return a <code>byte</code> array, <code>null</code> if null array input
  1851. * @throws NullPointerException if array content is <code>null</code>
  1852. */
  1853. public static short[] toPrimitive(final Short[] array) {
  1854. if (array == null) {
  1855. return null;
  1856. } else if (array.length == 0) {
  1857. return EMPTY_SHORT_ARRAY;
  1858. }
  1859. final short[] result = new short[array.length];
  1860. for (int i = 0; i < array.length; i++) {
  1861. result[i] = array[i].shortValue();
  1862. }
  1863. return result;
  1864. }
  1865. /**
  1866. * <p>Converts an array of object Short to primitives handling <code>null</code>.</p>
  1867. *
  1868. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  1869. *
  1870. * @param array a <code>Short</code> array, may be <code>null</code>
  1871. * @param valueForNull the value to insert if <code>null</code> found
  1872. * @return a <code>byte</code> array, <code>null</code> if null array input
  1873. */
  1874. public static short[] toPrimitive(final Short[] array, final short valueForNull) {
  1875. if (array == null) {
  1876. return null;
  1877. } else if (array.length == 0) {
  1878. return EMPTY_SHORT_ARRAY;
  1879. }
  1880. final short[] result = new short[array.length];
  1881. for (int i = 0; i < array.length; i++) {
  1882. Short b = array[i];
  1883. result[i] = (b == null ? valueForNull : b.shortValue());
  1884. }
  1885. return result;
  1886. }
  1887. /**
  1888. * <p>Converts an array of primitive shorts to objects.</p>
  1889. *
  1890. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  1891. *
  1892. * @param array a <code>short</code> array
  1893. * @return a <code>Short</code> array, <code>null</code> if null array input
  1894. */
  1895. public static Short[] toObject(final short[] array) {
  1896. if (array == null) {
  1897. return null;
  1898. } else if (array.length == 0) {
  1899. return EMPTY_SHORT_OBJECT_ARRAY;
  1900. }
  1901. final Short[] result = new Short[array.length];
  1902. for (int i = 0; i < array.length; i++) {
  1903. result[i] = new Short(array[i]);
  1904. }
  1905. return result;
  1906. }
  1907. // Byte array converters
  1908. // ----------------------------------------------------------------------
  1909. /**
  1910. * <p>Converts an array of object Bytes to primitives.</p>
  1911. *
  1912. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  1913. *
  1914. * @param array a <code>Byte</code> array, may be <code>null</code>
  1915. * @return a <code>byte</code> array, <code>null</code> if null array input
  1916. * @throws NullPointerException if array content is <code>null</code>
  1917. */
  1918. public static byte[] toPrimitive(final Byte[] array) {
  1919. if (array == null) {
  1920. return null;
  1921. } else if (array.length == 0) {
  1922. return EMPTY_BYTE_ARRAY;
  1923. }
  1924. final byte[] result = new byte[array.length];
  1925. for (int i = 0; i < array.length; i++) {
  1926. result[i] = array[i].byteValue();
  1927. }
  1928. return result;
  1929. }
  1930. /**
  1931. * <p>Converts an array of object Bytes to primitives handling <code>null</code>.</p>
  1932. *
  1933. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  1934. *
  1935. * @param array a <code>Byte</code> array, may be <code>null</code>
  1936. * @param valueForNull the value to insert if <code>null</code> found
  1937. * @return a <code>byte</code> array, <code>null</code> if null array input
  1938. */
  1939. public static byte[] toPrimitive(final Byte[] array, final byte valueForNull) {
  1940. if (array == null) {
  1941. return null;
  1942. } else if (array.length == 0) {
  1943. return EMPTY_BYTE_ARRAY;
  1944. }
  1945. final byte[] result = new byte[array.length];
  1946. for (int i = 0; i < array.length; i++) {
  1947. Byte b = array[i];
  1948. result[i] = (b == null ? valueForNull : b.byteValue());
  1949. }
  1950. return result;
  1951. }
  1952. /**
  1953. * <p>Converts an array of primitive bytes to objects.</p>
  1954. *
  1955. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  1956. *
  1957. * @param array a <code>byte</code> array
  1958. * @return a <code>Byte</code> array, <code>null</code> if null array input
  1959. */
  1960. public static Byte[] toObject(final byte[] array) {
  1961. if (array == null) {
  1962. return null;
  1963. } else if (array.length == 0) {
  1964. return EMPTY_BYTE_OBJECT_ARRAY;
  1965. }
  1966. final Byte[] result = new Byte[array.length];
  1967. for (int i = 0; i < array.length; i++) {
  1968. result[i] = new Byte(array[i]);
  1969. }
  1970. return result;
  1971. }
  1972. // Double array converters
  1973. // ----------------------------------------------------------------------
  1974. /**
  1975. * <p>Converts an array of object Doubles to primitives.</p>
  1976. *
  1977. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  1978. *
  1979. * @param array a <code>Double</code> array, may be <code>null</code>
  1980. * @return a <code>double</code> array, <code>null</code> if null array input
  1981. * @throws NullPointerException if array content is <code>null</code>
  1982. */
  1983. public static double[] toPrimitive(final Double[] array) {
  1984. if (array == null) {
  1985. return null;
  1986. } else if (array.length == 0) {
  1987. return EMPTY_DOUBLE_ARRAY;
  1988. }
  1989. final double[] result = new double[array.length];
  1990. for (int i = 0; i < array.length; i++) {
  1991. result[i] = array[i].doubleValue();
  1992. }
  1993. return result;
  1994. }
  1995. /**
  1996. * <p>Converts an array of object Doubles to primitives handling <code>null</code>.</p>
  1997. *
  1998. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  1999. *
  2000. * @param array a <code>Double</code> array, may be <code>null</code>
  2001. * @param valueForNull the value to insert if <code>null</code> found
  2002. * @return a <code>double</code> array, <code>null</code> if null array input
  2003. */
  2004. public static double[] toPrimitive(final Double[] array, final double valueForNull) {
  2005. if (array == null) {
  2006. return null;
  2007. } else if (array.length == 0) {
  2008. return EMPTY_DOUBLE_ARRAY;
  2009. }
  2010. final double[] result = new double[array.length];
  2011. for (int i = 0; i < array.length; i++) {
  2012. Double b = array[i];
  2013. result[i] = (b == null ? valueForNull : b.doubleValue());
  2014. }
  2015. return result;
  2016. }
  2017. /**
  2018. * <p>Converts an array of primitive doubles to objects.</p>
  2019. *
  2020. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  2021. *
  2022. * @param array a <code>double</code> array
  2023. * @return a <code>Double</code> array, <code>null</code> if null array input
  2024. */
  2025. public static Double[] toObject(final double[] array) {
  2026. if (array == null) {
  2027. return null;
  2028. } else if (array.length == 0) {
  2029. return EMPTY_DOUBLE_OBJECT_ARRAY;
  2030. }
  2031. final Double[] result = new Double[array.length];
  2032. for (int i = 0; i < array.length; i++) {
  2033. result[i] = new Double(array[i]);
  2034. }
  2035. return result;
  2036. }
  2037. // Float array converters
  2038. // ----------------------------------------------------------------------
  2039. /**
  2040. * <p>Converts an array of object Floats to primitives.</p>
  2041. *
  2042. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  2043. *
  2044. * @param array a <code>Float</code> array, may be <code>null</code>
  2045. * @return a <code>float</code> array, <code>null</code> if null array input
  2046. * @throws NullPointerException if array content is <code>null</code>
  2047. */
  2048. public static float[] toPrimitive(final Float[] array) {
  2049. if (array == null) {
  2050. return null;
  2051. } else if (array.length == 0) {
  2052. return EMPTY_FLOAT_ARRAY;
  2053. }
  2054. final float[] result = new float[array.length];
  2055. for (int i = 0; i < array.length; i++) {
  2056. result[i] = array[i].floatValue();
  2057. }
  2058. return result;
  2059. }
  2060. /**
  2061. * <p>Converts an array of object Floats to primitives handling <code>null</code>.</p>
  2062. *
  2063. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  2064. *
  2065. * @param array a <code>Float</code> array, may be <code>null</code>
  2066. * @param valueForNull the value to insert if <code>null</code> found
  2067. * @return a <code>float</code> array, <code>null</code> if null array input
  2068. */
  2069. public static float[] toPrimitive(final Float[] array, final float valueForNull) {
  2070. if (array == null) {
  2071. return null;
  2072. } else if (array.length == 0) {
  2073. return EMPTY_FLOAT_ARRAY;
  2074. }
  2075. final float[] result = new float[array.length];
  2076. for (int i = 0; i < array.length; i++) {
  2077. Float b = array[i];
  2078. result[i] = (b == null ? valueForNull : b.floatValue());
  2079. }
  2080. return result;
  2081. }
  2082. /**
  2083. * <p>Converts an array of primitive floats to objects.</p>
  2084. *
  2085. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  2086. *
  2087. * @param array a <code>float</code> array
  2088. * @return a <code>Float</code> array, <code>null</code> if null array input
  2089. */
  2090. public static Float[] toObject(final float[] array) {
  2091. if (array == null) {
  2092. return null;
  2093. } else if (array.length == 0) {
  2094. return EMPTY_FLOAT_OBJECT_ARRAY;
  2095. }
  2096. final Float[] result = new Float[array.length];
  2097. for (int i = 0; i < array.length; i++) {
  2098. result[i] = new Float(array[i]);
  2099. }
  2100. return result;
  2101. }
  2102. // Boolean array converters
  2103. // ----------------------------------------------------------------------
  2104. /**
  2105. * <p>Converts an array of object Booleans to primitives.</p>
  2106. *
  2107. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  2108. *
  2109. * @param array a <code>Boolean</code> array, may be <code>null</code>
  2110. * @return a <code>boolean</code> array, <code>null</code> if null array input
  2111. * @throws NullPointerException if array content is <code>null</code>
  2112. */
  2113. public static boolean[] toPrimitive(final Boolean[] array) {
  2114. if (array == null) {
  2115. return null;
  2116. } else if (array.length == 0) {
  2117. return EMPTY_BOOLEAN_ARRAY;
  2118. }
  2119. final boolean[] result = new boolean[array.length];
  2120. for (int i = 0; i < array.length; i++) {
  2121. result[i] = array[i].booleanValue();
  2122. }
  2123. return result;
  2124. }
  2125. /**
  2126. * <p>Converts an array of object Booleans to primitives handling <code>null</code>.</p>
  2127. *
  2128. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  2129. *
  2130. * @param array a <code>Boolean</code> array, may be <code>null</code>
  2131. * @param valueForNull the value to insert if <code>null</code> found
  2132. * @return a <code>boolean</code> array, <code>null</code> if null array input
  2133. */
  2134. public static boolean[] toPrimitive(final Boolean[] array, final boolean valueForNull) {
  2135. if (array == null) {
  2136. return null;
  2137. } else if (array.length == 0) {
  2138. return EMPTY_BOOLEAN_ARRAY;
  2139. }
  2140. final boolean[] result = new boolean[array.length];
  2141. for (int i = 0; i < array.length; i++) {
  2142. Boolean b = array[i];
  2143. result[i] = (b == null ? valueForNull : b.booleanValue());
  2144. }
  2145. return result;
  2146. }
  2147. /**
  2148. * <p>Converts an array of primitive booleans to objects.</p>
  2149. *
  2150. * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
  2151. *
  2152. * @param array a <code>boolean</code> array
  2153. * @return a <code>Boolean</code> array, <code>null</code> if null array input
  2154. */
  2155. public static Boolean[] toObject(final boolean[] array) {
  2156. if (array == null) {
  2157. return null;
  2158. } else if (array.length == 0) {
  2159. return EMPTY_BOOLEAN_OBJECT_ARRAY;
  2160. }
  2161. final Boolean[] result = new Boolean[array.length];
  2162. for (int i = 0; i < array.length; i++) {
  2163. result[i] = (array[i] ? Boolean.TRUE : Boolean.FALSE);
  2164. }
  2165. return result;
  2166. }
  2167. }