1 23 24 package com.sun.enterprise.util.collection; 25 26 import java.util.*; 27 28 public class LongArrayList 29 implements List 30 { 31 32 long[] data; 33 int minIndex; int size; 35 double growFactor; 36 37 public LongArrayList() { 38 this(6, 0.5); 39 } 40 41 public LongArrayList(int initialCapacity) { 42 this(initialCapacity, 0.5); 43 } 44 45 public LongArrayList(int initialCapacity, double growFactor) { 46 data = new long[initialCapacity]; 47 this.growFactor = growFactor; 48 } 49 50 private LongArrayList(long[] data, int minIndex, int size, double growFactor) { 51 this.data = data; 52 this.minIndex = minIndex; 53 this.size = size; 54 this.growFactor = growFactor; 55 } 56 57 public void add(int index, Object o) { 58 long val = ((Long ) o).longValue(); 59 if (size == data.length) { 60 makeRoom(1); 61 } 62 63 System.arraycopy(data, index, data, index+1, size - index); 64 data[index] = val; 65 size++; 66 67 } 68 69 public void add(int index, long val) { 70 if (size == data.length) { 71 makeRoom(1); 72 } 73 74 System.arraycopy(data, index, data, index+1, size - index); 75 data[index] = val; 76 size++; 77 78 } 79 80 public boolean add(Object o) { 81 long val = ((Long ) o).longValue(); 82 if (size == data.length) { 83 makeRoom(1); 84 } 85 86 data[size++] = val; 87 return true; 88 } 89 90 public boolean add(long val) { 91 if (size == data.length) { 92 makeRoom(1); 93 } 94 95 data[size++] = val; 96 return true; 97 } 98 99 public boolean addAll(Collection coll) { 100 int collSize = coll.size(); 101 makeRoom(collSize); 102 Iterator iter = coll.iterator(); 103 while (iter.hasNext()) { 104 data[size++] = ((Long ) iter.next()).longValue(); 105 } 106 return (collSize > 0); 107 } 108 109 public boolean addAll(LongArrayList coll) { 110 int collSize = coll.size(); 111 makeRoom(collSize); 112 LongIterator longIter = coll.longIterator(); 113 while (longIter.hasNext()) { 114 data[size++] = longIter.nextLong(); 115 } 116 return (collSize > 0); 117 } 118 119 public boolean addAll(int index, Collection coll) { 120 int collSize = coll.size(); 121 makeRoom(collSize); 122 System.arraycopy(data, index, data, index+collSize, size - index); 123 Iterator iter = coll.iterator(); 124 while (iter.hasNext()) { 125 data[index++] = ((Long ) iter.next()).longValue(); 126 } 127 size += collSize; 128 return (collSize > 0); 129 } 130 131 public boolean addAll(int index, LongArrayList coll) { 132 int collSize = coll.size(); 133 makeRoom(collSize); 134 System.arraycopy(data, index, data, index+collSize, size - index); 135 LongIterator longIter = coll.longIterator(); 136 while (longIter.hasNext()) { 137 data[index++] = longIter.nextLong(); 138 } 139 size += collSize; 140 return (collSize > 0); 141 } 142 143 public void clear() { 144 size = 0; 145 data = new long[6]; 146 } 147 148 public boolean contains(Object o) { 149 return (indexOf(o) >= 0); 150 } 151 152 public boolean contains(long val) { 153 return (indexOf(val) >= 0); 154 } 155 156 public boolean containsAll(Collection coll) { 157 Iterator iter = coll.iterator(); 158 while (iter.hasNext()) { 159 if (contains(iter.next()) == false) { 160 return false; 161 } 162 } 163 return true; 164 } 165 166 public boolean containsAll(LongArrayList coll) { 167 LongIterator iter = coll.longIterator(); 168 while (iter.hasNext()) { 169 if (contains(iter.nextLong()) == false) { 170 return false; 171 } 172 } 173 return true; 174 } 175 176 public boolean containsAll(long[] array) { 177 for (int i=array.length; i > 0; ) { 178 if (contains(array[--i]) == false) { 179 return false; 180 } 181 } 182 return true; 183 } 184 185 public boolean equals(Object o) { 186 return this == o; 188 } 189 190 public Object get(int index) { 191 return new Long (data[index]); 192 } 193 194 public long getValue(int index) { 195 return data[index]; 196 } 197 198 public int hashCode() { 199 long hashCode = 1; 200 for (int i=0; i<size; i++) { 201 hashCode = 31*hashCode + data[i]; 202 } 203 return (int) hashCode; 204 } 205 206 public int indexOf(Object o) { 207 long val = ((Long ) o).longValue(); 208 for (int i=0; i<size; i++) { 209 if (data[i] == val) { 210 return i; 211 } 212 } 213 return -1; 214 } 215 216 public int indexOf(long val) { 217 for (int i=0; i<size; i++) { 218 if (data[i] == val) { 219 return i; 220 } 221 } 222 return -1; 223 } 224 225 public boolean isEmpty() { 226 return size == 0; 227 } 228 229 public int lastIndexOf(Object o) { 230 long val = ((Long ) o).longValue(); 231 for (int i=size-1; i >= 0; i--) { 232 if (data[i] == val) { 233 return i; 234 } 235 } 236 return -1; 237 } 238 239 public int lastIndexOf(long val) { 240 for (int i=size-1; i >= 0; i--) { 241 if (data[i] == val) { 242 return i; 243 } 244 } 245 return -1; 246 } 247 248 public ListIterator listIterator() { 249 return new LongIterator(0); 250 } 251 252 public ListIterator listIterator(int index) { 253 return new LongIterator(index); 254 } 255 256 public Object remove(int index) { 257 long val = data[index]; 258 if (index < size - 1) { 259 System.arraycopy(data, index+1, data, index, size - index); 260 } 261 size--; 262 return new Long (val); 263 } 264 265 public long removeLong(int index) { 266 long val = data[index]; 267 if (index < size - 1) { 268 System.arraycopy(data, index+1, data, index, size - index); 269 } 270 size--; 271 return val; 272 } 273 274 public boolean remove(Object object) { 275 long val = ((Long ) object).longValue(); 276 for (int i=0; i<size; i++) { 277 if (data[i] == val) { 278 if (i < size - 1) { 279 System.arraycopy(data, i+1, data, i, size - i); 280 size--; 281 return true; 282 } 283 } 284 } 285 return false; 286 } 287 288 public boolean remove(long val) { 289 for (int i=0; i<size; i++) { 290 if (data[i] == val) { 291 if (i < size - 1) { 292 System.arraycopy(data, i+1, data, i, size - i); 293 size--; 294 return true; 295 } 296 } 297 } 298 return false; 299 } 300 301 302 303 public boolean removeAll(Collection coll) { 304 boolean removed = false; 305 Iterator iter = coll.iterator(); 306 while (iter.hasNext()) { 307 if (remove(iter.next()) == true) { 308 removed = true; 309 } 310 } 311 return removed; 312 } 313 314 public boolean removeAll(LongArrayList coll) { 315 boolean removed = false; 316 LongIterator iter = coll.longIterator(); 317 while (iter.hasNext()) { 318 if (remove(iter.nextLong()) == true) { 319 removed = true; 320 } 321 } 322 return removed; 323 } 324 325 public boolean removeAll(long[] array) { 326 boolean removed = false; 327 for (int i=0; i<array.length; i++) { 328 if (remove(array[i]) == true) { 329 removed = true; 330 } 331 } 332 return removed; 333 } 334 335 336 public boolean retainAll(Collection coll) { 337 return false; 338 } 339 340 public boolean retainAll(LongArrayList coll) { 341 return false; 342 } 343 344 public boolean retainAll(long[] array) { 345 return false; 346 } 347 348 349 public Object set(int index, Object o) { 350 long oldVal = data[index]; 351 data[index] = ((Long ) o).longValue(); 352 return new Long (oldVal); 353 } 354 355 public long set(int index, long val) { 356 long oldVal = data[index]; 357 data[index] = val; 358 return oldVal; 359 } 360 361 362 public int size() { 363 return size; 364 } 365 366 public List subList(int fromIndex, int toIndex) { 367 return new LongArrayList(data, fromIndex, toIndex, growFactor); 368 } 369 370 public Object [] toArray() { 371 Long [] array = new Long [size]; 372 for (int i=0; i<size; i++) { 373 array[i] = new Long (data[i]); 374 } 375 return array; 376 } 377 378 public Object [] toArray(Object [] arr) { 379 Long [] array = new Long [size]; 380 for (int i=0; i<size; i++) { 381 array[i] = new Long (data[i]); 382 } 383 return array; 384 } 385 386 387 protected void makeRoom(int space) { 388 389 if (space + size >= data.length) { 390 long[] oldData = data; 391 392 int newSize = size + space + ((int) (size * growFactor)) + 1; 393 data = new long[newSize]; 394 395 System.arraycopy(oldData, 0, data, 0, oldData.length); 396 } 397 } 398 399 400 public Iterator iterator() { 401 return new LongIterator(0); 402 } 403 404 public LongIterator longIterator() { 405 return new LongIterator(0); 406 } 407 408 public void print() { 409 System.out.print("Data (size: " + size + "): "); 410 for (int i=0; i<size; i++) { 411 System.out.print(" " + data[i]); 412 } 413 System.out.println(); 414 } 415 416 417 public static void main(String [] args) { 418 LongArrayList list = new LongArrayList(); 419 420 for (int i=0; i<15; i+=2) { 421 list.add(i); 422 } 423 424 for (int i=1; i<15; i+=2) { 425 list.add(i); 426 } 427 } 428 429 430 private class LongIterator 431 implements ListIterator 432 { 433 int startIndex; 434 int index = 0; 435 436 LongIterator(int startAt) { 437 startIndex = startAt; 438 } 439 440 public void add(Object o) { 441 ; 442 } 443 444 public boolean hasPrevious() { 445 return index > startIndex; 446 } 447 448 public boolean hasNext() { 449 return index < size; 450 } 451 452 public Object previous() { 453 return new Long (data[--index]); 454 } 455 456 public int previousIndex() { 457 return index; 458 } 459 460 public Object next() { 461 return new Long (data[index++]); 462 } 463 464 public int nextIndex() { 465 return index; 466 } 467 468 public long previousLong() { 469 return data[--index]; 470 } 471 472 public long nextLong() { 473 return data[index++]; 474 } 475 476 public void remove() { 477 } 479 480 public void set(Object o) { 481 ; 482 } 483 } 484 485 } 486 | Popular Tags |