mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-01-18 21:32:35 +01:00
BlockFileLoader: add stream()
This commit is contained in:
parent
46b2704f79
commit
18126e368b
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user