Skip to content

Commit b2288c6

Browse files
committed
Actions for streams implementation WIP
1 parent 79edc51 commit b2288c6

File tree

13 files changed

+316
-224
lines changed

13 files changed

+316
-224
lines changed

utbot-framework/src/main/java/org/utbot/engine/overrides/stream/UtStream.java

Lines changed: 33 additions & 223 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@
2828
import java.util.stream.LongStream;
2929
import java.util.stream.Stream;
3030
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;
3140

3241
import static org.utbot.api.mock.UtMock.assume;
3342
import static org.utbot.api.mock.UtMock.assumeOrExecuteConcretely;
@@ -43,12 +52,7 @@ public class UtStream<E> implements Stream<E>, UtGenericStorage<E> {
4352
*/
4453
private final Collection<E> origin;
4554

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;
5256

5357
private final RangeModifiableUnlimitedArray<Runnable> closeHandlers;
5458

@@ -63,12 +67,7 @@ public class UtStream<E> implements Stream<E>, UtGenericStorage<E> {
6367
public UtStream(Collection<E> collection) {
6468
visit(this);
6569

66-
mappings = new RangeModifiableUnlimitedArray<>();
67-
filters = new RangeModifiableUnlimitedArray<>();
68-
distinctInvocations = new RangeModifiableUnlimitedArray<>();
69-
sortingInvocations = new RangeModifiableUnlimitedArray<>();
7070
actions = new RangeModifiableUnlimitedArray<>();
71-
limits = new RangeModifiableUnlimitedArray<>();
7271
closeHandlers = new RangeModifiableUnlimitedArray<>();
7372

7473
origin = collection;
@@ -95,15 +94,13 @@ public UtStream(UtStream other) {
9594

9695
origin = other.origin;
9796

98-
mappings = other.mappings;
99-
filters = other.filters;
100-
distinctInvocations = other.distinctInvocations;
101-
sortingInvocations = other.sortingInvocations;
10297
actions = other.actions;
103-
limits = other.limits;
10498

10599
isParallel = other.isParallel;
106-
isClosed = other.isClosed;
100+
101+
// new stream should be opened
102+
isClosed = false;
103+
107104
closeHandlers = other.closeHandlers;
108105
}
109106

@@ -150,7 +147,8 @@ private void preconditionCheckWithClosingStream() {
150147
public Stream<E> filter(Predicate<? super E> predicate) {
151148
preconditionCheckWithClosingStream();
152149

153-
filters.insert(filters.end++, predicate);
150+
final FilterAction filterAction = new FilterAction((Predicate<Object>) predicate);
151+
actions.insert(actions.end++, filterAction);
154152

155153
return new UtStream<>(this);
156154
}
@@ -171,7 +169,8 @@ private int filterInvocation(Object[] originArray, Object[] filtered, Predicate
171169
public <R> Stream<R> map(Function<? super E, ? extends R> mapper) {
172170
preconditionCheckWithClosingStream();
173171

174-
mappings.insert(mappings.end++, mapper);
172+
final MapAction mapAction = new MapAction((Function<Object, Object>) mapper);
173+
actions.insert(actions.end++, mapAction);
175174

176175
return new UtStream<>(this);
177176
}
@@ -268,39 +267,8 @@ public DoubleStream flatMapToDouble(Function<? super E, ? extends DoubleStream>
268267
public Stream<E> distinct() {
269268
preconditionCheckWithClosingStream();
270269

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);
304272

305273
return new UtStream<>(this);
306274
}
@@ -342,33 +310,8 @@ private int distinctInvocation(Object[] curElements, Object[] distinctElements)
342310
public Stream<E> sorted() {
343311
preconditionCheckWithClosingStream();
344312

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);
372315

373316
return new UtStream<>(this);
374317
}
@@ -378,31 +321,8 @@ public Stream<E> sorted() {
378321
public Stream<E> sorted(Comparator<? super E> comparator) {
379322
preconditionCheckWithClosingStream();
380323

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);
406326

407327
return new UtStream<>(this);
408328
}
@@ -426,7 +346,8 @@ private void sortedInvocation(Object[] originElements, Object[] sortedElements,
426346
public Stream<E> peek(Consumer<? super E> action) {
427347
preconditionCheckWithoutClosing();
428348

429-
actions.insert(actions.end++, action);
349+
final ConsumerAction consumerAction = new ConsumerAction((Consumer<Object>) action);
350+
actions.insert(actions.end++, consumerAction);
430351

431352
return new UtStream<>(this);
432353
}
@@ -445,31 +366,8 @@ public Stream<E> limit(long maxSize) {
445366
throw new IllegalArgumentException();
446367
}
447368

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);
473371

474372
return new UtStream<>(this);
475373
}
@@ -514,32 +412,8 @@ public Stream<E> skip(long n) {
514412
return new UtStream<>(this);
515413
}
516414

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);
543417

544418
return new UtStream<>(this);
545419
}
@@ -606,76 +480,12 @@ public <A> A[] toArray(IntFunction<A[]> generator) {
606480
return (A[]) applyActions(array);
607481
}
608482

609-
@SuppressWarnings("StatementWithEmptyBody")
610483
@NotNull
611484
private Object[] applyActions(Object[] originArray) {
612-
int curSize = originArray.length;
613-
614-
int transformationsNumber = mappings.end;
485+
int actionsNumber = actions.end;
615486

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);
679489
}
680490

681491
return originArray;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.utbot.engine.overrides.stream.actions;
2+
3+
import java.util.function.Consumer;
4+
5+
public class ConsumerAction implements StreamAction {
6+
private final Consumer<Object> action;
7+
8+
public ConsumerAction(Consumer<Object> action) {
9+
this.action = action;
10+
}
11+
12+
@Override
13+
public Object[] applyAction(Object[] originArray) {
14+
for (Object element : originArray) {
15+
action.accept(element);
16+
}
17+
18+
return originArray;
19+
}
20+
}

0 commit comments

Comments
 (0)