-
Notifications
You must be signed in to change notification settings - Fork 17
/
CustomStream.java
208 lines (181 loc) · 5.43 KB
/
CustomStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package by.andd3dfx.stream;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* Implement CustomStream class to make possible usage of filter() and map() operations like next expression:
* stream.filter(item -> item.y > 10).map(Item::getX);
* <p>
* Add collectToList() method to CustomStream
*
* @see <a href="https://youtu.be/LvBjS17CatQ">Video solution part1</a>, <a href="https://youtu.be/iuzWoSzl1to">part2</a>
*/
public class CustomStream<T> {
private List<T> list;
private List<Action> actions = new ArrayList<>();
public CustomStream(List<T> list) {
this.list = list;
}
public static <T> CustomStream<T> empty() {
return new CustomStream<>(List.of());
}
public static <T> CustomStream<T> of(T value) {
return new CustomStream<>(List.of(value));
}
public static <T> CustomStream<T> of(T... values) {
return new CustomStream<>(List.of(values));
}
public static <T> CustomStream<T> ofNullable(T value) {
if (value == null) {
return empty();
}
return of(value);
}
public CustomStream<T> filter(Predicate<T> predicate) {
actions.add(stream -> {
List result = new ArrayList<>();
for (var item : list) {
if (predicate.test(item)) {
result.add(item);
}
}
list = result;
});
return this;
}
public CustomStream<T> map(Converter<T, Object> converter) {
actions.add(stream -> {
List result = new ArrayList<>();
for (var item : list) {
result.add(converter.convert(item));
}
list = result;
});
return this;
}
public CustomStream<T> limit(int n) {
actions.add(stream -> {
list = list.subList(0, Math.min(n, list.size()));
});
return this;
}
/**
* Like limit() but with condition instead of number
*/
public CustomStream<T> takeWhile(Predicate<T> predicate) {
actions.add(stream -> {
int i = 0;
for (var item : list) {
if (!predicate.test(item)) {
break;
}
i++;
}
list = list.subList(0, i);
});
return this;
}
public CustomStream<T> skip(int n) {
actions.add(stream -> {
list = list.subList(Math.min(n, list.size()), list.size());
});
return this;
}
/**
* Like skip() but with condition instead of number
*/
public CustomStream<T> dropWhile(Predicate<T> predicate) {
actions.add(stream -> {
int i = 0;
for (var item : list) {
if (!predicate.test(item)) {
break;
}
i++;
}
list = list.subList(i, list.size());
});
return this;
}
public CustomStream<T> distinct() {
actions.add(stream -> {
var result = new ArrayList<T>();
extLoop:
for (var item : list) {
for (var addedItem : result) {
if (isEquals(item, addedItem)) {
continue extLoop;
}
}
result.add(item);
}
list = result;
});
return this;
}
public CustomStream<T> sorted(Comparator<? super T> comparator) {
actions.add(stream -> {
Collections.sort(list, comparator);
});
return this;
}
static <T> boolean isEquals(T item1, T item2) {
if (item1 == null) {
return item2 == null;
}
return item1.equals(item2);
}
public List<T> collectToList() {
for (var action : actions) {
action.apply(this);
}
return list;
}
public int count() {
for (var action : actions) {
action.apply(this);
}
return list.size();
}
public Object[] toArray() {
for (var action : actions) {
action.apply(this);
}
return list.toArray();
}
public List<T> toList() {
for (var action : actions) {
action.apply(this);
}
return Collections.unmodifiableList(list);
}
public void forEach(Function<T, Object> function) {
for (var action : actions) {
action.apply(this);
}
for (var item : list) {
function.apply(item);
}
}
public static void main(String[] args) {
List<Item> list = Arrays.asList(new Item(1, 3), new Item(2, 18), new Item(5, 11));
var stream = new CustomStream<>(list);
List result = stream
.filter(item -> item.getY() > 10)
.map(Item::getX)
.collectToList();
System.out.println(result);
}
@Data
@AllArgsConstructor
public static class Item {
private int x;
private int y;
}
}