Skip to content

Commit 0e0327e

Browse files
committed
fix Array.of() and add new removeIf methods
1 parent 4a6a80f commit 0e0327e

6 files changed

Lines changed: 107 additions & 13 deletions

File tree

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ repositories {
1616
}
1717
1818
dependencies {
19-
compile 'com.spaceshift:rlib.common:9.5.0'
20-
compile 'com.spaceshift:rlib.fx:9.5.0'
21-
compile 'com.spaceshift:rlib.network:9.5.0'
22-
compile 'com.spaceshift:rlib.mail:9.5.0'
23-
compile 'com.spaceshift:rlib.testcontainers:9.5.0'
19+
compile 'com.spaceshift:rlib.common:9.6.0'
20+
compile 'com.spaceshift:rlib.fx:9.6.0'
21+
compile 'com.spaceshift:rlib.network:9.6.0'
22+
compile 'com.spaceshift:rlib.mail:9.6.0'
23+
compile 'com.spaceshift:rlib.testcontainers:9.6.0'
2424
}
2525
```
2626

@@ -41,27 +41,27 @@ dependencies {
4141
<dependency>
4242
<groupId>com.spaceshift</groupId>
4343
<artifactId>rlib.common</artifactId>
44-
<version>9.5.0</version>
44+
<version>9.6.0</version>
4545
</dependency>
4646
<dependency>
4747
<groupId>com.spaceshift</groupId>
4848
<artifactId>rlib.fx</artifactId>
49-
<version>9.5.0</version>
49+
<version>9.6.0</version>
5050
</dependency>
5151
<dependency>
5252
<groupId>com.spaceshift</groupId>
5353
<artifactId>rlib.network</artifactId>
54-
<version>9.5.0</version>
54+
<version>9.6.0</version>
5555
</dependency>
5656
<dependency>
5757
<groupId>com.spaceshift</groupId>
5858
<artifactId>rlib.mail</artifactId>
59-
<version>9.5.0</version>
59+
<version>9.6.0</version>
6060
</dependency>
6161
<dependency>
6262
<groupId>com.spaceshift</groupId>
6363
<artifactId>rlib.testcontainers</artifactId>
64-
<version>9.5.0</version>
64+
<version>9.6.0</version>
6565
</dependency>
6666

6767
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
}
99
}
1010

11-
rootProject.version = '9.5.0'
11+
rootProject.version = '9.6.0'
1212
group = 'com.spaceshift'
1313

1414
allprojects {

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.util.function.Consumer;
1616
import java.util.function.Function;
1717
import java.util.function.Predicate;
18-
import java.util.function.Supplier;
1918
import java.util.stream.Stream;
2019

2120
/**
@@ -56,7 +55,7 @@ public interface Array<E> extends Collection<E>, Serializable, Reusable, Cloneab
5655
* @return the new read only array.
5756
*/
5857
static <T> @NotNull ReadOnlyArray<T> of(@NotNull Array<T> another) {
59-
return ArrayFactory.newReadOnlyArray(ArrayUtils.copyOf(another.array(), 0, another.size()));
58+
return ArrayFactory.newReadOnlyArray(Arrays.copyOf(another.array(), another.size()));
6059
}
6160

6261
/**
@@ -723,6 +722,40 @@ default <A, B> boolean removeIf(
723722
return removed > 0;
724723
}
725724

725+
/**
726+
* Removes all of the elements of this collection that satisfy the given predicate.
727+
*
728+
* @param argument the additional argument.
729+
* @param converter the converter of the elements.
730+
* @param filter the predicate which returns {@code true} for elements to be removed.
731+
* @param <A> the argument's type.
732+
* @param <B> the element converted type.
733+
* @return {@code true} if any elements were removed.
734+
*/
735+
default <A, B> boolean removeConvertedIf(
736+
@NotNull A argument,
737+
@NotNull NotNullFunction<? super E, B> converter,
738+
@NotNull NotNullBiPredicate<A, B> filter
739+
) {
740+
741+
var array = array();
742+
var removed = 0;
743+
744+
for (int i = 0, length = size(); i < length; i++) {
745+
746+
var element = array[i];
747+
748+
if (filter.test(argument, converter.apply(element))) {
749+
remove(i);
750+
i--;
751+
length--;
752+
removed++;
753+
}
754+
}
755+
756+
return removed > 0;
757+
}
758+
726759
/**
727760
* Return true if there is at least an element for the condition.
728761
*

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,4 +513,27 @@ default <A, B> boolean removeIfInWriteLock(
513513
writeUnlock(stamp);
514514
}
515515
}
516+
517+
/**
518+
* Removes all of the elements of this collection that satisfy the given predicate.
519+
*
520+
* @param argument the additional argument.
521+
* @param converter the converter of the elements.
522+
* @param filter the predicate which returns {@code true} for elements to be removed.
523+
* @param <A> the argument's type.
524+
* @param <B> the element converted type.
525+
* @return {@code true} if any elements were removed.
526+
*/
527+
default <A, B> boolean removeConvertedIfInWriteLock(
528+
@NotNull A argument,
529+
@NotNull NotNullFunction<? super E, B> converter,
530+
@NotNull NotNullBiPredicate<A, B> filter
531+
) {
532+
var stamp = writeLock();
533+
try {
534+
return removeConvertedIf(argument, converter, filter);
535+
} finally {
536+
writeUnlock(stamp);
537+
}
538+
}
516539
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@
1818
*/
1919
public class ArrayTest extends BaseTest {
2020

21+
@Test
22+
void ofTest() {
23+
24+
var array = ArrayFactory.asArray("First", "Second", "Third", " ");
25+
var copy = Array.of(array);
26+
27+
Assertions.assertEquals(array, copy);
28+
29+
var array2 = Array.of("First", "Second", "Third", " ");
30+
31+
Assertions.assertEquals(array, array2);
32+
33+
var single = ArrayFactory.asArray("First");
34+
var single2 = Array.of("First");
35+
36+
Assertions.assertEquals(single, single2);
37+
}
38+
2139
@Test
2240
void removeIfTest() {
2341

@@ -59,6 +77,16 @@ void removeIfTest() {
5977
));
6078

6179
Assertions.assertEquals(2, array.size());
80+
81+
array = ArrayFactory.asArray("10", "5", "2", "1");
82+
83+
Assertions.assertTrue(array.removeConvertedIf(
84+
5,
85+
Integer::parseInt,
86+
Integer::equals
87+
));
88+
89+
Assertions.assertEquals(3, array.size());
6290
}
6391

6492
@Test

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ void removeIfInWriteLockTest() {
5353
));
5454

5555
Assertions.assertEquals(2, array.size());
56+
57+
array = ConcurrentArray.of("10", "5", "2", "1");
58+
59+
Assertions.assertTrue(array.removeConvertedIfInWriteLock(
60+
5,
61+
Integer::parseInt,
62+
Integer::equals
63+
));
64+
65+
Assertions.assertEquals(3, array.size());
5666
}
5767

5868
@Test

0 commit comments

Comments
 (0)