Skip to content

Commit a2dc890

Browse files
custom stream draft implementation
1 parent db962d7 commit a2dc890

6 files changed

Lines changed: 176 additions & 0 deletions

File tree

custom-stream/build.gradle

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
group 'custom-stream'
6+
version '1.0-SNAPSHOT'
7+
8+
sourceCompatibility = 1.8
9+
10+
repositories {
11+
mavenCentral()
12+
}
13+
14+
dependencies {
15+
testCompile group: 'junit', name: 'junit', version: '4.12'
16+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
import java.util.Objects;
4+
import java.util.function.Function;
5+
import java.util.function.Predicate;
6+
7+
public class ReferencePipeline implements Stream {
8+
9+
protected ReferencePipeline previousStage;
10+
11+
private List<StreamObject> objects;
12+
13+
private ReferencePipeline(List<StreamObject> objects) {
14+
this.objects = objects;
15+
}
16+
17+
public ReferencePipeline() {
18+
}
19+
20+
public Sink opWrapSink(Sink sink) {
21+
throw new UnsupportedOperationException();
22+
}
23+
24+
public static ReferencePipeline of(List<StreamObject> objects) {
25+
return new ReferencePipeline(objects);
26+
}
27+
28+
public Stream filter(Predicate<StreamObject> predicate) {
29+
return new Operation(this) {
30+
@Override
31+
public Sink opWrapSink(Sink sink) {
32+
return new ChainedReference(sink) {
33+
34+
@Override
35+
public void accept(Object o) {
36+
if (predicate.test((StreamObject) o))
37+
downstream.accept(o);
38+
}
39+
};
40+
}
41+
};
42+
}
43+
44+
public Stream map(Function function) {
45+
return new Operation(this) {
46+
@Override
47+
public Sink opWrapSink(Sink sink) {
48+
return new ChainedReference(sink) {
49+
50+
@Override
51+
public void accept(Object o) {
52+
downstream.accept(function.apply(o));
53+
}
54+
};
55+
}
56+
};
57+
}
58+
59+
public List<Object> toList() {
60+
final List<Object> result = new ArrayList<>();
61+
62+
Sink finalSink = o -> result.add(o);
63+
64+
ReferencePipeline previousStage = this;
65+
Sink sink = finalSink;
66+
67+
while (previousStage.previousStage != null) {
68+
sink = previousStage.opWrapSink(sink);
69+
previousStage = previousStage.previousStage;
70+
}
71+
72+
for (StreamObject object : previousStage.objects) {
73+
sink.accept(object);
74+
}
75+
76+
return result;
77+
}
78+
79+
abstract class Operation extends ReferencePipeline {
80+
81+
public Operation(ReferencePipeline previousStage) {
82+
this.previousStage = previousStage;
83+
}
84+
}
85+
86+
abstract class ChainedReference implements Sink {
87+
protected final Sink downstream;
88+
89+
public ChainedReference(Sink downstream) {
90+
this.downstream = Objects.requireNonNull(downstream);
91+
}
92+
}
93+
94+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import java.util.function.Consumer;
2+
3+
interface Sink extends Consumer {
4+
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import java.util.List;
2+
import java.util.function.Function;
3+
import java.util.function.Predicate;
4+
5+
interface Stream {
6+
Stream filter(Predicate<StreamObject> predicate);
7+
Stream map(Function<StreamObject, Integer> function);
8+
List<Object> toList();
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class StreamObject {
2+
3+
private int id;
4+
private String name;
5+
6+
public int getId() {
7+
return id;
8+
}
9+
10+
public void setId(int id) {
11+
this.id = id;
12+
}
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import org.junit.Test;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class StreamTest {
8+
9+
@Test
10+
public void debugStream() {
11+
StreamObject object0 = new StreamObject();
12+
object0.setId(0);
13+
object0.setName("0");
14+
15+
StreamObject object1 = new StreamObject();
16+
object1.setId(1);
17+
object1.setName("1");
18+
19+
List<StreamObject> collection = Arrays.asList(object0, object1);
20+
// final List<Integer> collect = collection.stream()
21+
// .filter(streamObject -> streamObject.getId() == 0)
22+
// .map(StreamObject::getId)
23+
// .collect(Collectors.toList());
24+
25+
ReferencePipeline.of(collection)
26+
.filter(streamObject -> streamObject.getId() == 0)
27+
.map(StreamObject::getId)
28+
.toList();
29+
}
30+
31+
}

0 commit comments

Comments
 (0)