Skip to content

Commit

Permalink
use long parameters for Stream skip, get, mapWithIndex operators (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmoten authored Jan 31, 2024
1 parent 0dd9190 commit ed0d7b7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
17 changes: 8 additions & 9 deletions kool/src/main/java/org/davidmoten/kool/Indexed.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
package org.davidmoten.kool;

import java.util.Objects;

import com.github.davidmoten.guavamini.Preconditions;

public final class Indexed<T> {

private final T t;
private final int index;
private final long index;

private Indexed(T t, int index) {
private Indexed(T t, long index) {
Preconditions.checkNotNull(t);
this.t = t;
this.index = index;
}

public static <T> Indexed<T> create(T t, int index) {
public static <T> Indexed<T> create(T t, long index) {
return new Indexed<T>(t, index);
}

public T value() {
return t;
}

public int index() {
public long index() {
return index;
}


@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + index;
result = prime * result + t.hashCode();
return result;
return Objects.hash(index, t);
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions kool/src/main/java/org/davidmoten/kool/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ default Maybe<T> last() {
return new Last<T>(this);
}

default Maybe<T> get(int index) {
default Maybe<T> get(long index) {
return take(index + 1).last();
}

Expand Down Expand Up @@ -842,7 +842,7 @@ default Stream<List<T>> buffer(int size, int step) {
return new Buffer<T>(this, size, step);
}

default Stream<T> skip(int size) {
default Stream<T> skip(long size) {
return new Skip<T>(size, this);
}

Expand Down Expand Up @@ -917,11 +917,11 @@ default <S> Stream<S> bufferWhile(Callable<? extends S> factory,
step, maxReplay);
}

default Stream<Indexed<T>> mapWithIndex(int startIndex) {
default Stream<Indexed<T>> mapWithIndex(long startIndex) {
return defer(() -> {
int[] index = new int[] { startIndex };
long[] index = new long[] { startIndex };
return map(x -> {
int n = index[0];
long n = index[0];
index[0] = n + 1;
return Indexed.create(x, n);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

public final class Skip<T> implements Stream<T> {

private final int count;
private final long count;
private final Stream<T> source;

public Skip(int count, Stream<T> source) {
public Skip(long count, Stream<T> source) {
this.count = count;
this.source = source;
}
Expand All @@ -18,7 +18,7 @@ public StreamIterator<T> iterator() {
return new StreamIterator<T>() {

StreamIterator<T> it = source.iteratorNullChecked();
int n = count;
long n = count;

@Override
public boolean hasNext() {
Expand Down

0 comments on commit ed0d7b7

Please sign in to comment.