-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream.py
385 lines (302 loc) · 14.2 KB
/
stream.py
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
from functools import cmp_to_key
from .util.iterators import IteratorUtils
from .util.optional import Optional
class Stream():
"""
A sequence of elements supporting sequential operations.
The following example illustrates an aggregate operation using
Stream:
result = Stream(elements)
.filter(lambda w: w.getColor() == RED)
.map(lambda w: w.getWeight())
.sum()
A stream pipeline, like the elements example above, can be viewed as a query on the stream source.
A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may raise Exception if it detects that the stream is being reused.
"""
"""
Static Methods
"""
@staticmethod
def empty():
'''
Returns an empty sequential Stream.
:return: an empty sequential stream
'''
return Stream([])
@staticmethod
def of(elem):
'''
Returns a sequential Stream containing a single element.
:param T elem: the single element
:return: a singleton sequential stream
'''
return Stream(iter([elem]))
@staticmethod
# skipcq: PYL-E0102
def of(*elements):
'''
Returns a sequential ordered stream whose elements are the specified values.
:param *T elements: the elements of the new stream
:return: the new stream
'''
return Stream(iter(list(elements)))
@staticmethod
def ofNullable(elem):
'''
Returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.
:param T elem: the single element
:return: a stream with a single element if the specified element is non-null, otherwise an empty stream
'''
return Stream.of(elem) if elem is not None else Stream.empty()
@staticmethod
def iterate(seed, operator):
'''
Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.
:param T seed: the initial element
:param UnaryOperator operator: a function to be applied to the previous element to produce a new element
:return: a new sequential Stream
'''
return Stream(IteratorUtils.iterate(seed, operator))
@staticmethod
def generate(supplier):
'''
Returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc.
:param Supplier supplier: the Supplier of generated elements
:return: a new infinite sequential unordered Stream
'''
return Stream(IteratorUtils.generate(supplier))
@staticmethod
def concat(*streams):
'''
Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream and so on.
:param *Stream streams: the streams to concat
:return: the concatenation of the input streams
'''
return Stream(IteratorUtils.concat(*streams))
@staticmethod
def constant(element):
'''
Return an infinite sequential stream where each element is the passed element
:param T elem: the element
:return: the new stream made of @element
'''
return Stream.generate(lambda: element)
"""
Normal Methods
"""
def __init__(self, iterable):
self.iterable = iterable
def filter(self, predicate):
'''
Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
:param function predicate: predicate to apply to each element to determine if it should be included
:return: self
'''
self.iterable = IteratorUtils.filter(self.iterable, predicate)
return self
def map(self, mapper):
'''
Returns a stream consisting of the results of applying the given function to the elements of this stream.
:param function mapper: function to apply to each element
:return: self
'''
self.iterable = IteratorUtils.map(self.iterable, mapper)
return self
def flatMap(self, flatMapper):
'''
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)
:param function flatMapper: function to apply to each element which produces a stream of new values
:return: self
'''
self.iterable = IteratorUtils.flatMap(self.iterable, flatMapper)
return self
def distinct(self):
'''
Returns a stream consisting of the distinct elements of this stream.
:return: self
'''
self.iterable = IteratorUtils.distinct(self.iterable)
return self
def limit(self, count):
'''
Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
:param int count: the number of elements the stream should be limited to
:return: self
'''
self.iterable = IteratorUtils.limit(self.iterable, count)
return self
def skip(self, count):
'''
Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.
:param int count: the number of leading elements to skip
:return: self
'''
self.iterable = IteratorUtils.skip(self.iterable, count)
return self
def takeWhile(self, predicate):
'''
Returns a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.
:param Predicate predicate: predicate to apply to elements to determine the longest prefix of elements.
:return: self
'''
self.iterable = IteratorUtils.takeWhile(self.iterable, predicate)
return self
def dropWhile(self, predicate):
'''
Returns a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.
:param Predicate predicate: predicate to apply to elements to determine the longest prefix of elements.
:return: self
'''
self.iterable = IteratorUtils.dropWhile(self.iterable, predicate)
return self
"""
From here this method mustn't be called on infinite stream
"""
def sorted(self, comparator=None):
'''
Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.
:param Comparator comparator: Comparator to be used to compare stream elements - if null default comparator is used
:return: self
'''
self.iterable = iter(sorted(
self.iterable, key=cmp_to_key(comparator))) if comparator is not None else iter(sorted(
self.iterable))
return self
def peek(self, consumer):
'''
Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
:param Consumer consumer: action to perform on the elements as they are consumed from the stream
:return: self
'''
self.iterable = IteratorUtils.peek(self.iterable, consumer)
return self
def forEach(self, function):
'''
Performs an action for each element of this stream.
:param Function function: action to perform on the elements
:return: None
'''
for elem in self.iterable:
function(elem)
def anyMatch(self, predicate):
'''
Returns whether any elements of this stream match the provided predicate.
:param Predicate predicate: predicate to apply to elements of this stream
:return: True if any elements of the stream match the provided predicate, otherwise False
'''
return any([predicate(elem) for elem in self.iterable])
def allMatch(self, predicate):
'''
Returns whether all elements of this stream match the provided predicate.
:param Predicate predicate: predicate to apply to elements of this stream
:return: True if either all elements of the stream match the provided predicate or the stream is empty, otherwise False
'''
return all([predicate(elem) for elem in self.iterable])
def noneMatch(self, predicate):
'''
Returns whether no elements of this stream match the provided predicate.
:param Predicate predicate: predicate to apply to elements of this stream
:return: True if either no elements of the stream match the provided predicate or the stream is empty, otherwise False
'''
return not self.anyMatch(predicate)
def findFirst(self, predicate=None):
"""
Returns an Optional describing the first element
(with optional filtering by a given predicate) of this stream,
or an empty Optional if the stream is empty.
If the stream has no encounter order, then any element may be returned.
:param Predicate predicate: optional predicate to apply to elements
of this stream
:return: an Optional describing the first element of this stream,
or an empty Optional if the stream is empty
"""
if predicate:
self.filter(predicate)
for elem in self.iterable:
return Optional.of(elem)
return Optional.ofNullable(None)
def findAny(self):
'''
Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.
:return: an Optional describing some element of this stream, or an empty Optional if the stream is empty
'''
return self.findFirst()
def reduce(self, accumulator, identity=None):
'''
Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.
:param T identity: the identity value for the accumulating function - if not specified it will be the first element of the stream
:param Accumulator accumulator: function for combining two values
:return: the result of reduction
'''
result = identity
for elem in self.iterable:
if(result is None):
result = elem
else:
result = accumulator(result, elem)
return Optional.ofNullable(result)
def min(self, comparator=None):
'''
Returns the minimum element of this stream according to the provided Comparator. This is a special case of a reduction.
:param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used
:return: an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty
'''
elements = list(self.iterable)
if len(elements) == 0:
return Optional.empty()
return Optional.ofNullable(min(elements, key=cmp_to_key(comparator), default=None)) if comparator is not None else Optional.ofNullable(min(elements, default=None))
def max(self, comparator=None):
'''
Returns the maximum element of this stream according to the provided Comparator. This is a special case of a reduction.
:param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used
:return: an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty
'''
elements = list(self.iterable)
if len(elements) == 0:
return Optional.empty()
return Optional.ofNullable(max(elements, key=cmp_to_key(comparator))) if comparator is not None else Optional.ofNullable(max(elements))
def sum(self):
'''
Returns the sum of all elements of this stream. This is a special case of a reduction.
:return: an Optional describing the sum of all the elements of this stream, or an empty Optional if the stream is empty
'''
return self.reduce(lambda x, y: x + y)
def count(self):
'''
Returns the count of elements in this stream. This is a special case of a reduction.
:return: the count of elements in this stream
'''
count = 0
for elem in self.iterable:
count += 1
return count
def toList(self):
'''
Returns a list with the elements in this stream.
:return: the list of elements in this stream
'''
return list(self.iterable)
def toSet(self):
'''
Returns a set with the elements in this stream.
:return: the set of elements in this stream
'''
return set(self.iterable)
def toNumberStream(self):
from .numbers import NumberStream
return NumberStream(self)
def toBooleanStream(self):
from .booleans import BooleanStream
return BooleanStream(self)
def __iter__(self):
'''
Returns an iterator over the elements in this stream.
:return: the iterator over the elements in this stream
'''
return iter(self.iterable)
def __eq__(self, value):
'''
Check if this stream is equal to the specified stream
:return: True if the streams match, False otherwise
'''
return self.toSet() == value.toSet()