Skip to content

Commit

Permalink
Add stream() method to Table
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jun 19, 2019
1 parent 494b265 commit 1cf3dcd
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions core/src/main/java/tech/tablesaw/api/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;

import com.google.common.base.Preconditions;
import com.google.common.collect.Iterators;
import com.google.common.collect.Streams;
import com.google.common.primitives.Ints;

import io.github.classgraph.ClassGraph;
Expand Down Expand Up @@ -1024,50 +1027,35 @@ public boolean hasNext() {
};
}

public Stream<Row> stream() {
return Streams.stream(iterator());
}

/**
* Applies the operation in {@code doable} to every row in the table
* @deprecated use {@code stream().forEach}
*/
public void doWithRows(Consumer<Row> doable) {
Row row = new Row(this);
while (row.hasNext()) {
doable.accept(row.next());
}
stream().forEach(doable);
}

/**
* Applies the predicate to each row, and return true if any row returns true
* @deprecated use {@code stream().anyMatch}
*/
public boolean detect(Predicate<Row> predicate) {
Row row = new Row(this);
while (row.hasNext()) {
if (predicate.test(row.next())) {
return true;
}
}
return false;
return stream().anyMatch(predicate);
}

/**
* Applies the operation in {@code rowConsumer} to every series of n rows in the table
* @deprecated use {@code Streams.stream(Iterators.partition(iterator(), n)).forEach}
*/
public void stepWithRows(Consumer<Row[]> rowConsumer, int n) {
if (isEmpty()) {
return;
}
Row[] rows = new Row[n];
for (int i = 0; i < n; i++) {
rows[i] = new Row(this);
}

int max = rowCount() / n;

for (int i = 0; i < max; i++) { //0, 1
for (int r = 1; r <= n; r++) {
int row = i*n + r - 1;
rows[r-1].at(row);
}
rowConsumer.accept(rows);
}
Streams
.stream(Iterators.partition(iterator(), n))
.map(list -> list.toArray(new Row[list.size()]))
.forEach(rowConsumer);
}

/**
Expand Down

0 comments on commit 1cf3dcd

Please sign in to comment.