| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
package jaggregate; |
| 7 | |
|
| 8 | |
import java.io.IOException; |
| 9 | |
import java.io.ObjectInputStream; |
| 10 | |
import java.io.ObjectOutputStream; |
| 11 | |
import java.io.Serializable; |
| 12 | |
import java.lang.reflect.Array; |
| 13 | |
import java.util.NoSuchElementException; |
| 14 | |
import static java.lang.Math.*; |
| 15 | |
import static java.lang.System.*; |
| 16 | |
|
| 17 | |
import jaggregate.internal.Casting; |
| 18 | |
import jaggregate.internal.ReadOnlySequences; |
| 19 | |
import static jaggregate.internal.ArgumentChecks.*; |
| 20 | |
import static jaggregate.internal.ReadOnlySequences.*; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | 2787 | public class OrderedCollection<E> extends AbstractExtensibleCollection<E> |
| 33 | |
implements ContractibleSequence<E>, Sequence<E>, Serializable { |
| 34 | |
|
| 35 | |
private static final int LEFT_BOUND = -2; |
| 36 | |
private static final long serialVersionUID = -1L; |
| 37 | |
private static final int DEFAULT_INITIAL_CAPACITY = 10; |
| 38 | |
|
| 39 | |
private transient Object[] storage; |
| 40 | |
private int numberOfElements; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public OrderedCollection() { |
| 46 | 6920 | this( DEFAULT_INITIAL_CAPACITY ); |
| 47 | 6920 | } |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public OrderedCollection( Collection<? extends E> elements ) { |
| 56 | 193 | this(); |
| 57 | 193 | addAll( elements ); |
| 58 | 192 | } |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public OrderedCollection( E... elements ) { |
| 67 | 531 | this(); |
| 68 | 531 | addAll( elements ); |
| 69 | 530 | } |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
public OrderedCollection( Iterable<? extends E> elements ) { |
| 79 | 144 | this(); |
| 80 | 144 | addAll( elements ); |
| 81 | 143 | } |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | 6922 | public OrderedCollection( int capacity ) { |
| 90 | 6922 | if ( capacity <= 0 ) |
| 91 | 1 | throw new IllegalArgumentException( "non-positive capacity: " + capacity ); |
| 92 | |
|
| 93 | 6921 | storage = new Object[ capacity ]; |
| 94 | 6921 | numberOfElements = 0; |
| 95 | 6921 | } |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public static <T> OrderedCollection<T> emptyOrderedCollection() { |
| 104 | 6010 | return new OrderedCollection<T>(); |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public static <T> OrderedCollection<T> orderedCollectionFrom( |
| 116 | |
Collection<? extends T> elements ) { |
| 117 | |
|
| 118 | 181 | return new OrderedCollection<T>( elements ); |
| 119 | |
} |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public static <T> OrderedCollection<T> orderedCollectionFrom( T[] elements ) { |
| 130 | 530 | return new OrderedCollection<T>( elements ); |
| 131 | |
} |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public static <T> OrderedCollection<T> orderedCollectionWith( T newElement, |
| 146 | |
T... restOfNewElements ) { |
| 147 | |
|
| 148 | 1608 | OrderedCollection<T> ordered = emptyOrderedCollection(); |
| 149 | 1608 | ordered.add( newElement ); |
| 150 | 1608 | ordered.addAll( restOfNewElements ); |
| 151 | |
|
| 152 | 1608 | return ordered; |
| 153 | |
} |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
public static <T> OrderedCollection<T> orderedCollectionFrom( |
| 165 | |
Iterable<? extends T> elements ) { |
| 166 | |
|
| 167 | 144 | return new OrderedCollection<T>( elements ); |
| 168 | |
} |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
@Override |
| 174 | |
public Object[] toArray( Class<? super E> componentClass ) { |
| 175 | 23 | Object[] asArray = (Object[]) Array.newInstance( componentClass, size() ); |
| 176 | 23 | arraycopy( storage, 0, asArray, 0, size() ); |
| 177 | |
|
| 178 | 23 | return asArray; |
| 179 | |
} |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
@Override |
| 187 | |
public OrderedCollection<E> toOrderedCollection() { |
| 188 | 1 | return this; |
| 189 | |
} |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
@Override |
| 195 | |
public <R> OrderedCollection<R> collect( |
| 196 | |
UnaryFunctor<? super E, ? extends R> transformer ) { |
| 197 | |
|
| 198 | 15 | return (OrderedCollection<R>) super.collect( transformer ); |
| 199 | |
} |
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
public void forEachDo( UnaryFunctor<? super E, ?> operation ) { |
| 205 | 1588 | ensureNotNull( operation, OPERATION ); |
| 206 | |
|
| 207 | 6040 | for ( int i = 0; i < size(); ++i ) |
| 208 | 4616 | operation.evaluate( at( i ) ); |
| 209 | 1424 | } |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
@Override |
| 215 | |
public OrderedCollection<E> reject( UnaryCondition<? super E> discriminator ) { |
| 216 | 27 | return (OrderedCollection<E>) super.reject( discriminator ); |
| 217 | |
} |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
@Override |
| 223 | |
public OrderedCollection<E> select( UnaryCondition<? super E> discriminator ) { |
| 224 | 27 | return (OrderedCollection<E>) super.select( discriminator ); |
| 225 | |
} |
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
public int size() { |
| 231 | 80720 | return numberOfElements; |
| 232 | |
} |
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
public int findFirst( UnaryCondition<? super E> discriminator ) { |
| 238 | 31 | return ReadOnlySequences.findFirst( this, discriminator ); |
| 239 | |
} |
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
public int findLast( UnaryCondition<? super E> discriminator ) { |
| 245 | 41 | return ReadOnlySequences.findLast( this, discriminator ); |
| 246 | |
} |
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
public void forEachInReverseDo( UnaryFunctor<? super E, ?> operation ) { |
| 252 | 74 | ReadOnlySequences.forEachInReverseDo( this, operation ); |
| 253 | 34 | } |
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
public E first() { |
| 259 | 117 | ensureNotEmpty(); |
| 260 | |
|
| 261 | 116 | return at( 0 ); |
| 262 | |
} |
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
public void forEachInRangeDo( int start, int stop, |
| 268 | |
UnaryFunctor<? super E, ?> operation ) { |
| 269 | |
|
| 270 | 247 | ReadOnlySequences.forEachInRangeDo( this, start, stop, operation ); |
| 271 | 220 | } |
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
public void forEachInRangeDoWithIndex( int start, int stop, |
| 277 | |
BinaryFunctor<? super Integer, ? super E, ?> operation ) { |
| 278 | |
|
| 279 | 222 | ReadOnlySequences.forEachInRangeDoWithIndex( this, start, stop, operation ); |
| 280 | 195 | } |
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
public int indexOf( E target ) { |
| 286 | 294 | return ReadOnlySequences.indexOf( this, target ); |
| 287 | |
} |
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
public int indexOf( ReadOnlySequence<? extends E> targetSequence, int start ) { |
| 293 | 54 | return ReadOnlySequences.indexOf( this, targetSequence, start ); |
| 294 | |
} |
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
public void doWithIndex( BinaryFunctor<? super Integer, ? super E, ?> operation ) { |
| 300 | 2359 | ReadOnlySequences.doWithIndex( this, operation ); |
| 301 | 2083 | } |
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
public E last() { |
| 307 | 30 | ensureNotEmpty(); |
| 308 | |
|
| 309 | 29 | return at( size() - 1 ); |
| 310 | |
} |
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
public void add( E newElement ) { |
| 321 | 15348 | addLast( newElement ); |
| 322 | 15348 | } |
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
public boolean remove( E oldElement ) { |
| 328 | 56 | int indexOfOldElement = indexOf( oldElement ); |
| 329 | |
|
| 330 | 56 | if ( indexOfOldElement == -1 ) |
| 331 | 26 | return false; |
| 332 | |
|
| 333 | 30 | removeAt( indexOfOldElement ); |
| 334 | 30 | return true; |
| 335 | |
} |
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
@Override |
| 341 | |
public boolean removeIf( UnaryCondition<? super E> discriminator ) { |
| 342 | 23 | ensureNotNull( discriminator, DISCRIMINATOR ); |
| 343 | |
|
| 344 | 22 | boolean removalOccurred = false; |
| 345 | |
|
| 346 | 22 | int i = 0; |
| 347 | 154 | while ( i < size() ) { |
| 348 | 132 | if ( discriminator.matches( at( i ) ) ) { |
| 349 | 60 | removalOccurred = true; |
| 350 | 60 | removeAt( i ); |
| 351 | |
} |
| 352 | |
else |
| 353 | 72 | ++i; |
| 354 | |
} |
| 355 | |
|
| 356 | 22 | return removalOccurred; |
| 357 | |
} |
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
@Override |
| 363 | |
public boolean retainIf( UnaryCondition<? super E> discriminator ) { |
| 364 | 5 | ensureNotNull( discriminator, DISCRIMINATOR ); |
| 365 | |
|
| 366 | 4 | boolean removalOccurred = false; |
| 367 | |
|
| 368 | 4 | int i = 0; |
| 369 | 28 | while ( i < size() ) { |
| 370 | 24 | if ( discriminator.matches( at( i ) ) ) |
| 371 | 18 | ++i; |
| 372 | |
else { |
| 373 | 6 | removalOccurred = true; |
| 374 | 6 | removeAt( i ); |
| 375 | |
} |
| 376 | |
} |
| 377 | |
|
| 378 | 4 | return removalOccurred; |
| 379 | |
} |
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
public E removeAt( int index ) { |
| 385 | 119 | ensureIndexIsValid( index, "removal" ); |
| 386 | |
|
| 387 | 112 | E removedElement = Casting.<E>cast( storage[ index ] ); |
| 388 | 112 | --numberOfElements; |
| 389 | 112 | arraycopy( storage, index + 1, storage, index, size() - index ); |
| 390 | |
|
| 391 | 112 | return removedElement; |
| 392 | |
} |
| 393 | |
|
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
public E removeFirst() { |
| 398 | 5 | ensureNotEmpty(); |
| 399 | |
|
| 400 | 2 | return removeAt( 0 ); |
| 401 | |
} |
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
public E removeLast() { |
| 407 | 5 | ensureNotEmpty(); |
| 408 | |
|
| 409 | 2 | return removeAt( size() - 1 ); |
| 410 | |
} |
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
public OrderedCollection<E> concat( ReadOnlySequence<? extends E> otherSequence ) { |
| 416 | 15 | return ReadOnlySequences.concat( this, otherSequence ); |
| 417 | |
} |
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
public E after( E target ) { |
| 423 | 69 | int indexOfTarget = ensureIndexOfElementIsNot( target, size() - 1 ); |
| 424 | |
|
| 425 | 39 | return at( indexOfTarget + 1 ); |
| 426 | |
} |
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
public E at( int index ) { |
| 432 | 29397 | ensureIndexIsValid( index, "search" ); |
| 433 | |
|
| 434 | 29382 | return Casting.<E>cast( storage[ index ] ); |
| 435 | |
} |
| 436 | |
|
| 437 | |
|
| 438 | |
|
| 439 | |
|
| 440 | |
public E before( E target ) { |
| 441 | 69 | int indexOfTarget = indexOf( target ); |
| 442 | 69 | if ( indexOfTarget <= 0 ) |
| 443 | 30 | throw new NoSuchElementException( "no element before " + target ); |
| 444 | |
|
| 445 | 39 | return at( indexOfTarget - 1 ); |
| 446 | |
} |
| 447 | |
|
| 448 | |
|
| 449 | |
|
| 450 | |
|
| 451 | |
public OrderedCollection<E> copyRange( int start, int stop ) { |
| 452 | 235 | return ReadOnlySequences.copyRange( this, start, stop ); |
| 453 | |
} |
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
public OrderedCollection<E> copyWith( E newElement ) { |
| 459 | 23 | return ReadOnlySequences.copyWith( this, newElement ); |
| 460 | |
} |
| 461 | |
|
| 462 | |
|
| 463 | |
|
| 464 | |
|
| 465 | |
public OrderedCollection<E> copyWithout( E oldElement ) { |
| 466 | 77 | return ReadOnlySequences.copyWithout( this, oldElement ); |
| 467 | |
} |
| 468 | |
|
| 469 | |
|
| 470 | |
|
| 471 | |
|
| 472 | |
public OrderedCollection<E> reverse() { |
| 473 | 30 | return ReadOnlySequences.reverse( this ); |
| 474 | |
} |
| 475 | |
|
| 476 | |
|
| 477 | |
|
| 478 | |
|
| 479 | |
public <T> void doWithOthers( ReadOnlySequence<? extends T> otherSequence, |
| 480 | |
BinaryFunctor<? super E, ? super T, ?> operation ) { |
| 481 | |
|
| 482 | 33 | ReadOnlySequences.doWithOthers( this, otherSequence, operation ); |
| 483 | 30 | } |
| 484 | |
|
| 485 | |
|
| 486 | |
|
| 487 | |
|
| 488 | |
public E putAt( int index, E newElement ) { |
| 489 | 1662 | ensureIndexIsValid( index, "insertion" ); |
| 490 | |
|
| 491 | 1643 | E previousElement = at( index ); |
| 492 | 1643 | storage[ index ] = newElement; |
| 493 | 1643 | return previousElement; |
| 494 | |
} |
| 495 | |
|
| 496 | |
|
| 497 | |
|
| 498 | |
|
| 499 | |
public void putAtAll( Collection<? extends Integer> indices, final E newElement ) { |
| 500 | 8 | indices.forEachDo( new UnaryFunctor<Integer, Void>() { |
| 501 | 15 | public Void evaluate( Integer argument ) { |
| 502 | 7 | putAt( argument, newElement ); |
| 503 | 3 | return null; |
| 504 | |
} |
| 505 | |
} ); |
| 506 | 3 | } |
| 507 | |
|
| 508 | |
|
| 509 | |
|
| 510 | |
|
| 511 | |
public void putAtAll( int[] indices, E newElement ) { |
| 512 | 12 | for ( int each : indices ) |
| 513 | 8 | putAt( each, newElement ); |
| 514 | 3 | } |
| 515 | |
|
| 516 | |
|
| 517 | |
|
| 518 | |
|
| 519 | |
public void putAtAll( Integer[] indices, E newElement ) { |
| 520 | 12 | for ( int each : indices ) |
| 521 | 8 | putAt( each, newElement ); |
| 522 | 3 | } |
| 523 | |
|
| 524 | |
|
| 525 | |
|
| 526 | |
|
| 527 | |
public void putAtAll( Iterable<? extends Integer> indices, E newElement ) { |
| 528 | 8 | for ( int each : indices ) |
| 529 | 8 | putAt( each, newElement ); |
| 530 | 3 | } |
| 531 | |
|
| 532 | |
|
| 533 | |
|
| 534 | |
|
| 535 | |
public void putAtAll( final E newElement ) { |
| 536 | 2 | doWithIndex( new BinaryFunctor<Integer, E, Void>() { |
| 537 | 4 | public Void evaluate( Integer first, E second ) { |
| 538 | 2 | putAt( first, newElement ); |
| 539 | 2 | return null; |
| 540 | |
} |
| 541 | |
} ); |
| 542 | 2 | } |
| 543 | |
|
| 544 | |
|
| 545 | |
|
| 546 | |
|
| 547 | |
public void replace( int start, int stop, final E replacement ) { |
| 548 | 9 | ensureIndexIsValid( start, STARTING_INDEX ); |
| 549 | 6 | ensureIndexIsValid( stop, ENDING_INDEX ); |
| 550 | |
|
| 551 | 4 | forEachInRangeDoWithIndex( start, stop, new BinaryFunctor<Integer, E, Void>() { |
| 552 | 8 | public Void evaluate( Integer first, E second ) { |
| 553 | 4 | putAt( first, replacement ); |
| 554 | 4 | return null; |
| 555 | |
} |
| 556 | |
} ); |
| 557 | 4 | } |
| 558 | |
|
| 559 | |
|
| 560 | |
|
| 561 | |
|
| 562 | |
public void replaceRange( final int start, int stop, |
| 563 | |
ReadOnlySequence<? extends E> replacements ) { |
| 564 | |
|
| 565 | 11 | ensureNotNull( replacements, REPLACEMENT_SEQUENCE ); |
| 566 | 10 | ensureIndexIsValid( start, STARTING_INDEX ); |
| 567 | 7 | ensureIndexIsValid( stop, ENDING_INDEX ); |
| 568 | |
|
| 569 | 5 | if ( stop >= start ) { |
| 570 | 4 | if ( replacements.size() != stop - start + 1 ) { |
| 571 | 1 | throw new IllegalArgumentException( |
| 572 | |
EXPECTED_SEQUENCE_SIZE |
| 573 | |
+ ( stop - start + 1 ) |
| 574 | |
+ BUT_WAS |
| 575 | |
+ replacements.size() ); |
| 576 | |
} |
| 577 | |
|
| 578 | 3 | replacements.doWithIndex( new BinaryFunctor<Integer, E, Void>() { |
| 579 | 7 | public Void evaluate( Integer first, E second ) { |
| 580 | 4 | putAt( first + start, second ); |
| 581 | 4 | return null; |
| 582 | |
} |
| 583 | |
} ); |
| 584 | |
} |
| 585 | 4 | } |
| 586 | |
|
| 587 | |
|
| 588 | |
|
| 589 | |
|
| 590 | |
public void replaceRange( int start, int stop, |
| 591 | |
final ReadOnlySequence<? extends E> replacements, int replacementStart ) { |
| 592 | |
|
| 593 | 16 | ensureNotNull( replacements, REPLACEMENT_SEQUENCE ); |
| 594 | 15 | ensureIndexIsValid( start, STARTING_INDEX ); |
| 595 | 12 | ensureIndexIsValid( stop, ENDING_INDEX ); |
| 596 | 10 | checkIndexIsValidOn( replacements, replacementStart, "replacement starting" ); |
| 597 | |
|
| 598 | 8 | if ( stop >= start ) { |
| 599 | 7 | if ( replacements.size() != replacementStart + stop - start + 1 ) { |
| 600 | 2 | throw new IllegalArgumentException( |
| 601 | |
"expecting replacement sequence of size " |
| 602 | |
+ ( replacementStart + stop - start + 1 ) |
| 603 | |
+ BUT_WAS |
| 604 | |
+ replacements.size() ); |
| 605 | |
} |
| 606 | |
|
| 607 | 5 | final int[] replacementIndex = { replacementStart }; |
| 608 | |
|
| 609 | 5 | forEachInRangeDoWithIndex( start, stop, |
| 610 | |
new BinaryFunctor<Integer, E, Void>() { |
| 611 | 11 | public Void evaluate( Integer first, E second ) { |
| 612 | 6 | int whereToReplace = replacementIndex[ 0 ]; |
| 613 | 6 | ++replacementIndex[ 0 ]; |
| 614 | 6 | putAt( first, replacements.at( whereToReplace ) ); |
| 615 | 6 | return null; |
| 616 | |
} |
| 617 | |
} |
| 618 | |
); |
| 619 | |
} |
| 620 | 6 | } |
| 621 | |
|
| 622 | |
|
| 623 | |
|
| 624 | |
|
| 625 | |
|
| 626 | |
|
| 627 | |
|
| 628 | |
|
| 629 | |
|
| 630 | |
|
| 631 | |
|
| 632 | |
|
| 633 | |
|
| 634 | |
|
| 635 | |
public void addAfter( E newElement, E target ) { |
| 636 | 3 | int indexOfTarget = ensureElementIsPresent( target ); |
| 637 | 2 | addAfterIndex( newElement, indexOfTarget ); |
| 638 | 2 | } |
| 639 | |
|
| 640 | |
|
| 641 | |
|
| 642 | |
|
| 643 | |
|
| 644 | |
|
| 645 | |
|
| 646 | |
|
| 647 | |
|
| 648 | |
|
| 649 | |
|
| 650 | |
|
| 651 | |
public void addAfterIndex( E newElement, int index ) { |
| 652 | 10 | ensureIndexIsGreaterThan( index, LEFT_BOUND, ADDITION_INDEX ); |
| 653 | 9 | ensureIndexIsLessThan( index, size(), ADDITION_INDEX ); |
| 654 | |
|
| 655 | 8 | if ( index == -1 ) |
| 656 | 2 | addFirst( newElement ); |
| 657 | 6 | else if ( index == size() - 1 ) |
| 658 | 1 | addLast( newElement ); |
| 659 | |
else { |
| 660 | 5 | resizeIfNeeded(); |
| 661 | 5 | arraycopy( storage, index + 1, storage, index + 2, size() - index - 1 ); |
| 662 | |
|
| 663 | 5 | putAt( index + 1, newElement ); |
| 664 | 5 | ++numberOfElements; |
| 665 | |
} |
| 666 | 8 | } |
| 667 | |
|
| 668 | |
|
| 669 | |
|
| 670 | |
|
| 671 | |
|
| 672 | |
|
| 673 | |
|
| 674 | |
|
| 675 | |
|
| 676 | |
|
| 677 | |
|
| 678 | |
|
| 679 | |
|
| 680 | |
|
| 681 | |
public void addBefore( E newElement, E target ) { |
| 682 | 3 | int indexOfTarget = ensureElementIsPresent( target ); |
| 683 | 2 | addBeforeIndex( newElement, indexOfTarget ); |
| 684 | 2 | } |
| 685 | |
|
| 686 | |
|
| 687 | |
|
| 688 | |
|
| 689 | |
|
| 690 | |
|
| 691 | |
|
| 692 | |
|
| 693 | |
|
| 694 | |
|
| 695 | |
|
| 696 | |
|
| 697 | |
public void addBeforeIndex( E newElement, int index ) { |
| 698 | 2483 | ensureIndexIsGreaterThan( index, -1, ADDITION_INDEX ); |
| 699 | 2482 | ensureIndexIsLessThan( index, size() + 1, ADDITION_INDEX ); |
| 700 | |
|
| 701 | 2481 | if ( index == 0 ) |
| 702 | 1039 | addFirst( newElement ); |
| 703 | 1442 | else if ( index == size() ) |
| 704 | 1000 | addLast( newElement ); |
| 705 | |
else { |
| 706 | 442 | resizeIfNeeded(); |
| 707 | 442 | arraycopy( storage, index, storage, index + 1, size() - index ); |
| 708 | |
|
| 709 | 442 | putAt( index, newElement ); |
| 710 | 442 | ++numberOfElements; |
| 711 | |
} |
| 712 | 2481 | } |
| 713 | |
|
| 714 | |
|
| 715 | |
|
| 716 | |
|
| 717 | |
|
| 718 | |
|
| 719 | |
|
| 720 | |
|
| 721 | |
|
| 722 | |
|
| 723 | |
|
| 724 | |
|
| 725 | |
|
| 726 | |
|
| 727 | |
|
| 728 | |
|
| 729 | |
|
| 730 | |
|
| 731 | |
public void addAllAfter( Collection<? extends E> newElements, E target ) { |
| 732 | 6 | int indexOfTarget = ensureElementIsPresent( target ); |
| 733 | 3 | addAllAfterIndex( newElements, indexOfTarget ); |
| 734 | 3 | } |
| 735 | |
|
| 736 | |
|
| 737 | |
|
| 738 | |
|
| 739 | |
|
| 740 | |
|
| 741 | |
|
| 742 | |
|
| 743 | |
|
| 744 | |
public void addAllAfter( E[] newElements, E target ) { |
| 745 | 2 | addAllAfter( orderedCollectionFrom( newElements ), target ); |
| 746 | 1 | } |
| 747 | |
|
| 748 | |
|
| 749 | |
|
| 750 | |
|
| 751 | |
|
| 752 | |
|
| 753 | |
|
| 754 | |
|
| 755 | |
|
| 756 | |
public void addAllAfter( Iterable<? extends E> newElements, E target ) { |
| 757 | 2 | addAllAfter( orderedCollectionFrom( newElements ), target ); |
| 758 | 1 | } |
| 759 | |
|
| 760 | |
|
| 761 | |
|
| 762 | |
|
| 763 | |
|
| 764 | |
|
| 765 | |
|
| 766 | |
|
| 767 | |
|
| 768 | |
|
| 769 | |
|
| 770 | |
|
| 771 | |
|
| 772 | |
|
| 773 | |
|
| 774 | |
|
| 775 | |
|
| 776 | |
public void addAllAfterIndex( Collection<? extends E> newElements, final int index ) { |
| 777 | 18 | ensureIndexIsGreaterThan( index, LEFT_BOUND, ADDITION_INDEX ); |
| 778 | 15 | ensureIndexIsLessThan( index, size(), ADDITION_INDEX ); |
| 779 | |
|
| 780 | 12 | if ( index == -1 ) |
| 781 | 3 | addAllFirst( newElements ); |
| 782 | 9 | else if ( index == size() - 1 ) |
| 783 | 3 | addAllLast( newElements ); |
| 784 | |
else { |
| 785 | 6 | resizeIfNeeded( size() + newElements.size() ); |
| 786 | 6 | arraycopy( storage, index + 1, storage, index + 1 + newElements.size(), |
| 787 | |
size() - index - 1 ); |
| 788 | |
|
| 789 | 6 | final int[] offset = { 0 }; |
| 790 | |
|
| 791 | 6 | newElements.forEachDo( new UnaryFunctor<E, Void>() { |
| 792 | 18 | public Void evaluate( E argument ) { |
| 793 | 12 | storage[ index + 1 + offset[ 0 ] ] = argument; |
| 794 | 12 | ++offset[ 0 ]; |
| 795 | 12 | ++numberOfElements; |
| 796 | 12 | return null; |
| 797 | |
} |
| 798 | |
} ); |
| 799 | |
} |
| 800 | 12 | } |
| 801 | |
|
| 802 | |
|
| 803 | |
|
| 804 | |
|
| 805 | |
|
| 806 | |
|
| 807 | |
|
| 808 | |
|
| 809 | |
|
| 810 | |
public void addAllAfterIndex( E[] newElements, int index ) { |
| 811 | 5 | addAllAfterIndex( orderedCollectionFrom( newElements ), index ); |
| 812 | 3 | } |
| 813 | |
|
| 814 | |
|
| 815 | |
|
| 816 | |
|
| 817 | |
|
| 818 | |
|
| 819 | |
|
| 820 | |
|
| 821 | |
|
| 822 | |
public void addAllAfterIndex( Iterable<? extends E> newElements, int index ) { |
| 823 | 5 | addAllAfterIndex( orderedCollectionFrom( newElements ), index ); |
| 824 | 3 | } |
| 825 | |
|
| 826 | |
|
| 827 | |
|
| 828 | |
|
| 829 | |
|
| 830 | |
|
| 831 | |
|
| 832 | |
|
| 833 | |
|
| 834 | |
|
| 835 | |
|
| 836 | |
|
| 837 | |
|
| 838 | |
|
| 839 | |
|
| 840 | |
|
| 841 | |
|
| 842 | |
|
| 843 | |
public void addAllBefore( Collection<? extends E> newElements, E target ) { |
| 844 | 6 | int indexOfTarget = ensureElementIsPresent( target ); |
| 845 | 3 | addAllBeforeIndex( newElements, indexOfTarget ); |
| 846 | 3 | } |
| 847 | |
|
| 848 | |
|
| 849 | |
|
| 850 | |
|
| 851 | |
|
| 852 | |
|
| 853 | |
|
| 854 | |
|
| 855 | |
|
| 856 | |
public void addAllBefore( E[] newElements, E target ) { |
| 857 | 2 | addAllBefore( orderedCollectionFrom( newElements ), target ); |
| 858 | 1 | } |
| 859 | |
|
| 860 | |
|
| 861 | |
|
| 862 | |
|
| 863 | |
|
| 864 | |
|
| 865 | |
|
| 866 | |
|
| 867 | |
|
| 868 | |
public void addAllBefore( Iterable<? extends E> newElements, E target ) { |
| 869 | 2 | addAllBefore( orderedCollectionFrom( newElements ), target ); |
| 870 | 1 | } |
| 871 | |
|
| 872 | |
|
| 873 | |
|
| 874 | |
|
| 875 | |
|
| 876 | |
|
| 877 | |
|
| 878 | |
|
| 879 | |
|
| 880 | |
|
| 881 | |
|
| 882 | |
|
| 883 | |
|
| 884 | |
|
| 885 | |
|
| 886 | |
|
| 887 | |
|
| 888 | |
|
| 889 | |
public void addAllBeforeIndex( Collection<? extends E> newElements, |
| 890 | |
final int index ) { |
| 891 | |
|
| 892 | 24 | ensureIndexIsGreaterThan( index, -1, ADDITION_INDEX ); |
| 893 | 21 | ensureIndexIsLessThan( index, size() + 1, ADDITION_INDEX ); |
| 894 | |
|
| 895 | 18 | if ( index == 0 ) |
| 896 | 12 | addAllFirst( newElements ); |
| 897 | 6 | else if ( index == size() ) |
| 898 | 3 | addAllLast( newElements ); |
| 899 | |
else { |
| 900 | 3 | resizeIfNeeded( size() + newElements.size() ); |
| 901 | 3 | arraycopy( storage, index, storage, index + newElements.size(), |
| 902 | |
size() - index ); |
| 903 | |
|
| 904 | 3 | final int[] offset = { 0 }; |
| 905 | |
|
| 906 | 3 | newElements.forEachDo( new UnaryFunctor<E, Void>() { |
| 907 | 9 | public Void evaluate( E argument ) { |
| 908 | 6 | storage[ index + offset[ 0 ] ] = argument; |
| 909 | 6 | ++offset[ 0 ]; |
| 910 | 6 | ++numberOfElements; |
| 911 | 6 | return null; |
| 912 | |
} |
| 913 | |
} ); |
| 914 | |
} |
| 915 | 18 | } |
| 916 | |
|
| 917 | |
|
| 918 | |
|
| 919 | |
|
| 920 | |
|
| 921 | |
|
| 922 | |
|
| 923 | |
|
| 924 | |
|
| 925 | |
public void addAllBeforeIndex( E[] newElements, int index ) { |
| 926 | 5 | addAllBeforeIndex( orderedCollectionFrom( newElements ), index ); |
| 927 | 3 | } |
| 928 | |
|
| 929 | |
|
| 930 | |
|
| 931 | |
|
| 932 | |
|
| 933 | |
|
| 934 | |
|
| 935 | |
|
| 936 | |
|
| 937 | |
public void addAllBeforeIndex( Iterable<? extends E> newElements, int index ) { |
| 938 | 5 | addAllBeforeIndex( orderedCollectionFrom( newElements ), index ); |
| 939 | 3 | } |
| 940 | |
|
| 941 | |
|
| 942 | |
|
| 943 | |
|
| 944 | |
|
| 945 | |
|
| 946 | |
|
| 947 | |
|
| 948 | |
|
| 949 | |
|
| 950 | |
|
| 951 | |
|
| 952 | |
|
| 953 | |
|
| 954 | |
public void addAllFirst( Collection<? extends E> newElements ) { |
| 955 | 403 | ensureNotNull( newElements, "new elements" ); |
| 956 | |
|
| 957 | 402 | if ( !newElements.isEmpty() ) { |
| 958 | 402 | resizeIfNeeded( size() + newElements.size() ); |
| 959 | 402 | arraycopy( storage, 0, storage, newElements.size(), size() ); |
| 960 | |
} |
| 961 | |
|
| 962 | 402 | final int[] index = { 0 }; |
| 963 | 402 | newElements.forEachDo( new UnaryFunctor<E, Void>() { |
| 964 | 1699 | public Void evaluate( E argument ) { |
| 965 | 1297 | storage[ index[ 0 ] ] = argument; |
| 966 | 1297 | ++index[ 0 ]; |
| 967 | 1297 | numberOfElements++; |
| 968 | 1297 | return null; |
| 969 | |
} |
| 970 | |
} ); |
| 971 | 402 | } |
| 972 | |
|
| 973 | |
|
| 974 | |
|
| 975 | |
|
| 976 | |
|
| 977 | |
|
| 978 | |
public void addAllFirst( E... newElements ) { |
| 979 | 129 | addAllFirst( orderedCollectionFrom( newElements ) ); |
| 980 | 129 | } |
| 981 | |
|
| 982 | |
|
| 983 | |
|
| 984 | |
|
| 985 | |
|
| 986 | |
|
| 987 | |
public void addAllFirst( Iterable<? extends E> newElements ) { |
| 988 | 129 | addAllFirst( orderedCollectionFrom( newElements ) ); |
| 989 | 129 | } |
| 990 | |
|
| 991 | |
|
| 992 | |
|
| 993 | |
|
| 994 | |
|
| 995 | |
|
| 996 | |
|
| 997 | |
|
| 998 | |
|
| 999 | |
|
| 1000 | |
|
| 1001 | |
|
| 1002 | |
|
| 1003 | |
public void addAllLast( Collection<? extends E> newElements ) { |
| 1004 | 135 | addAll( newElements ); |
| 1005 | 135 | } |
| 1006 | |
|
| 1007 | |
|
| 1008 | |
|
| 1009 | |
|
| 1010 | |
|
| 1011 | |
|
| 1012 | |
public void addAllLast( E... newElements ) { |
| 1013 | 129 | addAll( newElements ); |
| 1014 | 129 | } |
| 1015 | |
|
| 1016 | |
|
| 1017 | |
|
| 1018 | |
|
| 1019 | |
|
| 1020 | |
|
| 1021 | |
public void addAllLast( Iterable<? extends E> newElements ) { |
| 1022 | 129 | addAll( newElements ); |
| 1023 | 129 | } |
| 1024 | |
|
| 1025 | |
|
| 1026 | |
|
| 1027 | |
|
| 1028 | |
|
| 1029 | |
|
| 1030 | |
|
| 1031 | |
|
| 1032 | |
public void addFirst( E newElement ) { |
| 1033 | 1074 | resizeIfNeeded(); |
| 1034 | 1074 | arraycopy( storage, 0, storage, 1, size() ); |
| 1035 | 1074 | ++numberOfElements; |
| 1036 | 1074 | putAt( 0, newElement ); |
| 1037 | 1074 | } |
| 1038 | |
|
| 1039 | |
|
| 1040 | |
|
| 1041 | |
|
| 1042 | |
|
| 1043 | |
|
| 1044 | |
|
| 1045 | |
public void addLast( E newElement ) { |
| 1046 | 16382 | resizeIfNeeded(); |
| 1047 | 16382 | storage[ numberOfElements ] = newElement; |
| 1048 | 16382 | ++numberOfElements; |
| 1049 | 16382 | } |
| 1050 | |
|
| 1051 | |
|
| 1052 | |
|
| 1053 | |
|
| 1054 | |
@Override |
| 1055 | |
public boolean equals( Object that ) { |
| 1056 | 1937 | return ReadOnlySequences.areSequencesEqual( this, that ); |
| 1057 | |
} |
| 1058 | |
|
| 1059 | |
|
| 1060 | |
|
| 1061 | |
|
| 1062 | |
@Override |
| 1063 | |
public int hashCode() { |
| 1064 | 90 | return ReadOnlySequences.hashCode( this ); |
| 1065 | |
} |
| 1066 | |
|
| 1067 | |
|
| 1068 | |
|
| 1069 | |
|
| 1070 | |
@Override |
| 1071 | |
public String toString() { |
| 1072 | 5 | return ReadOnlySequences.toString( this ); |
| 1073 | |
} |
| 1074 | |
|
| 1075 | |
|
| 1076 | |
|
| 1077 | |
|
| 1078 | |
@Override |
| 1079 | |
protected <T> Set<T> newEmptySet() { |
| 1080 | 12 | return Set.emptySet(); |
| 1081 | |
} |
| 1082 | |
|
| 1083 | |
|
| 1084 | |
|
| 1085 | |
|
| 1086 | |
@Override |
| 1087 | |
protected OrderedCollection<E> newEmptyExtensibleCollection() { |
| 1088 | 50 | return newEmptyExtensibleResultCollection(); |
| 1089 | |
} |
| 1090 | |
|
| 1091 | |
|
| 1092 | |
|
| 1093 | |
|
| 1094 | |
@Override |
| 1095 | |
protected <R> OrderedCollection<R> newEmptyExtensibleResultCollection() { |
| 1096 | 63 | return emptyOrderedCollection(); |
| 1097 | |
} |
| 1098 | |
|
| 1099 | |
private void ensureNotEmpty() { |
| 1100 | 157 | if ( isEmpty() ) |
| 1101 | 8 | throw new NoSuchElementException( EMPTY_SEQUENCE ); |
| 1102 | 149 | } |
| 1103 | |
|
| 1104 | |
private int ensureIndexOfElementIsNot( E target, int badIndex ) { |
| 1105 | 87 | int indexOfTarget = indexOf( target ); |
| 1106 | |
|
| 1107 | 87 | if ( indexOfTarget == badIndex ) |
| 1108 | 38 | throw new NoSuchElementException( String.valueOf( target ) ); |
| 1109 | |
|
| 1110 | 49 | return indexOfTarget; |
| 1111 | |
} |
| 1112 | |
|
| 1113 | |
private int ensureElementIsPresent( E target ) { |
| 1114 | 18 | return ensureIndexOfElementIsNot( target, -1 ); |
| 1115 | |
} |
| 1116 | |
|
| 1117 | |
private void ensureIndexIsValid( int index, String failureMessage ) { |
| 1118 | 31237 | checkIndexIsValidOn( this, index, failureMessage ); |
| 1119 | 31181 | } |
| 1120 | |
|
| 1121 | |
private void resizeIfNeeded() { |
| 1122 | 17903 | resizeIfNeeded( size() ); |
| 1123 | 17903 | } |
| 1124 | |
|
| 1125 | |
private void resizeIfNeeded( int magnitude ) { |
| 1126 | 18314 | if ( magnitude >= storage.length ) { |
| 1127 | 33 | Object[] oldStorage = storage; |
| 1128 | 33 | int newCapacity = max( DEFAULT_INITIAL_CAPACITY, magnitude * 2 ); |
| 1129 | 33 | storage = new Object[ newCapacity ]; |
| 1130 | 33 | arraycopy( oldStorage, 0, storage, 0, oldStorage.length ); |
| 1131 | |
} |
| 1132 | 18314 | } |
| 1133 | |
|
| 1134 | |
private void writeObject( ObjectOutputStream output ) throws IOException { |
| 1135 | 4 | output.defaultWriteObject(); |
| 1136 | |
|
| 1137 | 19 | for ( int i = 0; i < numberOfElements; ++i ) |
| 1138 | 15 | output.writeObject( storage[ i ] ); |
| 1139 | 4 | } |
| 1140 | |
|
| 1141 | |
private void readObject( ObjectInputStream input ) |
| 1142 | |
throws IOException, ClassNotFoundException { |
| 1143 | |
|
| 1144 | 4 | input.defaultReadObject(); |
| 1145 | |
|
| 1146 | 4 | storage = new Object[ numberOfElements ]; |
| 1147 | |
|
| 1148 | 19 | for ( int i = 0; i < numberOfElements; ++i ) |
| 1149 | 15 | storage[ i ] = input.readObject(); |
| 1150 | 4 | } |
| 1151 | |
} |