Skip to content

Commit 4243a07

Browse files
committed
Added wrapped methods for creating streams from primitive arrays
1 parent f4c74dc commit 4243a07

File tree

6 files changed

+83
-17
lines changed

6 files changed

+83
-17
lines changed

utbot-framework/src/main/java/org/utbot/engine/overrides/collections/Collection.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,15 @@
99
public interface Collection<E> extends java.util.Collection<E> {
1010
@SuppressWarnings("unchecked")
1111
@Override
12-
default Stream<E> parallelStream() {
12+
default Stream<E> stream() {
1313
Object[] data = toArray();
1414
int size = data.length;
1515

1616
return new UtStream<>((E[]) data, size);
1717
}
1818

19-
@SuppressWarnings("unchecked")
2019
@Override
21-
default Stream<E> stream() {
22-
Object[] data = toArray();
23-
int size = data.length;
24-
25-
return new UtStream<>((E[]) data, size);
20+
default Stream<E> parallelStream() {
21+
return stream();
2622
}
2723
}

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

+51
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import org.utbot.engine.overrides.collections.UtArrayList;
55

66
import java.util.List;
7+
import java.util.stream.DoubleStream;
8+
import java.util.stream.IntStream;
9+
import java.util.stream.LongStream;
710
import java.util.stream.Stream;
811

912
@UtClassMock(target = java.util.Arrays.class, internalUsage = true)
@@ -18,6 +21,54 @@ public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusi
1821
return new UtStream<>(array, startInclusive, endExclusive);
1922
}
2023

24+
// from docs - array is assumed to be umnodified during use
25+
public static IntStream stream(int[] array, int startInclusive, int endExclusive) {
26+
int size = array.length;
27+
28+
if (startInclusive < 0 || endExclusive < startInclusive || endExclusive > size) {
29+
throw new ArrayIndexOutOfBoundsException();
30+
}
31+
32+
Integer[] data = new Integer[size];
33+
for (int i = 0; i < size; i++) {
34+
data[i] = array[i];
35+
}
36+
37+
return new UtIntStream(data, startInclusive, endExclusive);
38+
}
39+
40+
// from docs - array is assumed to be umnodified during use
41+
public static LongStream stream(long[] array, int startInclusive, int endExclusive) {
42+
int size = array.length;
43+
44+
if (startInclusive < 0 || endExclusive < startInclusive || endExclusive > size) {
45+
throw new ArrayIndexOutOfBoundsException();
46+
}
47+
48+
Long[] data = new Long[size];
49+
for (int i = 0; i < size; i++) {
50+
data[i] = array[i];
51+
}
52+
53+
return new UtLongStream(data, startInclusive, endExclusive);
54+
}
55+
56+
// from docs - array is assumed to be umnodified during use
57+
public static DoubleStream stream(double[] array, int startInclusive, int endExclusive) {
58+
int size = array.length;
59+
60+
if (startInclusive < 0 || endExclusive < startInclusive || endExclusive > size) {
61+
throw new ArrayIndexOutOfBoundsException();
62+
}
63+
64+
Double[] data = new Double[size];
65+
for (int i = 0; i < size; i++) {
66+
data[i] = array[i];
67+
}
68+
69+
return new UtDoubleStream(data, startInclusive, endExclusive);
70+
}
71+
2172
@SuppressWarnings({"unused", "varargs"})
2273
@SafeVarargs
2374
public static <T> List<T> asList(T... a) {

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,17 @@ public UtDoubleStream() {
5151
}
5252

5353
public UtDoubleStream(Double[] data, int length) {
54+
this(data, 0, length);
55+
}
56+
57+
public UtDoubleStream(Double[] data, int startInclusive, int endExclusive) {
5458
visit(this);
59+
60+
int size = endExclusive - startInclusive;
61+
5562
elementData = new RangeModifiableUnlimitedArray<>();
56-
elementData.setRange(0, data, 0, length);
57-
elementData.end = length;
63+
elementData.setRange(0, data, startInclusive, size);
64+
elementData.end = endExclusive;
5865
}
5966

6067
/**

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,17 @@ public UtIntStream() {
5252
}
5353

5454
public UtIntStream(Integer[] data, int length) {
55+
this(data, 0, length);
56+
}
57+
58+
public UtIntStream(Integer[] data, int startInclusive, int endExclusive) {
5559
visit(this);
60+
61+
int size = endExclusive - startInclusive;
62+
5663
elementData = new RangeModifiableUnlimitedArray<>();
57-
elementData.setRange(0, data, 0, length);
58-
elementData.end = length;
64+
elementData.setRange(0, data, startInclusive, size);
65+
elementData.end = endExclusive;
5966
}
6067

6168
/**

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,17 @@ public UtLongStream() {
5252
}
5353

5454
public UtLongStream(Long[] data, int length) {
55+
this(data, 0, length);
56+
}
57+
58+
public UtLongStream(Long[] data, int startInclusive, int endExclusive) {
5559
visit(this);
60+
61+
int size = endExclusive - startInclusive;
62+
5663
elementData = new RangeModifiableUnlimitedArray<>();
57-
elementData.setRange(0, data, 0, length);
58-
elementData.end = length;
64+
elementData.setRange(0, data, startInclusive, size);
65+
elementData.end = endExclusive;
5966
}
6067

6168
/**

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ public UtStream() {
5656
}
5757

5858
public UtStream(E[] data, int length) {
59-
visit(this);
60-
elementData = new RangeModifiableUnlimitedArray<>();
61-
elementData.setRange(0, data, 0, length);
62-
elementData.end = length;
59+
this(data, 0, length);
6360
}
6461

6562
public UtStream(E[] data, int startInclusive, int endExclusive) {
@@ -185,6 +182,7 @@ public DoubleStream mapToDouble(ToDoubleFunction<? super E> mapper) {
185182

186183
int size = elementData.end;
187184
Double[] data = new Double[size];
185+
assume(data.length == elementData.end);
188186
for (int i = 0; i < size; i++) {
189187
final Object object = elementData.get(i);
190188
UtMock.disableClassCastExceptionCheck(object);

0 commit comments

Comments
 (0)