BlockFileLoader: add stream()

This commit is contained in:
Sean Gilligan 2023-08-13 13:33:19 -07:00 committed by Andreas Schildbach
parent 46b2704f79
commit 18126e368b
2 changed files with 44 additions and 0 deletions

View File

@ -33,6 +33,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static org.bitcoinj.base.internal.Preconditions.checkArgument;
@ -212,4 +214,8 @@ public class BlockFileLoader implements Iterable<Block> {
public Iterator<Block> iterator() {
return new BlockIterator(files);
}
public Stream<Block> stream() {
return StreamSupport.stream(spliterator(), false);
}
}

View File

@ -67,4 +67,42 @@ public class BlockFileLoaderTest {
assertEquals(439, blockCount2);
}
@Test
public void streamFirst100kCount() {
File blockFile = new File(getClass().getResource("../core/first-100k-blocks.dat").getFile());
BlockFileLoader loader = new BlockFileLoader(BitcoinNetwork.MAINNET, Collections.singletonList(blockFile));
long blockCount = loader.stream().count();
assertEquals(439, blockCount);
}
@Test
public void streamFirst100kTwice() {
File blockFile = new File(getClass().getResource("../core/first-100k-blocks.dat").getFile());
BlockFileLoader loader = new BlockFileLoader(BitcoinNetwork.MAINNET, Collections.singletonList(blockFile));
long blockCount = loader.stream().count();
assertEquals(439, blockCount);
long blockCount2 = loader.stream().count();
assertEquals(439, blockCount2);
}
@Test
public void streamFirst100kCountTransactions() {
File blockFile = new File(getClass().getResource("../core/first-100k-blocks.dat").getFile());
BlockFileLoader loader = new BlockFileLoader(BitcoinNetwork.MAINNET, Collections.singletonList(blockFile));
long transactionCount = loader.stream()
.map(Block::getTransactions)
.filter(Objects::nonNull)
.mapToLong(Collection::size)
.sum();
assertEquals(446, transactionCount);
}
}