Skip to content

Commit 43c2290

Browse files
committed
15: Implemented reject method
1 parent 8fa50a9 commit 43c2290

4 files changed

Lines changed: 94 additions & 0 deletions

File tree

readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* [.select](#select)
2929
* [.map](#map)
3030
* [.count](#count)
31+
* [.reject](#reject)
3132

3233
* [How to contribute?](#how-to-contribute)
3334

@@ -77,6 +78,7 @@ enumerable4j (MIT) | Java 8 | [cactoos](https://github.com/yegor256/cactoos) (MI
7778
`.any(...)` | `.stream().anyMatch(...);` | `new Or<>(...,...).value()`| tbd |
7879
`.none(...)` | `.stream().noneMatch(...);` | `new And<>(...,...).value()`| tbd |
7980
`.select(...)` | `.stream().filter(...).collect(Collectors.toList())` | `new Filtered<>(...,...)` | tbd |
81+
`.reject(...)` | `.stream().filter((...).negate()).collect(Collectors.toList())` | `new Filtered<>(...,...)` | tbd |
8082
`.map(...)` | `.stream().map(...).collect(Collectors.toList())` | `new Mapped<>(...,...)` | tbd |
8183
`.count(...)` | `.stream().filter(...).count()` | `-` | tbd |
8284

@@ -108,6 +110,13 @@ Enumerable<Integer> src = new EnumerableOf<>(-1, 1, 2); // [java.util.Collection
108110
Enumerable<Integer> positive = src.select(v -> v > 0); // [1, 2]
109111
```
110112

113+
#### .reject
114+
115+
```java
116+
Enumerable<Integer> src = new EnumerableOf<>(-1, 1, 2); // [java.util.Collection] => [-1, 1, 2]
117+
Enumerable<Integer> negative = src.reject(v -> v > 0); // [-1]
118+
```
119+
111120
#### .map
112121

113122
```java

src/main/java/io/github/dgroup/enumerable4j/Enumerable.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ public interface Enumerable<T> extends Collection<T> {
7272
*/
7373
Enumerable<T> select(Predicate<T> prd);
7474

75+
/**
76+
* Returns an enumerable containing all elements of enumerable for which the given function
77+
* returns a false value.
78+
* If no function (null) is given, then 'this' is returned instead.
79+
* @param prd The function to match each element.
80+
* @return The enumerable.
81+
*/
82+
Enumerable<T> reject(Predicate<T> prd);
83+
7584
/**
7685
* Returns an enumerable containing all elements, on which given function was applied.
7786
* If no function (null) is given, then 'this' is returned instead.

src/main/java/io/github/dgroup/enumerable4j/EnumerableOf.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ public final Enumerable<T> select(final Predicate<T> prd) {
100100
return out;
101101
}
102102

103+
@Override
104+
public final Enumerable<T> reject(final Predicate<T> prd) {
105+
final Enumerable<T> out;
106+
if (prd == null) {
107+
out = this;
108+
} else {
109+
out = new EnumerableOf<>(
110+
this.stream().filter(prd.negate()).collect(Collectors.toList())
111+
);
112+
}
113+
return out;
114+
}
115+
103116
@Override
104117
public final <Y> Enumerable<Y> map(final Function<? super T, ? extends Y> fnc) {
105118
final Enumerable<Y> out;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2019-2021 Yurii Dubinka
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"),
8+
* to deal in the Software without restriction, including without limitation
9+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
* and/or sell copies of the Software, and to permit persons to whom
11+
* the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22+
* OR OTHER DEALINGS IN THE SOFTWARE.
23+
*/
24+
25+
package io.github.dgroup.enumerable4j;
26+
27+
import org.hamcrest.core.AllOf;
28+
import org.junit.jupiter.api.Test;
29+
import org.llorllale.cactoos.matchers.Assertion;
30+
import org.llorllale.cactoos.matchers.HasSize;
31+
import org.llorllale.cactoos.matchers.HasValues;
32+
33+
/**
34+
* Test cases for {@link EnumerableOf#reject}.
35+
*
36+
* @since 0.1.0
37+
* @checkstyle MagicNumberCheck (500 lines)
38+
* @checkstyle JavadocMethodCheck (500 lines)
39+
*/
40+
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
41+
final class RejectTest {
42+
43+
@Test
44+
void reject() {
45+
new Assertion<>(
46+
"Negative values from enumerable found",
47+
new EnumerableOf<>(1, 2, 3, -1).reject(val -> val > 0),
48+
new AllOf<>(
49+
new HasSize(1),
50+
new HasValues<>(-1)
51+
)
52+
).affirm();
53+
}
54+
55+
@Test
56+
void nullFunction() {
57+
new Assertion<>(
58+
"In case null-function the self enumerable is expected",
59+
new EnumerableOf<>(3, 0, 2, -1).select(null),
60+
new HasValues<>(3, 0, 2, -1)
61+
).affirm();
62+
}
63+
}

0 commit comments

Comments
 (0)