28
28
import java .util .stream .LongStream ;
29
29
import java .util .stream .Stream ;
30
30
import org .jetbrains .annotations .NotNull ;
31
+ import org .utbot .engine .overrides .stream .actions .ConsumerAction ;
32
+ import org .utbot .engine .overrides .stream .actions .DistinctAction ;
33
+ import org .utbot .engine .overrides .stream .actions .FilterAction ;
34
+ import org .utbot .engine .overrides .stream .actions .LimitAction ;
35
+ import org .utbot .engine .overrides .stream .actions .MapAction ;
36
+ import org .utbot .engine .overrides .stream .actions .NaturalSortingAction ;
37
+ import org .utbot .engine .overrides .stream .actions .SkipAction ;
38
+ import org .utbot .engine .overrides .stream .actions .SortingAction ;
39
+ import org .utbot .engine .overrides .stream .actions .StreamAction ;
31
40
32
41
import static org .utbot .api .mock .UtMock .assume ;
33
42
import static org .utbot .api .mock .UtMock .assumeOrExecuteConcretely ;
@@ -43,12 +52,7 @@ public class UtStream<E> implements Stream<E>, UtGenericStorage<E> {
43
52
*/
44
53
private final Collection <E > origin ;
45
54
46
- private final RangeModifiableUnlimitedArray <Function > mappings ;
47
- private final RangeModifiableUnlimitedArray <Predicate > filters ;
48
- private final RangeModifiableUnlimitedArray <Boolean > distinctInvocations ;
49
- private final RangeModifiableUnlimitedArray <Comparator > sortingInvocations ;
50
- private final RangeModifiableUnlimitedArray <Consumer > actions ;
51
- private final RangeModifiableUnlimitedArray <Long > limits ;
55
+ private final RangeModifiableUnlimitedArray <StreamAction > actions ;
52
56
53
57
private final RangeModifiableUnlimitedArray <Runnable > closeHandlers ;
54
58
@@ -63,12 +67,7 @@ public class UtStream<E> implements Stream<E>, UtGenericStorage<E> {
63
67
public UtStream (Collection <E > collection ) {
64
68
visit (this );
65
69
66
- mappings = new RangeModifiableUnlimitedArray <>();
67
- filters = new RangeModifiableUnlimitedArray <>();
68
- distinctInvocations = new RangeModifiableUnlimitedArray <>();
69
- sortingInvocations = new RangeModifiableUnlimitedArray <>();
70
70
actions = new RangeModifiableUnlimitedArray <>();
71
- limits = new RangeModifiableUnlimitedArray <>();
72
71
closeHandlers = new RangeModifiableUnlimitedArray <>();
73
72
74
73
origin = collection ;
@@ -95,15 +94,13 @@ public UtStream(UtStream other) {
95
94
96
95
origin = other .origin ;
97
96
98
- mappings = other .mappings ;
99
- filters = other .filters ;
100
- distinctInvocations = other .distinctInvocations ;
101
- sortingInvocations = other .sortingInvocations ;
102
97
actions = other .actions ;
103
- limits = other .limits ;
104
98
105
99
isParallel = other .isParallel ;
106
- isClosed = other .isClosed ;
100
+
101
+ // new stream should be opened
102
+ isClosed = false ;
103
+
107
104
closeHandlers = other .closeHandlers ;
108
105
}
109
106
@@ -150,7 +147,8 @@ private void preconditionCheckWithClosingStream() {
150
147
public Stream <E > filter (Predicate <? super E > predicate ) {
151
148
preconditionCheckWithClosingStream ();
152
149
153
- filters .insert (filters .end ++, predicate );
150
+ final FilterAction filterAction = new FilterAction ((Predicate <Object >) predicate );
151
+ actions .insert (actions .end ++, filterAction );
154
152
155
153
return new UtStream <>(this );
156
154
}
@@ -171,7 +169,8 @@ private int filterInvocation(Object[] originArray, Object[] filtered, Predicate
171
169
public <R > Stream <R > map (Function <? super E , ? extends R > mapper ) {
172
170
preconditionCheckWithClosingStream ();
173
171
174
- mappings .insert (mappings .end ++, mapper );
172
+ final MapAction mapAction = new MapAction ((Function <Object , Object >) mapper );
173
+ actions .insert (actions .end ++, mapAction );
175
174
176
175
return new UtStream <>(this );
177
176
}
@@ -268,39 +267,8 @@ public DoubleStream flatMapToDouble(Function<? super E, ? extends DoubleStream>
268
267
public Stream <E > distinct () {
269
268
preconditionCheckWithClosingStream ();
270
269
271
- /*int size = origin.size();
272
- Object[] distinctElements = new Object[size];
273
- int distinctSize = 0;
274
-
275
- for (E element : origin) {
276
- boolean isDuplicate = false;
277
-
278
- if (element == null) {
279
- for (int j = 0; j < distinctSize; j++) {
280
- Object alreadyProcessedElement = distinctElements[j];
281
- if (alreadyProcessedElement == null) {
282
- isDuplicate = true;
283
- break;
284
- }
285
- }
286
- } else {
287
- for (int j = 0; j < distinctSize; j++) {
288
- Object alreadyProcessedElement = distinctElements[j];
289
- if (element.equals(alreadyProcessedElement)) {
290
- isDuplicate = true;
291
- break;
292
- }
293
- }
294
- }
295
-
296
- if (!isDuplicate) {
297
- distinctElements[distinctSize++] = element;
298
- }
299
- }
300
-
301
- return new UtStream<>((E[]) distinctElements, distinctSize);*/
302
-
303
- distinctInvocations .insert (distinctInvocations .end ++, true );
270
+ final DistinctAction distinctAction = new DistinctAction ();
271
+ actions .insert (actions .end ++, distinctAction );
304
272
305
273
return new UtStream <>(this );
306
274
}
@@ -342,33 +310,8 @@ private int distinctInvocation(Object[] curElements, Object[] distinctElements)
342
310
public Stream <E > sorted () {
343
311
preconditionCheckWithClosingStream ();
344
312
345
- /*int size = origin.size();
346
-
347
- if (size == 0) {
348
- return new UtStream<>();
349
- }
350
-
351
- E[] sortedElements = (E[]) new Object[size];
352
- int i = 0;
353
-
354
- for (E element : origin) {
355
- sortedElements[i++] = element;
356
- }
357
-
358
- // bubble sort
359
- for (i = 0; i < size - 1; i++) {
360
- for (int j = 0; j < size - i - 1; j++) {
361
- if (((Comparable<E>) sortedElements[j]).compareTo(sortedElements[j + 1]) > 0) {
362
- E tmp = sortedElements[j];
363
- sortedElements[j] = sortedElements[j + 1];
364
- sortedElements[j + 1] = tmp;
365
- }
366
- }
367
- }
368
-
369
- return new UtStream<>(sortedElements);*/
370
-
371
- sortingInvocations .insert (sortingInvocations .end ++, Comparator .naturalOrder ());
313
+ final NaturalSortingAction naturalSortingAction = new NaturalSortingAction ();
314
+ actions .insert (actions .end ++, naturalSortingAction );
372
315
373
316
return new UtStream <>(this );
374
317
}
@@ -378,31 +321,8 @@ public Stream<E> sorted() {
378
321
public Stream <E > sorted (Comparator <? super E > comparator ) {
379
322
preconditionCheckWithClosingStream ();
380
323
381
- /*int size = origin.size();
382
-
383
- if (size == 0) {
384
- return new UtStream<>();
385
- }
386
-
387
- E[] sortedElements = (E[]) new Object[size];
388
- int i = 0;
389
-
390
- for (E element : origin) {
391
- sortedElements[i++] = element;
392
- }
393
-
394
- // bubble sort
395
- for (i = 0; i < size - 1; i++) {
396
- for (int j = 0; j < size - i - 1; j++) {
397
- if (comparator.compare(sortedElements[j], sortedElements[j + 1]) > 0) {
398
- E tmp = sortedElements[j];
399
- sortedElements[j] = sortedElements[j + 1];
400
- sortedElements[j + 1] = tmp;
401
- }
402
- }
403
- }*/
404
-
405
- sortingInvocations .insert (sortingInvocations .end ++, comparator );
324
+ final SortingAction sortingAction = new SortingAction ((Comparator <Object >) comparator );
325
+ actions .insert (actions .end ++, sortingAction );
406
326
407
327
return new UtStream <>(this );
408
328
}
@@ -426,7 +346,8 @@ private void sortedInvocation(Object[] originElements, Object[] sortedElements,
426
346
public Stream <E > peek (Consumer <? super E > action ) {
427
347
preconditionCheckWithoutClosing ();
428
348
429
- actions .insert (actions .end ++, action );
349
+ final ConsumerAction consumerAction = new ConsumerAction ((Consumer <Object >) action );
350
+ actions .insert (actions .end ++, consumerAction );
430
351
431
352
return new UtStream <>(this );
432
353
}
@@ -445,31 +366,8 @@ public Stream<E> limit(long maxSize) {
445
366
throw new IllegalArgumentException ();
446
367
}
447
368
448
- /*if (maxSize == 0) {
449
- return new UtStream<>();
450
- }
451
-
452
- assumeOrExecuteConcretely(maxSize <= Integer.MAX_VALUE);
453
-
454
- int newSize = (int) maxSize;
455
- int curSize = origin.size();
456
-
457
- if (newSize > curSize) {
458
- newSize = curSize;
459
- }
460
-
461
- E[] elements = (E[]) new Object[newSize];
462
- int i = 0;
463
-
464
- for (E element : origin) {
465
- if (i >= newSize) {
466
- break;
467
- }
468
-
469
- elements[i++] = element;
470
- }*/
471
-
472
- limits .insert (limits .end ++, maxSize );
369
+ final LimitAction limitAction = new LimitAction (maxSize );
370
+ actions .insert (actions .end ++, limitAction );
473
371
474
372
return new UtStream <>(this );
475
373
}
@@ -514,32 +412,8 @@ public Stream<E> skip(long n) {
514
412
return new UtStream <>(this );
515
413
}
516
414
517
- /*int curSize = origin.size();
518
- if (n > curSize) {
519
- return new UtStream<>();
520
- }
521
-
522
- // n is 1...Integer.MAX_VALUE here
523
- int newSize = (int) (curSize - n);
524
-
525
- if (newSize == 0) {
526
- return new UtStream<>();
527
- }
528
-
529
- E[] elements = (E[]) new Object[newSize];
530
- int i = 0;
531
- int j = 0;
532
-
533
- for (E element : origin) {
534
- if (i++ < n) {
535
- break;
536
- }
537
-
538
- elements[j++] = element;
539
- }*/
540
-
541
- // add the negative size for skip, opposite to limit
542
- limits .insert (limits .end ++, -n );
415
+ final SkipAction skipAction = new SkipAction (n );
416
+ actions .insert (actions .end ++, skipAction );
543
417
544
418
return new UtStream <>(this );
545
419
}
@@ -606,76 +480,12 @@ public <A> A[] toArray(IntFunction<A[]> generator) {
606
480
return (A []) applyActions (array );
607
481
}
608
482
609
- @ SuppressWarnings ("StatementWithEmptyBody" )
610
483
@ NotNull
611
484
private Object [] applyActions (Object [] originArray ) {
612
- int curSize = originArray .length ;
613
-
614
- int transformationsNumber = mappings .end ;
485
+ int actionsNumber = actions .end ;
615
486
616
- for (int i = 0 ; i < transformationsNumber ; i ++) {
617
- Object [] transformed = new Object [curSize ];
618
- int newSize ;
619
-
620
- Function mapping = mappings .get (i );
621
-
622
- if (mapping != null ) {
623
- mapInvocation (originArray , transformed , mapping );
624
-
625
- originArray = transformed ;
626
- } else {
627
- Predicate filter = filters .get (i );
628
-
629
- if (filter != null ) {
630
- newSize = filterInvocation (originArray , transformed , filter );
631
-
632
- curSize = newSize ;
633
- originArray = new Object [curSize ];
634
- UtArrayMock .arraycopy (transformed , 0 , originArray , 0 , curSize );
635
- } else {
636
- Boolean isDistinctInvocation = distinctInvocations .get (i );
637
-
638
- if (isDistinctInvocation != null ) {
639
- newSize = distinctInvocation (originArray , transformed );
640
-
641
- curSize = newSize ;
642
- originArray = new Object [curSize ];
643
- UtArrayMock .arraycopy (transformed , 0 , originArray , 0 , curSize );
644
- } else {
645
- Comparator comparator = sortingInvocations .get (i );
646
-
647
- if (comparator != null ) {
648
- sortedInvocation (originArray , transformed , comparator , curSize );
649
-
650
- originArray = transformed ;
651
- } else {
652
- Consumer action = actions .get (i );
653
-
654
- if (action != null ) {
655
- actionInvocation (originArray , action );
656
- } else {
657
- Long limit = limits .get (i );
658
-
659
- if (limit != null ) {
660
- if (limit < 0 ) {
661
- // skip action
662
- transformed = skipInvocation (originArray , -limit , curSize );
663
- } else {
664
- // limit
665
- transformed = limitInvocation (originArray , limit , curSize );
666
- }
667
-
668
- curSize = transformed .length ;
669
- originArray = new Object [curSize ];
670
- UtArrayMock .arraycopy (transformed , 0 , originArray , 0 , curSize );
671
- } else {
672
- // no action is required, skip
673
- }
674
- }
675
- }
676
- }
677
- }
678
- }
487
+ for (int i = 0 ; i < actionsNumber ; i ++) {
488
+ originArray = actions .get (i ).applyAction (originArray );
679
489
}
680
490
681
491
return originArray ;
0 commit comments