Skip to content

Commit 15a075e

Browse files
committed
Add '.after' method
1 parent 8cefcd2 commit 15a075e

3 files changed

Lines changed: 167 additions & 0 deletions

File tree

readme.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* [.reject](#reject)
3232
* [.find](#find)
3333
* [.reduce](#reduce)
34+
* [.after](#after)
3435

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

@@ -161,6 +162,29 @@ public interface Enumerable<X> extends Collection<X> {
161162
default X reduce(X idn, BinaryOperator<X> opr) {
162163
// ...
163164
}
165+
166+
/**
167+
* Returns an enumerable containing all elements of enumerable
168+
* after the first one which corresponds the condition.
169+
* If no predicate (null) is given, then 'this' is returned instead.
170+
* @param prd The function to match element after which enumerable elements should be returned.
171+
* @return The enumerable.
172+
*/
173+
default Enumerable<X> after(Predicate<X> prd) {
174+
// ...
175+
}
176+
177+
/**
178+
* Returns an enumerable containing a certain number of elements of enumerable
179+
* after the first one which corresponds the condition.
180+
* If no predicate (null) is given, then 'this' is returned instead.
181+
* @param prd The function to match element after which enumerable elements should be returned.
182+
* @param size The number of elements the enumerable should be limited to.
183+
* @return The enumerable.
184+
*/
185+
default Enumerable<X> after(Predicate<X> prd, long size) {
186+
// ...
187+
}
164188
}
165189
```
166190
See [more](./src/main/java/io/github/dgroup/enumerable4j/Enumerable.java).
@@ -200,6 +224,7 @@ See [more](./src/main/java/io/github/dgroup/enumerable4j/Enumerable.java).
200224
`.count(...)` | `.stream().filter(...).count()` | `new Filtered<>(...).size()` | tbd |
201225
`.find(...)` | `.stream().filter(...).findFirst().orElse(...)` | `new FirstOf<>(...,...).value()` | tbd |
202226
`.reduce(...,...)` | `.stream().reduce(...,...)` | `new Reduced<>(...,...).value()` | tbd |
227+
`.after(...)` | | | tbd |
203228

204229
#### .all
205230

@@ -265,6 +290,14 @@ YourOwnCollection<Integer> src = ... // with elements [1, 2,
265290
Integer sum = src.reduce(0, Integer::sum); // 6
266291
```
267292

293+
#### .after
294+
295+
```java
296+
YourOwnCollection<Integer> src = ... // with elements [2, 3, 4, 5, 6]
297+
Enumerable<Integer> afterThree = src.after(val -> val == 3); // [4, 5, 6]
298+
Enumerable<Integer> firstTwoAfterThree = src.after(val -> val == 3, 2); // [4, 5]
299+
```
300+
268301
### How to contribute?
269302

270303
[![EO badge](http://www.elegantobjects.org/badge.svg)](http://www.elegantobjects.org/#principles)

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* @param <X> The type of entities.
4343
* @since 0.1.0
4444
*/
45+
@SuppressWarnings("PMD.TooManyMethods")
4546
public interface Enumerable<X> extends Collection<X> {
4647

4748
/**
@@ -210,4 +211,42 @@ default X reduce(X idn, BinaryOperator<X> opr) {
210211
}
211212
return res;
212213
}
214+
215+
/**
216+
* Returns an enumerable containing all elements of enumerable
217+
* after the first one which corresponds the condition.
218+
* If no predicate (null) is given, then 'this' is returned instead.
219+
* @param prd The function to match element after which enumerable elements should be returned.
220+
* @return The enumerable.
221+
*/
222+
default Enumerable<X> after(Predicate<X> prd) {
223+
return this.after(prd, this.size() - 1);
224+
}
225+
226+
/**
227+
* Returns an enumerable containing a certain number of elements of enumerable
228+
* after the first one which corresponds the condition.
229+
* If no predicate (null) is given, then 'this' is returned instead.
230+
* @param prd The function to match element after which enumerable elements should be returned.
231+
* @param size The number of elements the enumerable should be limited to.
232+
* @return The enumerable.
233+
*/
234+
default Enumerable<X> after(Predicate<X> prd, long size) {
235+
final Enumerable<X> out;
236+
if (prd == null) {
237+
out = this;
238+
} else {
239+
int skip = 0;
240+
for (final X elem : this) {
241+
++skip;
242+
if (prd.test(elem)) {
243+
break;
244+
}
245+
}
246+
out = new Linked<>(
247+
this.stream().skip(skip).limit(size).collect(Collectors.toList())
248+
);
249+
}
250+
return out;
251+
}
213252
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.junit.jupiter.api.Test;
28+
import org.llorllale.cactoos.matchers.Assertion;
29+
import org.llorllale.cactoos.matchers.HasValues;
30+
31+
/**
32+
* Test cases for {@link Enumerable#after}.
33+
*
34+
* @since 0.1.0
35+
* @checkstyle MagicNumberCheck (500 lines)
36+
* @checkstyle JavadocMethodCheck (500 lines)
37+
*/
38+
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
39+
public final class AfterTest {
40+
41+
@Test
42+
void nullPredicate() {
43+
new Assertion<>(
44+
"In case null-function the self enumerable is expected",
45+
new Linked<>(1, 2, 3, 4).after(null),
46+
new HasValues<>(1, 2, 3, 4)
47+
).affirm();
48+
}
49+
50+
@Test
51+
void allAfter() {
52+
new Assertion<>(
53+
"All elements after the first one which corresponds the condition",
54+
new Linked<>(1, 2, 3, 4, 5, 6, 7).after(n -> n > 2),
55+
new HasValues<>(4, 5, 6, 7)
56+
).affirm();
57+
}
58+
59+
@Test
60+
void firstThreeAfter() {
61+
new Assertion<>(
62+
"First 3 elements after the first one which corresponds the condition",
63+
new Linked<>(1, 2, 3, 4, 5, 6, 7).after(n -> n > 2, 3),
64+
new HasValues<>(4, 5, 6)
65+
).affirm();
66+
}
67+
68+
@Test
69+
void allStringsAfter() {
70+
new Assertion<>(
71+
"All string elements after the first one which corresponds the condition",
72+
new Linked<>("a", "b", "c", "d", "e").after(s -> s.equals("c")),
73+
new HasValues<>("d", "e")
74+
).affirm();
75+
}
76+
77+
@Test
78+
void firstTenAfter() {
79+
new Assertion<>(
80+
"The specified size can be greater than an actual enumerable size",
81+
new Linked<>(1, 2, 3, 4, 5).after(n -> n == 3, 10),
82+
new HasValues<>(4, 5)
83+
).affirm();
84+
}
85+
86+
@Test
87+
void allAfterFirst() {
88+
new Assertion<>(
89+
"The first element of enumerable corresponds the condition",
90+
new Linked<>(1, 2, 3, 4, 5).after(n -> n < 10),
91+
new HasValues<>(2, 3, 4, 5)
92+
).affirm();
93+
}
94+
95+
}

0 commit comments

Comments
 (0)