Skip to content

Commit b3458b0

Browse files
authored
Merge pull request #6 from dykov/add-next-method
Add '.next' method
2 parents 24048de + 4b00c8d commit b3458b0

File tree

4 files changed

+155
-1
lines changed

4 files changed

+155
-1
lines changed

readme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* [.find](#find)
3333
* [.reduce](#reduce)
3434
* [.after](#after)
35+
* [.next](#next)
3536

3637
* [How to contribute?](#how-to-contribute)
3738

@@ -185,6 +186,27 @@ public interface Enumerable<X> extends Collection<X> {
185186
default Enumerable<X> after(Predicate<X> prd, long size) {
186187
// ...
187188
}
189+
190+
/**
191+
* Returns the next element of enumerable after the first one which corresponds the condition.
192+
* If no predicate (null) is given, or no element found then alternative is returned instead.
193+
* @param prd The function to match element after which enumerable element should be returned.
194+
* @return The next element of enumerable after the first one which corresponds the condition.
195+
*/
196+
default X next(Predicate<X> prd) {
197+
// ...
198+
}
199+
200+
/**
201+
* Returns the next element of enumerable after the first one which corresponds the condition.
202+
* If no predicate (null) is given, or no element found then alternative is returned instead.
203+
* @param prd The function to match element after which enumerable element should be returned.
204+
* @param alt The alternative to return in case of null predicate or no element found.
205+
* @return The next element of enumerable after the first one which corresponds the condition.
206+
*/
207+
default X next(Predicate<X> prd, X alt) {
208+
// ...
209+
}
188210
}
189211
```
190212
See [more](./src/main/java/io/github/dgroup/enumerable4j/Enumerable.java).
@@ -225,6 +247,7 @@ See [more](./src/main/java/io/github/dgroup/enumerable4j/Enumerable.java).
225247
`.find(...)` | `.stream().filter(...).findFirst().orElse(...)` | `new FirstOf<>(...,...).value()` | tbd |
226248
`.reduce(...,...)` | `.stream().reduce(...,...)` | `new Reduced<>(...,...).value()` | tbd |
227249
`.after(...)` | | | tbd |
250+
`.next(...)` | | | tbd |
228251

229252
#### .all
230253

@@ -297,6 +320,13 @@ YourOwnCollection<Integer> src = ... // with
297320
Enumerable<Integer> afterThree = src.after(val -> val == 3); // [4, 5, 6]
298321
Enumerable<Integer> firstTwoAfterThree = src.after(val -> val == 3, 2); // [4, 5]
299322
```
323+
#### .next
324+
325+
```java
326+
YourOwnCollection<Integer> src = ... // with elements [1, 2, 3, 4]
327+
Integer next = src.next(val -> val == 2); // 3
328+
Integer alternative = src.next(val -> val > 5, -1); // -1
329+
```
300330

301331
### How to contribute?
302332

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,32 @@ default Enumerable<X> after(Predicate<X> prd, long size) {
258258
}
259259
return out;
260260
}
261+
262+
/**
263+
* Returns the next element of enumerable after the first one which corresponds the condition.
264+
* If no predicate (null) is given, or no element found then null is returned instead.
265+
* @param prd The function to match element after which enumerable element should be returned.
266+
* @return The next element of enumerable after the first one which corresponds the condition.
267+
*/
268+
default X next(Predicate<X> prd) {
269+
return this.next(prd, null);
270+
}
271+
272+
/**
273+
* Returns the next element of enumerable after the first one which corresponds the condition.
274+
* If no predicate (null) is given, or no element found then alternative is returned instead.
275+
* @param prd The function to match element after which enumerable element should be returned.
276+
* @param alt The alternative to return in case of null predicate or no element found.
277+
* @return The next element of enumerable after the first one which corresponds the condition.
278+
*/
279+
default X next(Predicate<X> prd, X alt) {
280+
final X out;
281+
if (prd == null) {
282+
out = alt;
283+
} else {
284+
out = this.after(prd, 1).stream().findFirst().orElse(alt);
285+
}
286+
return out;
287+
}
288+
261289
}

src/test/java/io/github/dgroup/enumerable4j/FindTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void find() {
5050
@Test
5151
void nullPredicate() {
5252
new Assertion<>(
53-
"In case of null predicate we will get the same enumerable",
53+
"In case of the null predicate, we get null as a result",
5454
new Linked<>(1, 2, 3).find(null),
5555
new IsNull<>()
5656
).affirm();
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.IsEqual;
28+
import org.hamcrest.core.IsNull;
29+
import org.junit.jupiter.api.Test;
30+
import org.llorllale.cactoos.matchers.Assertion;
31+
32+
/**
33+
* Test cases for {@link Enumerable#next}.
34+
*
35+
* @since 0.1.0
36+
* @checkstyle MagicNumberCheck (500 lines)
37+
* @checkstyle JavadocMethodCheck (500 lines)
38+
*/
39+
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
40+
public class NextTest {
41+
42+
@Test
43+
void nullPredicate() {
44+
new Assertion<>(
45+
"In a case of the null predicate, we get null as a result",
46+
new Linked<>(1, 2, 3).next(null),
47+
new IsNull<>()
48+
).affirm();
49+
}
50+
51+
@Test
52+
void altNullPredicate() {
53+
new Assertion<>(
54+
"In a case of the null predicate, we get an alternative result",
55+
new Linked<>(1, 2, 3).next(null, -1),
56+
new IsEqual<>(-1)
57+
).affirm();
58+
}
59+
60+
@Test
61+
void getNext() {
62+
new Assertion<>(
63+
"Returns the next element after the first one which corresponds the condition",
64+
new Linked<>(1, 2, 3, 4, 5).next(n -> n > 1),
65+
new IsEqual<>(3)
66+
).affirm();
67+
}
68+
69+
@Test
70+
void altGetNext() {
71+
new Assertion<>(
72+
"Returns the next element after the first one which corresponds the condition",
73+
new Linked<>(1, 2, 3, 4, 5).next(n -> n > 1, -1),
74+
new IsEqual<>(3)
75+
).affirm();
76+
}
77+
78+
@Test
79+
void noneMatch() {
80+
new Assertion<>(
81+
"No one element corresponds the condition, the result is null",
82+
new Linked<>(1, 2, 3).next(n -> n > 10),
83+
new IsNull<>()
84+
).affirm();
85+
}
86+
87+
@Test
88+
void altNoneMatch() {
89+
new Assertion<>(
90+
"No one element corresponds the condition, we get an alternative result",
91+
new Linked<>(1, 2, 3).next(n -> n > 10, -1),
92+
new IsEqual<>(-1)
93+
).affirm();
94+
}
95+
96+
}

0 commit comments

Comments
 (0)