|
1 | 1 | package stream; |
2 | 2 |
|
3 | 3 | import functional.ForEach; |
4 | | -import functional.Mapper; |
| 4 | +import functional.Map; |
5 | 5 | import functional.NextItem; |
6 | 6 | import functional.Predicate; |
7 | 7 |
|
8 | | -import java.util.List; |
9 | | - |
10 | 8 | /** |
11 | 9 | * @Author xiongyx |
12 | 10 | * on 2019/3/5. |
13 | 11 | */ |
14 | | -public class Stream <T>{ |
| 12 | +public class Stream <T> implements StreamInterface<T>{ |
15 | 13 |
|
16 | 14 | //=====================================成员属性=============================== |
17 | 15 |
|
18 | 16 | private T head; |
19 | 17 |
|
20 | | - private NextItem nextItem; |
| 18 | + private Stream<T> tail; |
| 19 | + |
| 20 | + private boolean isEnd; |
| 21 | + |
| 22 | + /** |
| 23 | + * 是否被求值过 |
| 24 | + * */ |
| 25 | + private boolean evaled; |
| 26 | + |
| 27 | + private NextItem<T> eval; |
| 28 | + |
| 29 | + public T getHead() { |
| 30 | + return head; |
| 31 | + } |
| 32 | + |
| 33 | + public void setHead(T head) { |
| 34 | + this.head = head; |
| 35 | + } |
| 36 | + |
| 37 | + public Stream<T> getTail() { |
| 38 | + return tail; |
| 39 | + } |
| 40 | + |
| 41 | + public void setTail(Stream<T> tail) { |
| 42 | + this.tail = tail; |
| 43 | + } |
| 44 | + |
| 45 | + public NextItem<T> getEval() { |
| 46 | + return eval; |
| 47 | + } |
| 48 | + |
| 49 | + public void setEval(NextItem<T> eval) { |
| 50 | + this.eval = eval; |
| 51 | + } |
21 | 52 |
|
22 | 53 | //====================================构造函数=============================== |
23 | 54 |
|
24 | 55 | public Stream() { |
25 | 56 | } |
26 | 57 |
|
27 | | - private Stream(List<T> list){ |
| 58 | + public Stream(T head, Stream<T> tail) { |
| 59 | + this.head = head; |
| 60 | + this.tail = tail; |
| 61 | + } |
28 | 62 |
|
| 63 | + public Stream(NextItem<T> eval) { |
| 64 | + this.eval = eval; |
29 | 65 | } |
30 | | - //=====================================公共接口================================= |
31 | 66 |
|
32 | | - public <R> Stream<R> map(Mapper<R,T> mapper){ |
| 67 | + public Stream(boolean isEnd) { |
| 68 | + this.isEnd = isEnd; |
| 69 | + } |
33 | 70 |
|
34 | | - return new Stream<>(); |
| 71 | + //=====================================公共接口================================= |
| 72 | + |
| 73 | + @Override |
| 74 | + public <R> Stream<R> map(Map<R,T> mapper){ |
| 75 | + return map(mapper,this); |
35 | 76 | } |
36 | 77 |
|
| 78 | + @Override |
37 | 79 | public Stream<T> filter(Predicate<T> predicate){ |
38 | 80 |
|
39 | 81 | return new Stream<>(); |
40 | 82 | } |
41 | 83 |
|
| 84 | + @Override |
42 | 85 | public void forEach(ForEach<T> forEach){ |
43 | 86 | } |
| 87 | + |
| 88 | + //=====================================私有方法===================================== |
| 89 | + |
| 90 | + private <R> Stream<R> map(Map<R,T> mapper,Stream<T> stream){ |
| 91 | + if(isEmptyStream(stream)){ |
| 92 | + return StreamInterface.makeEmptyStream(); |
| 93 | + } |
| 94 | + |
| 95 | + R head = mapper.apply(stream.head); |
| 96 | + Stream tail = new Stream<>( |
| 97 | + ()-> map(mapper,stream.force() |
| 98 | + )); |
| 99 | + |
| 100 | + |
| 101 | + Stream newStream = new Stream( |
| 102 | + head, |
| 103 | + tail |
| 104 | + ); |
| 105 | + return newStream; |
| 106 | + } |
| 107 | + |
| 108 | + private void delay(NextItem<T> nextItem){ |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + private Stream force(){ |
| 113 | + return this.tail.eval.apply(); |
| 114 | + } |
| 115 | + |
| 116 | + private static boolean isEmptyStream(Stream stream){ |
| 117 | + return stream.isEnd; |
| 118 | + } |
44 | 119 | } |
0 commit comments