1 16 package org.apache.myfaces.util; 17 18 import java.lang.reflect.Array ; 19 20 21 40 public class ArrayUtils 41 { 42 public static final Object [] EMPTY_OBJECT_ARRAY = new Object [0]; 43 public static final String [] EMPTY_STRING_ARRAY = new String [0]; 44 45 47 protected ArrayUtils() 48 { 49 } 51 52 54 public static Class commonClass(Class c1, Class c2) 55 { 56 if (c1 == c2) 57 { 58 return c1; 59 } 60 61 if ((c1 == Object .class) || c1.isAssignableFrom(c2)) 62 { 63 return c1; 64 } 65 66 if (c2.isAssignableFrom(c1)) 67 { 68 return c2; 69 } 70 71 if (c1.isPrimitive() || c2.isPrimitive()) 72 { 73 throw new IllegalArgumentException ("incompatible types " + c1 + " and " + c2); 75 } 76 77 return Object .class; 79 } 80 81 92 public static Object concat(Object arr1, Object arr2) 93 { 94 int len1 = (arr1 == null) ? (-1) : Array.getLength(arr1); 95 96 if (len1 <= 0) 97 { 98 return arr2; 99 } 100 101 int len2 = (arr2 == null) ? (-1) : Array.getLength(arr2); 102 103 if (len2 <= 0) 104 { 105 return arr1; 106 } 107 108 Class commonComponentType = 109 commonClass(arr1.getClass().getComponentType(), arr2.getClass().getComponentType()); 110 Object newArray = Array.newInstance(commonComponentType, len1 + len2); 111 System.arraycopy(arr1, 0, newArray, 0, len1); 112 System.arraycopy(arr2, 0, newArray, len1, len2); 113 114 return newArray; 115 } 116 117 125 public static Object concat(Object [] arrs) 126 { 127 int totalLen = 0; 128 Class commonComponentType = null; 129 for (int i = 0, len = arrs.length; i < len; i++) 130 { 131 if (arrs[i] == null) 133 { 134 continue; 135 } 136 137 int arrayLen = Array.getLength(arrs[i]); 138 139 if (arrayLen == 0) 141 { 142 continue; 143 } 144 145 totalLen += arrayLen; 146 147 Class componentType = arrs[i].getClass().getComponentType(); 148 commonComponentType = 149 (commonComponentType == null) ? componentType 150 : commonClass(commonComponentType, componentType); 151 } 152 153 if (commonComponentType == null) 154 { 155 return null; 156 } 157 158 return concat(Array.newInstance(commonComponentType, totalLen), totalLen, arrs); 159 } 160 161 public static Object concat(Object toArray, int totalLen, Object [] arrs) 162 { 163 if (totalLen == 0) 164 { 165 return toArray; 167 } 168 169 if (totalLen > Array.getLength(toArray)) 170 { 171 toArray = Array.newInstance(toArray.getClass().getComponentType(), totalLen); 172 } 173 174 for (int i = 0, len = arrs.length, offset = 0; i < len; i++) 175 { 176 final Object arr = arrs[i]; 177 if (arr != null) 178 { 179 int arrayLen = Array.getLength(arr); 180 if (arrayLen > 0) 181 { 182 System.arraycopy(arr, 0, toArray, offset, arrayLen); 183 offset += arrayLen; 184 } 185 } 186 } 187 188 return toArray; 189 } 190 191 public static Object concat(Object arr1, Object arr2, Object arr3) 192 { 193 return concat(new Object [] {arr1, arr2, arr3}); 194 } 195 196 public static Object concat(Object arr1, Object arr2, Object arr3, Object arr4) 197 { 198 return concat(new Object [] {arr1, arr2, arr3, arr4}); 199 } 200 201 public static Object concat(Object arr1, Object arr2, Object arr3, Object arr4, Object arr5) 202 { 203 return concat(new Object [] {arr1, arr2, arr3, arr4, arr5}); 204 } 205 206 public static Object concatSameType(Object toArray, Object [] arrs) 207 { 208 int totalLen = 0; 209 for (int i = 0, len = arrs.length; i < len; i++) 210 { 211 if (arrs[i] != null) 212 { 213 totalLen += Array.getLength(arrs[i]); 214 } 215 } 216 217 return concat(toArray, totalLen, arrs); 218 } 219 220 221 222 public static boolean contains(Object [] array, Object value) 223 { 224 if (array == null || array.length == 0) 225 { 226 return false; 227 } 228 229 for (int i = 0; i < array.length; i++) 230 { 231 Object o = array[i]; 232 if ((o == null && value == null) || 233 (o != null && o.equals(value))) 234 { 235 return true; 236 } 237 } 238 239 return false; 240 } 241 242 243 244 } 264 | Popular Tags |