1
1
package net .danielhildebrandt ;
2
2
3
- import java .util .ArrayList ;
4
3
import java .util .Arrays ;
5
4
import java .util .Collection ;
6
5
import java .util .Comparator ;
7
6
import java .util .HashSet ;
8
7
import java .util .Iterator ;
9
- import java .util .List ;
10
8
import java .util .Set ;
11
9
12
10
/**
@@ -381,15 +379,38 @@ public static final <E> E removeLast(E[] arr, E emptyElem, E removed)
381
379
}
382
380
383
381
/**
382
+ * Removes all instances of each listed item from the array. Returns an array
383
+ * of all elements successfully removed - this will be a subset of
384
+ * {@code removed}, in no particular order.
385
+ * <p>
386
+ * If the array of elements to be removed is empty, this method will return an
387
+ * empty array, having removed nothing and throwing no exceptions. If any of
388
+ * the elements of the removal array are the empty element, this method will
389
+ * behave as though they weren't there. Inlcusion of the same element many
390
+ * times will have no additional effect. The "empty element" is the element
391
+ * used in the array to represent an empty spot - this is often simply
392
+ * {@code null}, but can also be a pseudo-empty element if {@code null} is to
393
+ * be considered a valid element.
394
+ * <p>
395
+ * This method ensures that the array is complete both before and after its
396
+ * execution. Completeness is defined by the array in question having all
397
+ * instances of the empty element placed only at its trailing end. Refer also
398
+ * to {@link #isComplete(Object[], Object) isComplete}.
384
399
*
400
+ * @param arr the array from which to remove zero or more elements
401
+ * @param emptyElem the element representing "nothing" in the array
402
+ * @param removed an array of elements to remove
403
+ * @return an array of the elements successfully removed (a subset of
404
+ * {@code removed})
405
+ * @throws IllegalArgumentException if the array to remove from is null
406
+ * @throws IncompleteArrayException if the array to remove from is incomplete
407
+ * @throws NullPointerException if the array from which to remove or the array
408
+ * of element to take out is a null reference
385
409
*
386
- * @param arr
387
- * @param emptyElem
388
- * @param removed
389
- * @return
410
+ * @see #isComplete(Object[], Object)
390
411
*/
391
412
@ SafeVarargs
392
- public static final <E > E [] removeAll (E [] arr , E emptyElem , E ... removed )
413
+ public static final <E > Object [] removeAll (E [] arr , E emptyElem , E ... removed )
393
414
{
394
415
if (arr == null )
395
416
throw new NullPointerException ("The array removed from cannot be a null reference." );
@@ -403,37 +424,56 @@ else if(!isComplete(arr, emptyElem))
403
424
throw new IncompleteArrayException (arr , emptyElem );
404
425
else {
405
426
Set <E > removalSet = new HashSet <E >(Arrays .asList (removed ));
427
+ Set <E > removedElemSet = new HashSet <E >();
406
428
407
- // For each element set to be removed (except empty)...
408
429
for (E elem : removalSet ) {
409
- if (!elem .equals (emptyElem )) {
410
- // Write over each instance of elem, dragging over subsequent elements
430
+ if (((elem != null ) ? (!elem .equals (emptyElem )) : (elem != emptyElem )) && !removedElemSet .contains (elem )) {
411
431
for (int i = 0 ; i < arr .length ; ++i ) {
412
- if ((emptyElem == null && arr [i ] == emptyElem ) || ( emptyElem != null && arr [i ].equals (emptyElem )))
432
+ if ((emptyElem == null ) ? ( arr [i ] == emptyElem ) : ( arr [i ].equals (emptyElem )))
413
433
break ;
414
- else if ((elem != null && arr [i ].equals (elem )) || ( elem == null && arr [i ] == elem )) {
434
+ else if ((elem != null ) ? ( arr [i ].equals (elem )) : ( arr [i ] == elem )) {
415
435
for (int j = i ; j < arr .length - 1 ; ++j )
416
436
arr [j ] = arr [j + 1 ];
417
437
arr [arr .length - 1 ] = emptyElem ;
438
+ removedElemSet .add (elem );
418
439
--i ;
419
440
}
420
441
}
421
442
}
422
443
}
423
444
424
- return null ;
445
+ return removedElemSet . toArray () ;
425
446
}
426
447
}
427
448
428
449
/**
450
+ * Removes all instances of each listed item from the array. Returns a
451
+ * collection of all elements successfully removed - this will be a subset of
452
+ * {@code removed}, in no particular order.
453
+ * <p>
454
+ * If the collection of elements to be removed is empty, this method will
455
+ * return an empty collection, having removed nothing and throwing no
456
+ * exceptions. If any of the elements of the removal collection are the empty
457
+ * element, this method will behave as though they weren't there. Inlcusion of
458
+ * the same element many times will have no additional effect. The "empty
459
+ * element" is the element used in the array to represent an empty spot - this
460
+ * is often simply {@code null}, but can also be a pseudo-empty element if
461
+ * {@code null} is to be considered a valid element.
462
+ * <p>
463
+ * This method ensures that the array is complete both before and after its
464
+ * execution. Completeness is defined by the array in question having all
465
+ * instances of the empty element placed only at its trailing end. Refer also
466
+ * to {@link #isComplete(Object[], Object) isComplete}.
429
467
*
468
+ * @param arr arr the array from which to remove zero or more elements
469
+ * @param emptyElem the element representing "nothing" in the array
470
+ * @param removed a collection of elements to remove
471
+ * @return a {@code Collection} of the elements successfully removed (a subset
472
+ * of {@code removed})
430
473
*
431
- * @param arr
432
- * @param emptyElem
433
- * @param removed
434
- * @return
474
+ * @see #isComplete(Object[], Object)
435
475
*/
436
- public static final <E > List <E > removeAll (E [] arr , E emptyElem , Collection <? extends E > removed )
476
+ public static final <E > Collection <E > removeAll (E [] arr , E emptyElem , Collection <? extends E > removed )
437
477
{
438
478
if (arr == null )
439
479
throw new NullPointerException ("The array removed from cannot be a null reference." );
@@ -442,11 +482,10 @@ else if(removed == null)
442
482
else if (arr .length == 0 )
443
483
throw new IllegalArgumentException ("The array removed from must be non-empty." );
444
484
else if (removed .size () == 0 )
445
- return new ArrayList <E >(0 );
485
+ return new HashSet <E >();
446
486
else if (!isComplete (arr , emptyElem ))
447
487
throw new IncompleteArrayException (arr , emptyElem );
448
488
else {
449
- // Set<E> removalSet = new HashSet<E>(removed);
450
489
return null ;
451
490
}
452
491
}
0 commit comments