@@ -33,16 +33,19 @@ def splitted(iterable, pre, offset):
3333 return
3434
3535 @staticmethod
36- def _split (iterable ):
36+ def _iterator (iterable ):
3737 while True :
3838 try :
39- elem = next (iterable )
40- yield elem
39+ yield next (iterable )
4140 except :
4241 return
4342
4443 @staticmethod
4544 def split (iterable , count ):
45+ return [ParallelUtils ._iterator (iterable ) for i in range (count )]
46+
47+ @staticmethod
48+ def finiteSplit (iterable , count ):
4649 elements = list (iterable )
4750 chunks = [[] for _ in range (count )]
4851 chunk_size = int (len (elements ) / count )
@@ -64,16 +67,58 @@ def __init__(self, source):
6467 self ._stream = source
6568 self ._result = None
6669 self ._terminate = False
67- self .lock = RLock ()
6870
69- def _onThread (self , function , * args , ** kwargs ):
70- self ._queue .put ((function , args , kwargs ))
71+ def _onThread (self , function , * args ):
72+ self ._queue .put ((function , args ))
73+
74+ # Filter
75+ def _filter (self , * args ):
76+ self ._stream .filter (args [0 ])
77+
78+ def filter (self , predicate ):
79+ self ._onThread (self ._filter , predicate )
7180
81+ # Map
7282 def _map (self , * args ):
7383 self ._stream .map (args [0 ])
7484
7585 def map (self , mapper ):
76- self ._onThread (self ._map , mapper , None )
86+ self ._onThread (self ._map , mapper )
87+
88+ # FlatMap
89+ def _flatMap (self , * args ):
90+ self ._stream .flatMap (args [0 ])
91+
92+ def flatMap (self , flatMapper ):
93+ self ._onThread (self ._flatMap , flatMapper )
94+
95+ # Distinct
96+ def _distinct (self , * args ):
97+ self ._stream .distinct ()
98+
99+ def distinct (self ):
100+ self ._onThread (self ._distinct , None )
101+
102+ # Sorted
103+ def _sorted (self , * args ):
104+ self ._stream .sorted (args [0 ])
105+
106+ def sorted (self , comparator = None ):
107+ self ._onThread (self ._sorted , comparator )
108+
109+ # Peek
110+ def _peek (self , * args ):
111+ self ._stream .peek (args [0 ])
112+
113+ def peek (self , consumer ):
114+ self ._onThread (self ._peek , consumer )
115+
116+ # ForEach
117+ def _forEach (self , * args ):
118+ self ._stream .forEach (args [0 ])
119+
120+ def forEach (self , function ):
121+ self ._onThread (self ._forEach , function )
77122
78123 def _reduce (self , * args ):
79124 self ._result = self ._stream .reduce (args [0 ], args [1 ])
@@ -84,12 +129,9 @@ def reduce(self, accumulator, identity=None):
84129
85130 def run (self ):
86131 while not self ._terminate :
87- func , args , kwargs = self ._queue .get ()
132+ func , args = self ._queue .get ()
88133 if args :
89- if kwargs :
90- func (* args , ** kwargs )
91- else :
92- func (* args )
134+ func (* args )
93135 else :
94136 func ()
95137
@@ -102,12 +144,24 @@ class ParallelStream(Stream):
102144 PROCESS = 8
103145
104146 def __init__ (self , iterable ):
105- self .__streams = [StreamThread (
106- _stream ) for _stream in ParallelUtils .split (iterable , self .PROCESS )]
147+
148+ self .__streams = [StreamThread (Stream (iterator ))
149+ for iterator in ParallelUtils .split (iterable , self .PROCESS )]
107150
108151 for _stream in self .__streams :
109152 _stream .start ()
110153
154+ def filter (self , predicate ):
155+ '''
156+ 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.
157+
158+ :param function predicate: predicate to apply to each element to determine if it should be included
159+ :return: the new stream
160+ '''
161+ for _stream in self .__streams :
162+ _stream .fi
163+ return Stream (IteratorUtils .filter (self .__iterable , predicate ))
164+
111165 def map (self , mapper ):
112166
113167 for _stream in self .__streams :
@@ -123,7 +177,12 @@ def reduce(self, accumulator, identity=None):
123177 for _stream in self .__streams :
124178 _stream .join ()
125179
126- results = [_stream .getResult ().get () for _stream in self .__streams ]
180+ results = []
181+
182+ for _stream in self .__streams :
183+ res = _stream .getResult ()
184+ if res .isPresent ():
185+ results .append (res .get ())
127186 return Stream (results ).reduce (accumulator , identity )
128187
129188 def get (self ):
0 commit comments