Skip to content

Commit f678cbf

Browse files
committed
feat: StreamEx
1 parent 373cba2 commit f678cbf

2 files changed

Lines changed: 52 additions & 53 deletions

File tree

src/main/java/com/github/lokic/javaplus/stream/ExStream.java

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ public <R> ExStream<R> func(Function<Stream<T>, Stream<R>> function) {
2828
return new ExStream<>(function.apply(stream()));
2929
}
3030

31-
// public ExStream<T> filter(Predicate<? super T> predicate) {
32-
// return new ExStream<>(stream().filter(predicate));
33-
// }
34-
//
35-
// public <R> ExStream<R> map(Function<? super T, ? extends R> mapper) {
36-
// return new ExStream<>(stream().map(mapper));
37-
// }
38-
//
39-
// public <R> ExStream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper) {
40-
// return new ExStream<>(stream().flatMap(mapper));
41-
// }
31+
public ExStream<T> filter(Predicate<? super T> predicate) {
32+
return new ExStream<>(stream().filter(predicate));
33+
}
34+
35+
public <R> ExStream<R> map(Function<? super T, ? extends R> mapper) {
36+
return new ExStream<>(stream().map(mapper));
37+
}
38+
39+
public <R> ExStream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper) {
40+
return new ExStream<>(stream().flatMap(mapper));
41+
}
4242

4343
public ExStream<T> takeWhile(Predicate<? super T> predicate) {
4444
return new ExStream<>(Predicates.takeWhile(stream(), predicate));
@@ -110,19 +110,4 @@ public static <T> ExStream<T> of(T... values) {
110110
return new ExStream<>(Stream.of(values));
111111
}
112112

113-
114-
public static class Func {
115-
public static <T> Function<Stream<T>, Stream<T>> filter(Predicate<? super T> predicate) {
116-
return s -> s.filter(predicate);
117-
}
118-
119-
public static <T, R> Function<Stream<T>, Stream<R>> map(Function<? super T, ? extends R> mapper) {
120-
return s -> s.map(mapper);
121-
}
122-
123-
public static <T, R> Function<Stream<T>, Stream<R>> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper) {
124-
return s -> s.flatMap(mapper);
125-
}
126-
}
127-
128113
}

src/test/java/com/github/lokic/javaplus/stream/ExStreamTest.java

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
import java.util.List;
1212
import java.util.concurrent.atomic.AtomicInteger;
1313
import java.util.function.Function;
14+
import java.util.function.Predicate;
1415
import java.util.stream.Collectors;
1516
import java.util.stream.Stream;
1617

1718
import static com.github.lokic.javaplus.join.Join.on;
18-
import static com.github.lokic.javaplus.stream.ExStream.Func.*;
19+
import static com.github.lokic.javaplus.stream.ExStreamTest.Func.*;
1920

2021
public class ExStreamTest {
2122

@@ -62,14 +63,14 @@ public void func_filter() {
6263
Assert.assertEquals(Lists.newArrayList("1", "3"), li);
6364
}
6465

65-
// @Test
66-
// public void test_filter() {
67-
// List<String> li = ExStream.of(Stream.of("1", "2", "3"))
68-
// .filter(x -> !"2".equals(x))
69-
// .stream()
70-
// .collect(Collectors.toList());
71-
// Assert.assertEquals(Lists.newArrayList("1", "3"), li);
72-
// }
66+
@Test
67+
public void test_filter() {
68+
List<String> li = ExStream.of(Stream.of("1", "2", "3"))
69+
.filter(x -> !"2".equals(x))
70+
.stream()
71+
.collect(Collectors.toList());
72+
Assert.assertEquals(Lists.newArrayList("1", "3"), li);
73+
}
7374

7475
@Test
7576
public void test_map() {
@@ -80,15 +81,14 @@ public void test_map() {
8081
Assert.assertEquals(Lists.newArrayList("A1", "A2", "A3"), li);
8182
}
8283

83-
//
84-
// @Test
85-
// public void func_map() {
86-
// List<String> li = ExStream.of(Stream.of("1", "2", "3"))
87-
// .map(x -> "A" + x)
88-
// .stream()
89-
// .collect(Collectors.toList());
90-
// Assert.assertEquals(Lists.newArrayList("A1", "A2", "A3"), li);
91-
// }
84+
@Test
85+
public void func_map() {
86+
List<String> li = ExStream.of(Stream.of("1", "2", "3"))
87+
.map(x -> "A" + x)
88+
.stream()
89+
.collect(Collectors.toList());
90+
Assert.assertEquals(Lists.newArrayList("A1", "A2", "A3"), li);
91+
}
9292

9393
@Test
9494
public void func_flatMap() {
@@ -99,15 +99,15 @@ public void func_flatMap() {
9999
Assert.assertEquals(Lists.newArrayList("A1", "A2", "A3"), li);
100100
}
101101

102-
//
103-
// @Test
104-
// public void test_flatMap() {
105-
// List<String> li = ExStream.of(Stream.of("1", "2", "3"))
106-
// .flatMap(x -> Stream.of("A" + x))
107-
// .stream()
108-
// .collect(Collectors.toList());
109-
// Assert.assertEquals(Lists.newArrayList("A1", "A2", "A3"), li);
110-
// }
102+
103+
@Test
104+
public void test_flatMap() {
105+
List<String> li = ExStream.of(Stream.of("1", "2", "3"))
106+
.flatMap(x -> Stream.of("A" + x))
107+
.stream()
108+
.collect(Collectors.toList());
109+
Assert.assertEquals(Lists.newArrayList("A1", "A2", "A3"), li);
110+
}
111111

112112
@Test
113113
public void takeWhile() {
@@ -216,4 +216,18 @@ public String convert(Integer x) {
216216
}
217217
}
218218

219+
public static class Func {
220+
public static <T> Function<Stream<T>, Stream<T>> filter(Predicate<? super T> predicate) {
221+
return s -> s.filter(predicate);
222+
}
223+
224+
public static <T, R> Function<Stream<T>, Stream<R>> map(Function<? super T, ? extends R> mapper) {
225+
return s -> s.map(mapper);
226+
}
227+
228+
public static <T, R> Function<Stream<T>, Stream<R>> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper) {
229+
return s -> s.flatMap(mapper);
230+
}
231+
}
232+
219233
}

0 commit comments

Comments
 (0)