1 16 17 package com.icesoft.faces.util; 18 19 import java.lang.reflect.Array ; 20 21 22 29 public class ArrayUtils { 30 public static final Object [] EMPTY_OBJECT_ARRAY = new Object [0]; 31 public static final String [] EMPTY_STRING_ARRAY = new String [0]; 32 33 35 protected ArrayUtils() { 36 } 38 39 41 public static Class commonClass(Class c1, Class c2) { 42 if (c1 == c2) { 43 return c1; 44 } 45 46 if ((c1 == Object .class) || c1.isAssignableFrom(c2)) { 47 return c1; 48 } 49 50 if (c2.isAssignableFrom(c1)) { 51 return c2; 52 } 53 54 if (c1.isPrimitive() || c2.isPrimitive()) { 55 throw new IllegalArgumentException ( 57 "incompatible types " + c1 + " and " + c2); 58 } 59 60 return Object .class; 62 } 63 64 75 public static Object concat(Object arr1, Object arr2) { 76 int len1 = (arr1 == null) ? (-1) : Array.getLength(arr1); 77 78 if (len1 <= 0) { 79 return arr2; 80 } 81 82 int len2 = (arr2 == null) ? (-1) : Array.getLength(arr2); 83 84 if (len2 <= 0) { 85 return arr1; 86 } 87 88 Class commonComponentType = 89 commonClass(arr1.getClass().getComponentType(), 90 arr2.getClass().getComponentType()); 91 Object newArray = Array.newInstance(commonComponentType, len1 + len2); 92 System.arraycopy(arr1, 0, newArray, 0, len1); 93 System.arraycopy(arr2, 0, newArray, len1, len2); 94 95 return newArray; 96 } 97 98 106 public static Object concat(Object [] arrs) { 107 int totalLen = 0; 108 Class commonComponentType = null; 109 for (int i = 0, len = arrs.length; i < len; i++) { 110 if (arrs[i] == null) { 112 continue; 113 } 114 115 int arrayLen = Array.getLength(arrs[i]); 116 117 if (arrayLen == 0) { 119 continue; 120 } 121 122 totalLen += arrayLen; 123 124 Class componentType = arrs[i].getClass().getComponentType(); 125 commonComponentType = 126 (commonComponentType == null) ? componentType 127 : commonClass(commonComponentType, componentType); 128 } 129 130 if (commonComponentType == null) { 131 return null; 132 } 133 134 return concat(Array.newInstance(commonComponentType, totalLen), 135 totalLen, arrs); 136 } 137 138 public static Object concat(Object toArray, int totalLen, Object [] arrs) { 139 if (totalLen == 0) { 140 return toArray; 142 } 143 144 if (totalLen > Array.getLength(toArray)) { 145 toArray = Array.newInstance(toArray.getClass().getComponentType(), 146 totalLen); 147 } 148 149 for (int i = 0, len = arrs.length, offset = 0; i < len; i++) { 150 final Object arr = arrs[i]; 151 if (arr != null) { 152 int arrayLen = Array.getLength(arr); 153 if (arrayLen > 0) { 154 System.arraycopy(arr, 0, toArray, offset, arrayLen); 155 offset += arrayLen; 156 } 157 } 158 } 159 160 return toArray; 161 } 162 163 public static Object concat(Object arr1, Object arr2, Object arr3) { 164 return concat(new Object []{arr1, arr2, arr3}); 165 } 166 167 public static Object concat(Object arr1, Object arr2, Object arr3, 168 Object arr4) { 169 return concat(new Object []{arr1, arr2, arr3, arr4}); 170 } 171 172 public static Object concat(Object arr1, Object arr2, Object arr3, 173 Object arr4, Object arr5) { 174 return concat(new Object []{arr1, arr2, arr3, arr4, arr5}); 175 } 176 177 public static Object concatSameType(Object toArray, Object [] arrs) { 178 int totalLen = 0; 179 for (int i = 0, len = arrs.length; i < len; i++) { 180 if (arrs[i] != null) { 181 totalLen += Array.getLength(arrs[i]); 182 } 183 } 184 185 return concat(toArray, totalLen, arrs); 186 } 187 188 189 public static boolean contains(Object [] array, Object value) { 190 if (array == null || array.length == 0) { 191 return false; 192 } 193 194 for (int i = 0; i < array.length; i++) { 195 Object o = array[i]; 196 if ((o == null && value == null) || 197 (o != null && o.equals(value))) { 198 return true; 199 } 200 } 201 202 return false; 203 } 204 } 205 | Popular Tags |