Skip to content

Commit

Permalink
Merge b23a588 into 59d5cee
Browse files Browse the repository at this point in the history
  • Loading branch information
epkugelmass committed Jul 28, 2017
2 parents 59d5cee + b23a588 commit 9a989df
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Check also [MIGRATION.md](MIGRATION.md) for possible compatibility problems.

### 0.6.7
* Added: `skipLast` method for all Stream types.

### 0.6.6
* [#145] Added: `intersperse` method for all stream types.

Expand Down
1 change: 1 addition & 0 deletions CHEATSHEET.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Select entries which keys or values are instances of given class | `EntryStream.
Take stream elements while the condition is true | `any.takeWhile()`
Take stream elements while the condition is true including first violating element | `any.takeWhileInclusive()`
Skip stream elements while the condition is true | `any.dropWhile()`
Skip the last n elements | `any.skipLast()`

### mapping

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>one.util</groupId>
<artifactId>streamex</artifactId>
<version>0.6.6-SNAPSHOT</version>
<version>0.6.7-SNAPSHOT</version>
<packaging>jar</packaging>

<name>StreamEx</name>
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/one/util/streamex/AbstractStreamEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package one.util.streamex;

import java.util.*;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.*;
import java.util.stream.*;
import java.util.stream.Collector.Characteristics;
Expand Down Expand Up @@ -252,6 +254,61 @@ public S skip(long n) {
return supply(stream().skip(n));
}

/**
* Returns a new, non-splittable, sequential stream containing
* all the elements of the original stream except the last n elements.
* Consumes space proportional to the number of elements skipped.
*
* <p>
* For example, {@code StreamEx.of("a", "b", "c").skipLast(1)} will yield a stream containing
* two elements: a, b.
*
* <p>
* This is an <a href="package-summary.html#StreamOps">quasi-intermediate operation</a>.
*
* @param n the non-negative number of elements to leave off the end of the stream.
* @return the new, sequential stream
* @throws IllegalArgumentException if {@code n} is negative or {@link Integer#MAX_VALUE}
* @since 0.6.7
*/
public S skipLast(int n) {
if (n < 0 || n == Integer.MAX_VALUE)
throw new IllegalArgumentException(Long.toString(n));
if (n == 0)
return supply(this);

BlockingDeque<T> buffer = new LinkedBlockingDeque<>(n + 1);
Spliterator<T> source = this.spliterator();

return supply(StreamEx.of(new Spliterator<T>() {

@Override
public boolean tryAdvance(Consumer<? super T> action) {
while (buffer.remainingCapacity() > 0 && source.tryAdvance(buffer::offer)) { }
if (buffer.size() > n) {
action.accept(buffer.poll());
return true;
}
return false;
}

@Override
public Spliterator<T> trySplit() {
return null;
}

@Override
public long estimateSize() {
return source.estimateSize() - n;
}

@Override
public int characteristics() {
return source.characteristics();
}
})).sequential();
}

@Override
public void forEach(Consumer<? super T> action) {
if (spliterator != null && !isParallel()) {
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/one/util/streamex/DoubleStreamEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import java.util.*;
import java.util.Map.Entry;
import java.util.PrimitiveIterator.OfDouble;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.*;
import java.util.stream.*;

Expand Down Expand Up @@ -580,6 +582,61 @@ public DoubleStreamEx skipOrdered(long n) {
return delegate(spliterator);
}

/**
* Returns a new, non-splittable, sequential stream containing
* all the elements of the original stream except the last n elements.
* Consumes space proportional to the number of elements skipped.
*
* <p>
* For example, {@code DoubleStreamEx.of(1d, 2d, 3d).skipLast(1)} will yield a stream containing
* two elements: 1d, 2d.
*
* <p>
* This is an <a href="package-summary.html#StreamOps">quasi-intermediate operation</a>.
*
* @param n the non-negative number of elements to leave off the end of the stream.
* @return the new, sequential stream
* @throws IllegalArgumentException if {@code n} is negative or {@link Integer#MAX_VALUE}
* @since 0.6.7
*/
public DoubleStreamEx skipLast(int n) {
if (n < 0 || n == Integer.MAX_VALUE)
throw new IllegalArgumentException(Long.toString(n));
if (n == 0)
return this;

BlockingDeque<Double> buffer = new LinkedBlockingDeque<>(n + 1);
Spliterator.OfDouble source = this.spliterator();

return delegate(new Spliterator.OfDouble() {

@Override
public boolean tryAdvance(DoubleConsumer action) {
while (buffer.remainingCapacity() > 0 && source.tryAdvance((DoubleConsumer) buffer::offer)) { }
if (buffer.size() > n) {
action.accept(buffer.poll());
return true;
}
return false;
}

@Override
public Spliterator.OfDouble trySplit() {
return null;
}

@Override
public long estimateSize() {
return source.estimateSize() - n;
}

@Override
public int characteristics() {
return source.characteristics();
}
}).sequential();
}

@Override
public void forEach(DoubleConsumer action) {
if (spliterator != null && !isParallel()) {
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/one/util/streamex/IntStreamEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.util.Map.Entry;
import java.util.PrimitiveIterator.OfInt;
import java.util.Spliterators.AbstractIntSpliterator;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.*;
import java.util.stream.*;

Expand Down Expand Up @@ -620,6 +622,61 @@ public IntStreamEx skipOrdered(long n) {
return delegate(spliterator);
}

/**
* Returns a new, non-splittable, sequential stream containing
* all the elements of the original stream except the last n elements.
* Consumes space proportional to the number of elements skipped.
*
* <p>
* For example, {@code IntStreamEx.of(1, 2, 3).skipLast(1)} will yield a stream containing
* two elements: 1, 2.
*
* <p>
* This is an <a href="package-summary.html#StreamOps">quasi-intermediate operation</a>.
*
* @param n the non-negative number of elements to leave off the end of the stream.
* @return the new, sequential stream
* @throws IllegalArgumentException if {@code n} is negative or {@link Integer#MAX_VALUE}
* @since 0.6.7
*/
public IntStreamEx skipLast(int n) {
if (n < 0 || n == Integer.MAX_VALUE)
throw new IllegalArgumentException(Long.toString(n));
if (n == 0)
return this;

BlockingDeque<Integer> buffer = new LinkedBlockingDeque<>(n + 1);
Spliterator.OfInt source = this.spliterator();

return delegate(new Spliterator.OfInt() {

@Override
public boolean tryAdvance(IntConsumer action) {
while (buffer.remainingCapacity() > 0 && source.tryAdvance((IntConsumer) buffer::offer)) { }
if (buffer.size() > n) {
action.accept(buffer.poll());
return true;
}
return false;
}

@Override
public Spliterator.OfInt trySplit() {
return null;
}

@Override
public long estimateSize() {
return source.estimateSize() - n;
}

@Override
public int characteristics() {
return source.characteristics();
}
}).sequential();
}

@Override
public void forEach(IntConsumer action) {
if (spliterator != null && !isParallel()) {
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/one/util/streamex/LongStreamEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import java.util.*;
import java.util.Map.Entry;
import java.util.PrimitiveIterator.OfLong;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.*;
import java.util.stream.*;

Expand Down Expand Up @@ -645,6 +647,61 @@ public LongStreamEx skipOrdered(long n) {
return delegate(spliterator);
}

/**
* Returns a new, non-splittable, sequential stream containing
* all the elements of the original stream except the last n elements.
* Consumes space proportional to the number of elements skipped.
*
* <p>
* For example, {@code IntStreamEx.of(1L, 2L, 3L).skipLast(1)} will yield a stream containing
* two elements: 1L, 2L.
*
* <p>
* This is an <a href="package-summary.html#StreamOps">quasi-intermediate operation</a>.
*
* @param n the non-negative number of elements to leave off the end of the stream.
* @return the new, sequential stream
* @throws IllegalArgumentException if {@code n} is negative or {@link Integer#MAX_VALUE}
* @since 0.6.7
*/
public LongStreamEx skipLast(int n) {
if (n < 0 || n == Integer.MAX_VALUE)
throw new IllegalArgumentException(Long.toString(n));
if (n == 0)
return this;

BlockingDeque<Long> buffer = new LinkedBlockingDeque<>(n + 1);
Spliterator.OfLong source = this.spliterator();

return delegate(new Spliterator.OfLong() {

@Override
public boolean tryAdvance(LongConsumer action) {
while (buffer.remainingCapacity() > 0 && source.tryAdvance((LongConsumer) buffer::offer)) { }
if (buffer.size() > n) {
action.accept(buffer.poll());
return true;
}
return false;
}

@Override
public Spliterator.OfLong trySplit() {
return null;
}

@Override
public long estimateSize() {
return source.estimateSize() - n;
}

@Override
public int characteristics() {
return source.characteristics();
}
}).sequential();
}

@Override
public void forEach(LongConsumer action) {
if (spliterator != null && !isParallel()) {
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/one/util/streamex/DoubleStreamExTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -500,4 +500,14 @@ public void testIntersperse() {
.toArray(), 0.0);
assertEquals(0L, IntStreamEx.empty().intersperse(1).count());
}

@Test
public void testSkipLast() {
Supplier<DoubleStreamEx> s = () -> DoubleStreamEx.of(1d, 2d, 3d);
assertArrayEquals(new double[] {1d, 2d}, s.get().skipLast(1).toArray(), 0.0);
assertArrayEquals(new double[0], s.get().skipLast(3).toArray(), 0.0);
assertArrayEquals(new double[0], s.get().skipLast(4).toArray(), 0.0);
assertArrayEquals(new double[0], s.get().skipLast(10).toArray(), 0.0);
assertArrayEquals(new double[0], DoubleStreamEx.empty().skipLast(10).toArray(), 0.0);
}
}
10 changes: 10 additions & 0 deletions src/test/java/one/util/streamex/IntStreamExTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -777,4 +777,14 @@ public void testIntersperse() {
.toArray());
assertEquals(0L, IntStreamEx.empty().intersperse(1).count());
}

@Test
public void testSkipLast() {
Supplier<IntStreamEx> s = () -> IntStreamEx.of(1, 2, 3);
assertArrayEquals(new int[] {1, 2}, s.get().skipLast(1).toArray());
assertArrayEquals(new int[0], s.get().skipLast(3).toArray());
assertArrayEquals(new int[0], s.get().skipLast(4).toArray());
assertArrayEquals(new int[0], s.get().skipLast(10).toArray());
assertArrayEquals(new int[0], IntStreamEx.empty().skipLast(10).toArray());
}
}
10 changes: 10 additions & 0 deletions src/test/java/one/util/streamex/LongStreamExTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,14 @@ public void testIntersperse() {
.toArray());
assertEquals(0L, IntStreamEx.empty().intersperse(1).count());
}

@Test
public void testSkipLast() {
Supplier<LongStreamEx> s = () -> LongStreamEx.of(1L, 2L, 3L);
assertArrayEquals(new long[] {1L, 2L}, s.get().skipLast(1).toArray());
assertArrayEquals(new long[0], s.get().skipLast(3).toArray());
assertArrayEquals(new long[0], s.get().skipLast(4).toArray());
assertArrayEquals(new long[0], s.get().skipLast(10).toArray());
assertArrayEquals(new long[0], LongStreamEx.empty().skipLast(10).toArray());
}
}
10 changes: 10 additions & 0 deletions src/test/java/one/util/streamex/StreamExTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2069,4 +2069,14 @@ public void testIntersperse() {
streamEx(asList("a", "b", "c", "d", "e")::stream, s -> assertEquals(expected, s.get().intersperse("--").toList()));
assertEquals(Collections.emptyList(), StreamEx.empty().intersperse("xyz").toList());
}

@Test
public void testSkipLast() {
streamEx(asList("a", "b", "c")::stream, s -> assertEquals(asList("a", "b", "c"), s.get().skipLast(0).toList()));
streamEx(asList("a", "b", "c")::stream, s -> assertEquals(asList("a", "b"), s.get().skipLast(1).toList()));
streamEx(asList("a", "b", "c")::stream, s -> assertEquals(asList(), s.get().skipLast(3).toList()));
streamEx(asList("a", "b", "c")::stream, s -> assertEquals(asList(), s.get().skipLast(4).toList()));
streamEx(asList("a", "b", "c")::stream, s -> assertEquals(asList(), s.get().skipLast(10).toList()));
assertEquals(asList(), StreamEx.empty().skipLast(3).toList());
}
}

0 comments on commit 9a989df

Please sign in to comment.