Skip to content

Commit e362c9f

Browse files
committed
Using Collection as an internal storage for Stream wrappers
1 parent 8381ee2 commit e362c9f

File tree

14 files changed

+279
-232
lines changed

14 files changed

+279
-232
lines changed

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,13 @@
77

88
@UtClassMock(target = java.util.Collection.class, internalUsage = true)
99
public interface Collection<E> extends java.util.Collection<E> {
10-
@SuppressWarnings("unchecked")
1110
@Override
12-
default Stream<E> parallelStream() {
13-
Object[] data = toArray();
14-
int size = data.length;
15-
16-
return new UtStream<>((E[]) data, size);
11+
default Stream<E> stream() {
12+
return new UtStream<>(this);
1713
}
1814

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

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,16 @@ public UtArrayList(Collection<? extends E> c) {
6464
}
6565

6666
public UtArrayList(E[] data) {
67+
this(data, 0, data.length);
68+
}
69+
70+
public UtArrayList(E[] data, int startInclusive, int endExclusive) {
6771
visit(this);
68-
int length = data.length;
72+
73+
int length = endExclusive - startInclusive;
74+
6975
elementData = new RangeModifiableUnlimitedArray<>();
70-
elementData.setRange(0, data, 0, length);
76+
elementData.setRange(0, data, startInclusive, length);
7177
elementData.end = length;
7278
}
7379

@@ -372,21 +378,6 @@ public void replaceAll(UnaryOperator<E> operator) {
372378
}
373379
}
374380

375-
@Override
376-
public Stream<E> stream() {
377-
preconditionCheck();
378-
379-
int size = elementData.end;
380-
E[] data = elementData.toCastedArray(0, size);
381-
382-
return new UtStream<>(data, size);
383-
}
384-
385-
@Override
386-
public Stream<E> parallelStream() {
387-
return stream();
388-
}
389-
390381
/**
391382
* Auxiliary method, that should be only executed concretely
392383
* @return new ArrayList with all the elements from this.

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -266,21 +266,6 @@ public Iterator<E> iterator() {
266266
return new UtHashSetIterator();
267267
}
268268

269-
@Override
270-
public Stream<E> stream() {
271-
preconditionCheck();
272-
273-
int size = elementData.end;
274-
E[] data = elementData.toCastedArray(0, size);
275-
276-
return new UtStream<>(data, size);
277-
}
278-
279-
@Override
280-
public Stream<E> parallelStream() {
281-
return stream();
282-
}
283-
284269
public class UtHashSetIterator implements Iterator<E> {
285270
int index = 0;
286271

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -451,22 +451,6 @@ public Iterator<E> descendingIterator() {
451451
preconditionCheck();
452452
return new ReverseIteratorWrapper(elementData.end);
453453
}
454-
455-
@Override
456-
public Stream<E> stream() {
457-
preconditionCheck();
458-
459-
int size = elementData.end;
460-
E[] data = elementData.toCastedArray(0, size);
461-
462-
return new UtStream<>(data, size);
463-
}
464-
465-
@Override
466-
public Stream<E> parallelStream() {
467-
return stream();
468-
}
469-
470454
public class ReverseIteratorWrapper implements ListIterator<E> {
471455

472456
int index;

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,24 @@
88

99
import static org.utbot.engine.overrides.UtOverrideMock.executeConcretely;
1010

11-
@SuppressWarnings("unused")
1211
@UtClassMock(target = java.util.stream.Stream.class, internalUsage = true)
1312
public interface Stream<E> extends BaseStream<E, Stream<E>> {
1413
@SuppressWarnings("unchecked")
1514
static <E> java.util.stream.Stream<E> of(E element) {
16-
Object[] data = new Object[1];
15+
E[] data = (E[]) new Object[1];
1716
data[0] = element;
1817

19-
return new UtStream<>((E[]) data, 1);
18+
return new UtStream<>(data);
2019
}
2120

2221
@SuppressWarnings("unchecked")
2322
static <E> java.util.stream.Stream<E> of(E... elements) {
24-
int size = elements.length;
25-
26-
return new UtStream<>(elements, size);
23+
return new UtStream<>(elements);
2724
}
2825

2926
@SuppressWarnings("unchecked")
3027
static <E> java.util.stream.Stream<E> empty() {
31-
return new UtStream<>((E[]) new Object[]{}, 0);
28+
return new UtStream<>((E[]) new Object[]{});
3229
}
3330

3431
static <E> java.util.stream.Stream<E> generate(Supplier<E> s) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.utbot.engine.overrides.stream;
2+
3+
import java.util.AbstractCollection;
4+
import java.util.Collection;
5+
import java.util.Iterator;
6+
7+
public class SubCollectionView<E> extends AbstractCollection<E> {
8+
private final Collection<E> innerCollection;
9+
10+
private final int fromInclusive;
11+
private final int endExclusive;
12+
13+
public SubCollectionView(Collection<E> innerCollection, int fromInclusive, int endExclusive) {
14+
this.innerCollection = innerCollection;
15+
this.fromInclusive = fromInclusive;
16+
this.endExclusive = endExclusive;
17+
}
18+
19+
public SubCollectionView(Collection<E> innerCollection, int fromInclusive) {
20+
this(innerCollection, fromInclusive, innerCollection.size());
21+
}
22+
23+
@Override
24+
public Iterator<E> iterator() {
25+
final Iterator<E> iterator = innerCollection.iterator();
26+
27+
// skip first elements
28+
for (int i = 0; i < fromInclusive; i++) {
29+
iterator.next();
30+
}
31+
32+
return new SubCollectionViewIterator(iterator);
33+
}
34+
35+
@Override
36+
public int size() {
37+
return endExclusive - fromInclusive;
38+
}
39+
40+
private class SubCollectionViewIterator implements Iterator<E> {
41+
private final Iterator<E> innerIterator;
42+
43+
private final int index = fromInclusive;
44+
45+
public SubCollectionViewIterator(Iterator<E> innerIterator) {
46+
this.innerIterator = innerIterator;
47+
}
48+
49+
@Override
50+
public boolean hasNext() {
51+
return index < endExclusive && innerIterator.hasNext();
52+
}
53+
54+
@Override
55+
public E next() {
56+
return innerIterator.next();
57+
}
58+
}
59+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public <U> Stream<U> mapToObj(DoubleFunction<? extends U> mapper) {
139139
mapped[i] = mapper.apply(elementData.get(i));
140140
}
141141

142-
return new UtStream<>((U[]) mapped, size);
142+
return new UtStream<>((U[]) mapped);
143143
}
144144

145145
@Override
@@ -544,7 +544,7 @@ public Stream<Double> boxed() {
544544
elements[i] = elementData.get(i);
545545
}
546546

547-
return new UtStream<>(elements, size);
547+
return new UtStream<>(elements);
548548
}
549549

550550
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public <U> Stream<U> mapToObj(IntFunction<? extends U> mapper) {
140140
mapped[i] = mapper.apply(elementData.get(i));
141141
}
142142

143-
return new UtStream<>(mapped, size);
143+
return new UtStream<>(mapped);
144144
}
145145

146146
@Override
@@ -586,7 +586,7 @@ public Stream<Integer> boxed() {
586586
elements[i] = elementData.get(i);
587587
}
588588

589-
return new UtStream<>(elements, size);
589+
return new UtStream<>(elements);
590590
}
591591

592592
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public <U> Stream<U> mapToObj(LongFunction<? extends U> mapper) {
140140
mapped[i] = mapper.apply(elementData.get(i));
141141
}
142142

143-
return new UtStream<>((U[]) mapped, size);
143+
return new UtStream<>((U[]) mapped);
144144
}
145145

146146
@Override
@@ -562,7 +562,7 @@ public Stream<Long> boxed() {
562562
elements[i] = elementData.get(i);
563563
}
564564

565-
return new UtStream<>(elements, size);
565+
return new UtStream<>(elements);
566566
}
567567

568568
@Override

0 commit comments

Comments
 (0)