Skip to content

Commit d4dbb01

Browse files
committed
add new API for arrays
1 parent bc10c1e commit d4dbb01

File tree

2 files changed

+71
-4
lines changed
  • rlib-common/src

2 files changed

+71
-4
lines changed

rlib-common/src/main/java/com/ss/rlib/common/util/array/Array.java

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ public interface Array<E> extends Collection<E>, Serializable, Reusable, Cloneab
124124
return ArrayFactory::newConcurrentStampedLockArray;
125125
}
126126

127+
/**
128+
* Copy all elements from this array to a target array.
129+
*
130+
* @param target the target array.
131+
*/
132+
default void copyTo(@NotNull Array<? super E> target) {
133+
target.addAll(this);
134+
}
135+
127136
/**
128137
* Adds all elements from the array to this array.
129138
*
@@ -418,8 +427,7 @@ default int lastIndexOf(@NotNull Object object) {
418427
}
419428

420429
/**
421-
* Removes all of this target's elements that are also contained in the specified array (optional operation). After
422-
* this call returns, this array will contain no elements in common with the specified array.
430+
* Removes all of this target's elements that are also contained in the specified array (optional operation).
423431
*
424432
* @param target array containing elements to be removed from this array.
425433
* @return true if this array changed as a result of the call.
@@ -432,15 +440,48 @@ default boolean removeAll(@NotNull Array<?> target) {
432440

433441
int count = 0;
434442

435-
for (Object element : target.array()) {
443+
for (var element : target.array()) {
436444
if (element == null) {
437445
break;
438446
} else if (slowRemove(element)) {
439447
count++;
440448
}
441449
}
442450

443-
return count == target.size();
451+
return count > 0;
452+
}
453+
454+
/**
455+
* Removes all of this target's elements that are also contained in the specified array (optional operation)
456+
* with reordering.
457+
*
458+
* @param target array containing elements to be removed from this array.
459+
* @return true if this array changed as a result of the call.
460+
*/
461+
default boolean fastRemoveAll(@NotNull Array<?> target) {
462+
463+
if (target.isEmpty()) {
464+
return false;
465+
}
466+
467+
var count = 0;
468+
var array = array();
469+
470+
for (int i = 0, length = size(); i < length; i++) {
471+
472+
var element = array[i];
473+
474+
if (!target.contains(element)) {
475+
continue;
476+
}
477+
478+
fastRemove(i);
479+
i--;
480+
length--;
481+
count++;
482+
}
483+
484+
return count > 0;
444485
}
445486

446487
@Override

rlib-common/src/test/java/com/ss/rlib/common/test/util/array/ArrayTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,32 @@ void forEachTest() {
424424
Assertions.assertEquals(array.size(), counter.getAndSet(0));
425425
}
426426

427+
@Test
428+
void fastRemoveAllTest() {
429+
430+
var array = ArrayFactory.asArray("First", "Second", "Third", " ", "55", "66", "22", "22", "11");
431+
var toRemove = ArrayFactory.asArray("First", "Third", "66", "22");
432+
var result = ArrayFactory.asArray("Second", " ", "55", "11");
433+
result.sort(String::compareTo);
434+
435+
array.fastRemoveAll(toRemove);
436+
array.sort(String::compareTo);
437+
438+
Assertions.assertEquals(result, array);
439+
}
440+
441+
@Test
442+
void copyToTest() {
443+
444+
var toCopy = ArrayFactory.asArray("123", "321", "555");
445+
var toCombine = ArrayFactory.asArray("First", "Second");
446+
var result = ArrayFactory.asArray("First", "Second", "123", "321", "555");
447+
448+
toCopy.copyTo(toCombine);
449+
450+
Assertions.assertEquals(result, toCombine);
451+
}
452+
427453
//FIXME OLD TESTS
428454

429455
@Test

0 commit comments

Comments
 (0)