11package org .utbot .engine .overrides .stream ;
22
3+ import org .jetbrains .annotations .NotNull ;
34import org .utbot .api .mock .UtMock ;
4- import org .utbot .engine .overrides .UtArrayMock ;
55import org .utbot .engine .overrides .collections .RangeModifiableUnlimitedArray ;
66import org .utbot .engine .overrides .collections .UtArrayList ;
77import org .utbot .engine .overrides .collections .UtGenericStorage ;
8+ import org .utbot .engine .overrides .stream .actions .DistinctAction ;
9+ import org .utbot .engine .overrides .stream .actions .LimitAction ;
10+ import org .utbot .engine .overrides .stream .actions .NaturalSortingAction ;
11+ import org .utbot .engine .overrides .stream .actions .SkipAction ;
12+ import org .utbot .engine .overrides .stream .actions .StreamAction ;
13+ import org .utbot .engine .overrides .stream .actions .objects .ConsumerAction ;
14+ import org .utbot .engine .overrides .stream .actions .objects .FilterAction ;
15+ import org .utbot .engine .overrides .stream .actions .objects .MapAction ;
16+ import org .utbot .engine .overrides .stream .actions .objects .SortingAction ;
17+ import org .utbot .engine .overrides .stream .actions .objects .ToDoubleMapAction ;
18+ import org .utbot .engine .overrides .stream .actions .objects .ToIntMapAction ;
19+ import org .utbot .engine .overrides .stream .actions .objects .ToLongMapAction ;
820
921import java .util .Collection ;
1022import java .util .Comparator ;
2840import java .util .stream .IntStream ;
2941import java .util .stream .LongStream ;
3042import java .util .stream .Stream ;
31- import org .jetbrains .annotations .NotNull ;
32- import org .utbot .engine .overrides .stream .actions .ConsumerAction ;
33- import org .utbot .engine .overrides .stream .actions .DistinctAction ;
34- import org .utbot .engine .overrides .stream .actions .FilterAction ;
35- import org .utbot .engine .overrides .stream .actions .LimitAction ;
36- import org .utbot .engine .overrides .stream .actions .MapAction ;
37- import org .utbot .engine .overrides .stream .actions .NaturalSortingAction ;
38- import org .utbot .engine .overrides .stream .actions .SkipAction ;
39- import org .utbot .engine .overrides .stream .actions .SortingAction ;
40- import org .utbot .engine .overrides .stream .actions .StreamAction ;
4143
4244import static org .utbot .api .mock .UtMock .assume ;
4345import static org .utbot .api .mock .UtMock .assumeOrExecuteConcretely ;
@@ -51,13 +53,13 @@ public class UtStream<E> implements Stream<E>, UtGenericStorage<E> {
5153 /**
5254 * A reference to the original collection. The default collection is {@link UtArrayList}.
5355 */
54- private final Collection origin ;
56+ final Collection origin ;
5557
56- private final RangeModifiableUnlimitedArray <StreamAction > actions ;
58+ final RangeModifiableUnlimitedArray <StreamAction > actions ;
5759
58- private final RangeModifiableUnlimitedArray <Runnable > closeHandlers ;
60+ final RangeModifiableUnlimitedArray <Runnable > closeHandlers ;
5961
60- private boolean isParallel = false ;
62+ boolean isParallel = false ;
6163
6264 /**
6365 * {@code false} by default, assigned to {@code true} after performing any operation on this stream. Any operation,
@@ -102,6 +104,42 @@ public UtStream(UtStream other) {
102104 isClosed = false ;
103105 }
104106
107+ public UtStream (UtIntStream other ) {
108+ visit (this );
109+
110+ origin = other .origin ;
111+ actions = other .actions ;
112+ isParallel = other .isParallel ;
113+ closeHandlers = other .closeHandlers ;
114+
115+ // new stream should be opened
116+ isClosed = false ;
117+ }
118+
119+ public UtStream (UtLongStream other ) {
120+ visit (this );
121+
122+ origin = other .origin ;
123+ actions = other .actions ;
124+ isParallel = other .isParallel ;
125+ closeHandlers = other .closeHandlers ;
126+
127+ // new stream should be opened
128+ isClosed = false ;
129+ }
130+
131+ public UtStream (UtDoubleStream other ) {
132+ visit (this );
133+
134+ origin = other .origin ;
135+ actions = other .actions ;
136+ isParallel = other .isParallel ;
137+ closeHandlers = other .closeHandlers ;
138+
139+ // new stream should be opened
140+ isClosed = false ;
141+ }
142+
105143 /**
106144 * Precondition check is called only once by object,
107145 * if it was passed as parameter to method under test.
@@ -116,8 +154,6 @@ void preconditionCheck() {
116154 if (alreadyVisited (this )) {
117155 return ;
118156 }
119- // TODO ?
120- // setEqualGenericType(elementData);
121157
122158 assume (origin != null );
123159
@@ -151,18 +187,6 @@ public Stream<E> filter(Predicate<? super E> predicate) {
151187 return new UtStream <>(this );
152188 }
153189
154- private int filterInvocation (Object [] originArray , Object [] filtered , Predicate filter ) {
155- int newSize = 0 ;
156-
157- for (Object o : originArray ) {
158- if (filter .test (o )) {
159- filtered [newSize ++] = o ;
160- }
161- }
162-
163- return newSize ;
164- }
165-
166190 @ Override
167191 public <R > Stream <R > map (Function <? super E , ? extends R > mapper ) {
168192 preconditionCheckWithClosingStream ();
@@ -173,18 +197,14 @@ public <R> Stream<R> map(Function<? super E, ? extends R> mapper) {
173197 return new UtStream <>(this );
174198 }
175199
176- private void mapInvocation (Object [] originArray , Object [] transformed , Function mapping ) {
177- int newSize = 0 ;
178-
179- for (Object o : originArray ) {
180- transformed [newSize ++] = mapping .apply (o );
181- }
182- }
183-
184200 @ Override
185201 public IntStream mapToInt (ToIntFunction <? super E > mapper ) {
186- // TODO
187- return null ;
202+ preconditionCheckWithClosingStream ();
203+
204+ final ToIntMapAction mapAction = new ToIntMapAction (mapper );
205+ actions .insert (actions .end ++, mapAction );
206+
207+ return new UtIntStream (this );
188208// preconditionCheckWithClosingStream();
189209//
190210// int size = origin.size();
@@ -200,8 +220,12 @@ public IntStream mapToInt(ToIntFunction<? super E> mapper) {
200220
201221 @ Override
202222 public LongStream mapToLong (ToLongFunction <? super E > mapper ) {
203- // TODO
204- return null ;
223+ preconditionCheckWithClosingStream ();
224+
225+ final ToLongMapAction mapAction = new ToLongMapAction (mapper );
226+ actions .insert (actions .end ++, mapAction );
227+
228+ return new UtLongStream (this );
205229// preconditionCheckWithClosingStream();
206230//
207231// int size = origin.size();
@@ -217,8 +241,12 @@ public LongStream mapToLong(ToLongFunction<? super E> mapper) {
217241
218242 @ Override
219243 public DoubleStream mapToDouble (ToDoubleFunction <? super E > mapper ) {
220- // TODO
221- return null ;
244+ preconditionCheckWithClosingStream ();
245+
246+ final ToDoubleMapAction mapAction = new ToDoubleMapAction (mapper );
247+ actions .insert (actions .end ++, mapAction );
248+
249+ return new UtDoubleStream (this );
222250// preconditionCheckWithClosingStream();
223251//
224252// int size = origin.size();
@@ -274,38 +302,6 @@ public Stream<E> distinct() {
274302 return new UtStream <>(this );
275303 }
276304
277- private int distinctInvocation (Object [] curElements , Object [] distinctElements ) {
278- int distinctSize = 0 ;
279-
280- for (Object element : curElements ) {
281- boolean isDuplicate = false ;
282-
283- if (element == null ) {
284- for (int j = 0 ; j < distinctSize ; j ++) {
285- Object alreadyProcessedElement = distinctElements [j ];
286- if (alreadyProcessedElement == null ) {
287- isDuplicate = true ;
288- break ;
289- }
290- }
291- } else {
292- for (int j = 0 ; j < distinctSize ; j ++) {
293- Object alreadyProcessedElement = distinctElements [j ];
294- if (element .equals (alreadyProcessedElement )) {
295- isDuplicate = true ;
296- break ;
297- }
298- }
299- }
300-
301- if (!isDuplicate ) {
302- distinctElements [distinctSize ++] = element ;
303- }
304- }
305-
306- return distinctSize ;
307- }
308-
309305 // TODO choose the best sorting https://github.com/UnitTestBot/UTBotJava/issues/188
310306 @ Override
311307 public Stream <E > sorted () {
@@ -322,43 +318,22 @@ public Stream<E> sorted() {
322318 public Stream <E > sorted (Comparator <? super E > comparator ) {
323319 preconditionCheckWithClosingStream ();
324320
325- final SortingAction sortingAction = new SortingAction (( Comparator < Object >) comparator );
321+ final SortingAction sortingAction = new SortingAction (comparator );
326322 actions .insert (actions .end ++, sortingAction );
327323
328324 return new UtStream <>(this );
329325 }
330326
331- private void sortedInvocation (Object [] originElements , Object [] sortedElements , Comparator comparator , int size ) {
332- UtArrayMock .arraycopy (originElements , 0 , sortedElements , 0 , size );
333-
334- // bubble sort
335- for (int i = 0 ; i < size - 1 ; i ++) {
336- for (int j = 0 ; j < size - i - 1 ; j ++) {
337- if (comparator .compare (sortedElements [j ], sortedElements [j + 1 ]) > 0 ) {
338- Object tmp = sortedElements [j ];
339- sortedElements [j ] = sortedElements [j + 1 ];
340- sortedElements [j + 1 ] = tmp ;
341- }
342- }
343- }
344- }
345-
346327 @ Override
347328 public Stream <E > peek (Consumer <? super E > action ) {
348329 preconditionCheckWithoutClosing ();
349330
350- final ConsumerAction consumerAction = new ConsumerAction (( Consumer < Object >) action );
331+ final ConsumerAction consumerAction = new ConsumerAction (action );
351332 actions .insert (actions .end ++, consumerAction );
352333
353334 return new UtStream <>(this );
354335 }
355336
356- public void actionInvocation (Object [] originArray , Consumer action ) {
357- for (Object element : originArray ) {
358- action .accept (element );
359- }
360- }
361-
362337 @ Override
363338 public Stream <E > limit (long maxSize ) {
364339 preconditionCheckWithClosingStream ();
@@ -375,33 +350,6 @@ public Stream<E> limit(long maxSize) {
375350 return new UtStream <>(this );
376351 }
377352
378- private Object [] limitInvocation (Object [] originArray , long maxSize , int curSize ) {
379- if (maxSize == 0 ) {
380- return new Object []{};
381- }
382-
383- assumeOrExecuteConcretely (maxSize <= Integer .MAX_VALUE );
384-
385- int newSize = (int ) maxSize ;
386-
387- if (newSize > curSize ) {
388- newSize = curSize ;
389- }
390-
391- Object [] elements = new Object [newSize ];
392- int i = 0 ;
393-
394- for (Object element : originArray ) {
395- if (i >= newSize ) {
396- break ;
397- }
398-
399- elements [i ++] = element ;
400- }
401-
402- return elements ;
403- }
404-
405353 @ Override
406354 public Stream <E > skip (long n ) {
407355 preconditionCheckWithClosingStream ();
@@ -418,33 +366,6 @@ public Stream<E> skip(long n) {
418366 return new UtStream <>(this );
419367 }
420368
421- private Object [] skipInvocation (Object [] originArray , long n , int curSize ) {
422- if (n > curSize ) {
423- return new Object []{};
424- }
425-
426- // n is 1...Integer.MAX_VALUE here
427- int newSize = (int ) (curSize - n );
428-
429- if (newSize == 0 ) {
430- return new Object []{};
431- }
432-
433- Object [] elements = new Object [newSize ];
434- int i = 0 ;
435- int j = 0 ;
436-
437- for (Object element : originArray ) {
438- if (i ++ < n ) {
439- break ;
440- }
441-
442- elements [j ++] = element ;
443- }
444-
445- return elements ;
446- }
447-
448369 @ Override
449370 public void forEach (Consumer <? super E > action ) {
450371 peek (action );
@@ -488,7 +409,6 @@ private Object[] applyActions(Object[] originArray) {
488409
489410 for (int i = 0 ; i < actionsNumber ; i ++) {
490411 originArray = actions .get (i ).applyAction (originArray );
491- UtMock .disableClassCastExceptionCheck (originArray ); // TODO do we need it?
492412 }
493413
494414 return originArray ;
@@ -774,34 +694,4 @@ public E next() {
774694 return (E ) data [index ++];
775695 }
776696 }
777- //
778- // private final List<Object> actions = new UtArrayList<>();
779- //
780- // private Object apply() {
781- // Object[] previousElements = elementData.toArray();
782- // int size = previousElements.length;
783- //
784- // for (Object action : actions) {
785- // Object[] newElements = new Object[size];
786- // int newSize = 0;
787- //
788- // if (action instanceof Predicate) {
789- // for (Object element : previousElements) {
790- // if (((Predicate<Object>) action).test(element)) {
791- // newElements[newSize++] = element;
792- // }
793- // }
794- // } else if (action instanceof Function) {
795- // for (Object element : previousElements) {
796- // newElements[newSize++] = ((Function<Object, Object>) action).apply(element);
797- // }
798- // }
799- //
800- // size = newSize;
801- // previousElements = new Object[size];
802- // UtArrayMock.arraycopy(newElements, 0, previousElements, 0, size);
803- // }
804- //
805- // return previousElements;
806- // }
807697}
0 commit comments