| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ArrayUtils |
|
| 2.8333333333333335;2.833 |
| 1 | package org.jtheque.utils.collections; | |
| 2 | ||
| 3 | import java.util.Arrays; | |
| 4 | ||
| 5 | /* | |
| 6 | * This file is part of JTheque. | |
| 7 | * | |
| 8 | * JTheque is free software: you can redistribute it and/or modify | |
| 9 | * it under the terms of the GNU General Public License as published by | |
| 10 | * the Free Software Foundation, either version 3 of the License. | |
| 11 | * | |
| 12 | * JTheque is distributed in the hope that it will be useful, | |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | * GNU General Public License for more details. | |
| 16 | * | |
| 17 | * You should have received a copy of the GNU General Public License | |
| 18 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
| 19 | */ | |
| 20 | ||
| 21 | /** | |
| 22 | * This class provide utility methods to the use of arrays. | |
| 23 | * | |
| 24 | * @author Baptiste Wicht | |
| 25 | */ | |
| 26 | public final class ArrayUtils { | |
| 27 | 2 | public static final Object[] ZERO_LENGTH_ARRAY = new Object[0]; |
| 28 | ||
| 29 | /** | |
| 30 | * Construct a new <code>ArrayUtils</code>. The constructor is private, all methods are static. | |
| 31 | */ | |
| 32 | private ArrayUtils() { | |
| 33 | 0 | super(); |
| 34 | 0 | } |
| 35 | ||
| 36 | /** | |
| 37 | * Inverse the order of the array and return it. | |
| 38 | * | |
| 39 | * @param array The array to inverse. | |
| 40 | */ | |
| 41 | public static void reverse(Object[] array) { | |
| 42 | 2 | int len = array.length; |
| 43 | 2 | int hlen = len / 2; |
| 44 | ||
| 45 | 6 | for (int i = 0; i < hlen; ++i) { |
| 46 | 4 | Object temp = array[i]; |
| 47 | 4 | array[i] = array[len - 1 - i]; |
| 48 | 4 | array[len - 1 - i] = temp; |
| 49 | } | |
| 50 | 2 | } |
| 51 | ||
| 52 | /** | |
| 53 | * Return the index of the object on the tab. | |
| 54 | * | |
| 55 | * @param object The object to search for. | |
| 56 | * @param tab The tab to search in. | |
| 57 | * @return The index of the objet in the tab else -1 if the object is not present in the tab. | |
| 58 | */ | |
| 59 | public static int indexOf(Object object, Object[] tab) { | |
| 60 | 32 | for (int i = 0; i < tab.length; i++) { |
| 61 | 28 | if (tab[i] == object) { |
| 62 | 4 | return i; |
| 63 | } | |
| 64 | } | |
| 65 | ||
| 66 | 4 | return -1; |
| 67 | } | |
| 68 | ||
| 69 | /** | |
| 70 | * Reverse the array. | |
| 71 | * | |
| 72 | * @param array The array to reverse. | |
| 73 | */ | |
| 74 | public static void reverse(int[] array) { | |
| 75 | 2 | int len = array.length; |
| 76 | 2 | int hlen = len / 2; |
| 77 | ||
| 78 | 8 | for (int i = 0; i < hlen; ++i) { |
| 79 | 6 | int temp = array[i]; |
| 80 | 6 | array[i] = array[len - 1 - i]; |
| 81 | 6 | array[len - 1 - i] = temp; |
| 82 | } | |
| 83 | 2 | } |
| 84 | ||
| 85 | /** | |
| 86 | * Test if the array is empty or contains only <code>null</code> values. | |
| 87 | * | |
| 88 | * @param array The array to test. | |
| 89 | * @return true if the array size is 0 or if the array contains only <code>null</code> values. | |
| 90 | */ | |
| 91 | public static boolean isEmpty(Object[] array) { | |
| 92 | 6 | if(array == null || array.length <= 0){ |
| 93 | 2 | return true; |
| 94 | } | |
| 95 | ||
| 96 | 6 | for(Object o : array){ |
| 97 | 4 | if(o != null){ |
| 98 | 2 | return false; |
| 99 | } | |
| 100 | } | |
| 101 | ||
| 102 | 2 | return true; |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * Return a copy of the array. | |
| 107 | * | |
| 108 | * @param array The array to copy. | |
| 109 | * @param <T> The type of objects. | |
| 110 | * @return The copy of the array. | |
| 111 | */ | |
| 112 | public static <T> T[] copyOf(T[] array) { | |
| 113 | 2 | return Arrays.copyOf(array, array.length); |
| 114 | } | |
| 115 | } |