Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP: actions for streams implementation
  • Loading branch information
Damtev committed Sep 2, 2022
commit 42077da8d3eef1cc3daa3e75a722b10af4831b67
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.utbot.engine.overrides.stream;

import org.utbot.api.mock.UtMock;
import org.utbot.engine.overrides.UtArrayMock;
import org.utbot.engine.overrides.collections.RangeModifiableUnlimitedArray;
import org.utbot.engine.overrides.collections.UtArrayList;
Expand Down Expand Up @@ -169,7 +170,7 @@ private int filterInvocation(Object[] originArray, Object[] filtered, Predicate
public <R> Stream<R> map(Function<? super E, ? extends R> mapper) {
preconditionCheckWithClosingStream();

final MapAction mapAction = new MapAction((Function<Object, Object>) mapper);
final MapAction mapAction = new MapAction(mapper);
actions.insert(actions.end++, mapAction);

return new UtStream<>(this);
Expand Down Expand Up @@ -366,8 +367,10 @@ public Stream<E> limit(long maxSize) {
throw new IllegalArgumentException();
}

final LimitAction limitAction = new LimitAction(maxSize);
actions.insert(actions.end++, limitAction);
assumeOrExecuteConcretely(maxSize <= Integer.MAX_VALUE);

final LimitAction limitAction = new LimitAction((int) maxSize);
actions.set(actions.end++, limitAction);

return new UtStream<>(this);
}
Expand Down Expand Up @@ -461,6 +464,8 @@ public Object[] toArray() {
preconditionCheckWithClosingStream();

Object[] originArray = origin.toArray();
UtMock.disableClassCastExceptionCheck(originArray);

originArray = applyActions(originArray);

return originArray;
Expand All @@ -477,7 +482,10 @@ public <A> A[] toArray(IntFunction<A[]> generator) {

UtArrayMock.arraycopy(origin.toArray(), 0, array, 0, size);

return (A[]) applyActions(array);
final Object[] result = applyActions(array);
UtMock.disableClassCastExceptionCheck(result);

return (A[]) result;
}

@NotNull
Expand All @@ -486,6 +494,7 @@ private Object[] applyActions(Object[] originArray) {

for (int i = 0; i < actionsNumber; i++) {
originArray = actions.get(i).applyAction(originArray);
UtMock.disableClassCastExceptionCheck(originArray); // TODO do we need it?
}

return originArray;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.utbot.engine.overrides.stream.actions;

import static org.utbot.api.mock.UtMock.assumeOrExecuteConcretely;
import org.utbot.engine.overrides.UtArrayMock;

public class LimitAction implements StreamAction {
private final long maxSize;
private final int maxSize;

public LimitAction(long maxSize) {
public LimitAction(int maxSize) {
this.maxSize = maxSize;
}

Expand All @@ -15,25 +15,15 @@ public Object[] applyAction(Object[] originArray) {
return new Object[]{};
}

assumeOrExecuteConcretely(maxSize <= Integer.MAX_VALUE);

final int curSize = originArray.length;
int newSize = (int) maxSize;
int newSize = maxSize;

if (newSize > curSize) {
newSize = curSize;
}

Object[] elements = new Object[newSize];
int i = 0;

for (Object element : originArray) {
if (i >= newSize) {
break;
}

elements[i++] = element;
}
UtArrayMock.arraycopy(originArray, 0, elements, 0, newSize);

return elements;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import java.util.function.Function;

public class MapAction implements StreamAction {
private final Function<Object, Object> mapping;
@SuppressWarnings("rawtypes")
private final Function mapping;

public MapAction(Function<Object, Object> mapping) {
@SuppressWarnings("rawtypes")
public MapAction(Function mapping) {
this.mapping = mapping;
}

@SuppressWarnings("unchecked")
@Override
public Object[] applyAction(Object[] originArray) {
Object[] transformed = new Object[originArray.length];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.utbot.engine.overrides.stream.actions;

import org.utbot.engine.overrides.UtArrayMock;

public class SkipAction implements StreamAction {
private final long n;

Expand All @@ -23,16 +25,7 @@ public Object[] applyAction(Object[] originArray) {
}

Object[] elements = new Object[newSize];
int i = 0;
int j = 0;

for (Object element : originArray) {
if (i++ < n) {
break;
}

elements[j++] = element;
}
UtArrayMock.arraycopy(originArray, 0, elements, 0, newSize);

return elements;
}
Expand Down
Loading