1 17 18 package org.objectweb.jac.util; 19 20 import java.util.Arrays ; 21 import java.util.List ; 22 import org.aopalliance.intercept.Interceptor; 23 import java.lang.reflect.Array ; 24 25 26 27 30 public class ExtArrays 31 { 32 public static final Object [] emptyObjectArray = new Object [0]; 33 public static final String [] emptyStringArray = new String [0]; 34 public static final Class [] emptyClassArray = new Class [0]; 35 public static final Interceptor[] emptyInterceptorArray = new Interceptor[0]; 36 37 51 public static Object [] add(int position, Object toInsert, Object [] array) { 52 Class type = array.getClass().getComponentType(); 53 if (!type.isInstance(toInsert)) { 54 if (toInsert.getClass().isAssignableFrom(type)) 55 type = toInsert.getClass(); 56 else 57 type = Object .class; 58 } 59 return add(position,toInsert,array,type); 60 } 61 62 74 public static Object [] add(int position, Object toInsert, Object [] array, Class type) { 75 Object [] newArray = (Object []) 76 Array.newInstance(type,array.length+1); 77 if (position>0) 78 System.arraycopy(array,0,newArray,0,position); 79 newArray[position] = toInsert; 80 if (position<array.length) { 81 System.arraycopy(array,position, 82 newArray,position+1, 83 array.length-position); 84 } 85 return newArray; 86 } 87 88 98 public static Object [] add(Object toAppend, Object [] array) { 99 return add(array.length,toAppend,array); 100 } 101 102 113 public static Object [] add(Object toAppend, Object [] array, Class type) { 114 return add(array.length,toAppend,array,type); 115 } 116 117 125 public static int indexOf(Object [] array, Object value) { 126 for (int i=0; i<array.length; i++) { 127 if (value==array[i]) { 128 return i; 129 } 130 } 131 return -1; 132 } 133 134 140 public static boolean contains(Object [] array, Object value) { 141 return indexOf(array,value)!=-1; 142 } 143 144 152 public static boolean equals(byte[] a, int offseta, 153 byte[] b, int offsetb, 154 int length) { 155 for (int i=0; i<length; i++) { 156 if (a[offseta+i]!=b[offsetb+i]) 157 return false; 158 } 159 return true; 160 } 161 162 166 public static List asList(byte[] array) { 167 Object [] objects = new Object [array.length]; 168 for (int i=0; i<array.length; i++) { 169 objects[i] = new Byte (array[i]); 170 } 171 return Arrays.asList(objects); 172 } 173 174 177 public static Object subArray(Object [] array, int start) { 178 Object result = 179 Array.newInstance( 180 array.getClass().getComponentType(), 181 array.length-start); 182 System.arraycopy(array, start, result, 0, array.length-start); 183 return result; 184 } 185 } 186 | Popular Tags |