1 19 20 package org.netbeans.editor; 21 22 import java.util.Comparator ; 23 24 30 31 public class ObjectArrayUtilities { 32 33 private ObjectArrayUtilities() { 34 } 36 37 61 public static int binarySearch(ObjectArray objectArray, Object key) { 62 int low = 0; 63 int high = objectArray.getItemCount() - 1; 64 65 while (low <= high) { 66 int mid = (low + high) >> 1; 67 Object midVal = objectArray.getItem(mid); 68 int cmp = ((Comparable )midVal).compareTo(key); 69 70 if (cmp < 0) 71 low = mid + 1; 72 else if (cmp > 0) 73 high = mid - 1; 74 else 75 return mid; } 77 78 return -(low + 1); } 80 81 91 public static int binarySearch(ObjectArray objectArray, Object key, Comparator c) { 92 int low = 0; 93 int high = objectArray.getItemCount() - 1; 94 95 while (low <= high) { 96 int mid = (low + high) >> 1; 97 Object midVal = objectArray.getItem(mid); 98 int cmp = c.compare(midVal, key); 99 100 if (cmp < 0) 101 low = mid + 1; 102 else if (cmp > 0) 103 high = mid - 1; 104 else 105 return mid; } 107 108 return -(low + 1); } 110 111 124 public static int findIndex(ObjectArray objectArray, Object item) { 125 int index = binarySearch(objectArray, item); 126 127 if (index < 0) { return -1; 129 } 130 if (objectArray.getItem(index) == item) { 131 return index; 132 } 133 134 int cnt = objectArray.getItemCount(); 135 for (int upIndex = index + 1; upIndex < cnt; upIndex++) { 136 Object indexItem = objectArray.getItem(upIndex); 137 if (indexItem == item) { 138 return upIndex; 139 } 140 if (((Comparable )item).compareTo(indexItem) != 0) { 141 break; 142 } 143 } 144 145 while (--index >= 0) { 146 Object indexItem = objectArray.getItem(index); 147 if (indexItem == item) { 148 return index; 149 } 150 if (((Comparable )item).compareTo(indexItem) != 0) { 151 break; 152 } 153 } 154 155 return -1; 156 } 157 158 172 public static int findIndex(ObjectArray objectArray, Object item, Comparator c) { 173 int index = binarySearch(objectArray, item, c); 174 175 if (index < 0) { return -1; 177 } 178 if (objectArray.getItem(index) == item) { 179 return index; 180 } 181 182 int cnt = objectArray.getItemCount(); 183 for (int upIndex = index + 1; upIndex < cnt; upIndex++) { 184 Object indexItem = objectArray.getItem(upIndex); 185 if (indexItem == item) { 186 return upIndex; 187 } 188 if (c.compare(item, indexItem) != 0) { 189 break; 190 } 191 } 192 193 while (--index >= 0) { 194 Object indexItem = objectArray.getItem(index); 195 if (indexItem == item) { 196 return index; 197 } 198 if (c.compare(item, indexItem) != 0) { 199 break; 200 } 201 } 202 203 return -1; 204 } 205 206 211 public static Object [] toArray(ObjectArray objectArray) { 212 return toArray(objectArray, 0, objectArray.getItemCount()); 213 } 214 215 222 public static Object [] toArray(ObjectArray objectArray, int startIndex, int endIndex) { 223 Object [] dest = new Object [endIndex - startIndex]; 224 copyItems(objectArray, startIndex, endIndex, dest, 0); 225 return dest; 226 } 227 228 233 public static void copyItems(ObjectArray srcObjectArray, Object [] dest) { 234 copyItems(srcObjectArray, 0, srcObjectArray.getItemCount(), dest, 0); 235 } 236 237 246 public static void copyItems(ObjectArray srcObjectArray, 247 int srcStartIndex, int srcEndIndex, Object [] dest, int destIndex) { 248 249 if (srcObjectArray instanceof ObjectArray.CopyItems) { 250 ((ObjectArray.CopyItems)srcObjectArray).copyItems( 251 srcStartIndex, srcEndIndex, dest, destIndex); 252 253 } else { 254 while (srcStartIndex < srcEndIndex) { 255 dest[destIndex++] = srcObjectArray.getItem(srcStartIndex++); 256 } 257 } 258 } 259 260 264 public static void reverse(Object [] array) { 265 for (int i = 0, j = array.length - 1; i < j; i++, j--) { 266 Object o = array[i]; 267 array[i] = array[j]; 268 array[j] = o; 269 } 270 } 271 272 273 public static ObjectArray.Modification mergeModifications(final ObjectArray.Modification mod1, final ObjectArray.Modification mod2) { 274 if (mod1.getArray() != mod2.getArray()) { 275 throw new IllegalArgumentException ("Cannot merge modifications to different object arrays."); } 277 final int index; 278 final Object [] addedItems; 279 final int removedItemsCount; 280 if (mod1.getIndex() <= mod2.getIndex()) { 281 index = mod1.getIndex(); 282 int overlap = Math.min(mod1.getIndex() + mod1.getAddedItems().length - mod2.getIndex(), mod2.getRemovedItemsCount()); 283 addedItems = new Object [mod1.getAddedItems().length + mod2.getAddedItems().length - overlap]; 284 if (overlap < 0) { System.arraycopy(mod1.getAddedItems(), 0, addedItems, 0, mod1.getAddedItems().length); 286 copyItems(mod1.getArray(), mod1.getIndex() + mod1.getRemovedItemsCount(), mod1.getIndex() + mod1.getRemovedItemsCount() - overlap, addedItems, mod1.getAddedItems().length); 287 System.arraycopy(mod2.getAddedItems(), 0, addedItems, mod2.getIndex() - mod1.getIndex(), mod2.getAddedItems().length); 288 } else { System.arraycopy(mod1.getAddedItems(), 0, addedItems, 0, mod2.getIndex() - mod1.getIndex()); 290 System.arraycopy(mod2.getAddedItems(), 0, addedItems, mod2.getIndex() - mod1.getIndex(), mod2.getAddedItems().length); 291 System.arraycopy(mod1.getAddedItems(), mod2.getIndex() - mod1.getIndex() + overlap, addedItems, mod2.getIndex() - mod1.getIndex() + mod2.getAddedItems().length, mod1.getAddedItems().length - mod2.getIndex() + mod1.getIndex() - overlap); 292 } 293 removedItemsCount = mod1.getRemovedItemsCount() + mod2.getRemovedItemsCount() - overlap; 294 } else { 295 index = mod2.getIndex(); 296 int overlap = Math.min(mod2.getIndex() + mod2.getRemovedItemsCount() - mod1.getIndex(), mod1.getAddedItems().length); 297 addedItems = new Object [mod1.getAddedItems().length + mod2.getAddedItems().length - overlap]; 298 System.arraycopy(mod2.getAddedItems(), 0, addedItems, 0, mod2.getAddedItems().length); 299 if (overlap < 0) { copyItems(mod1.getArray(), mod2.getIndex() + mod2.getRemovedItemsCount(), mod1.getIndex(), addedItems, mod2.getAddedItems().length); 301 System.arraycopy(mod1.getAddedItems(), 0, addedItems, mod2.getAddedItems().length - overlap, mod1.getAddedItems().length); 302 } else { System.arraycopy(mod1.getAddedItems(), overlap, addedItems, mod2.getAddedItems().length, mod1.getAddedItems().length - overlap); 304 } 305 removedItemsCount = mod1.getRemovedItemsCount() + mod2.getRemovedItemsCount() - overlap; 306 } 307 return new ObjectArray.Modification () { 308 public ObjectArray getArray() { 309 return mod1.getArray(); 310 } 311 public int getIndex() { 312 return index; 313 } 314 public Object [] getAddedItems() { 315 return addedItems; 316 } 317 public int getRemovedItemsCount() { 318 return removedItemsCount; 319 } 320 }; 321 } 322 323 public static ObjectArray.Modification mergeModifications(ObjectArray.Modification[] mods) { 324 ObjectArray.Modification ret = mods.length > 0 ? mods[0] : null; 325 for (int i = 1; i < mods.length; i++) { 326 ret = mergeModifications(ret, mods[i]); 327 } 328 return ret; 329 } 330 } 331 | Popular Tags |