Collapse foreach loops using Stream#collect

This commit is contained in:
Chris Beams 2014-12-27 02:44:46 +01:00
parent d099100adf
commit 003249cb77
No known key found for this signature in database
GPG Key ID: 3D214F8F5BC5ED73
7 changed files with 33 additions and 27 deletions

View File

@ -15,6 +15,7 @@ import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Roman Mandeleil
@ -73,9 +74,7 @@ public class BlockStore {
setParameter("limit", block.getNumber() - qty).
setMaxResults(qty).list();
for (byte[] h : result) {
hashes.add(h);
}
hashes.addAll(result.stream().collect(Collectors.toList()));
return hashes;
}

View File

@ -153,13 +153,13 @@ public class AccountState {
checked.add(key);
}
for (DataWord key : expectedKeys) {
if (!checked.contains(key)) {
String formatedString = String.format("Account: %s: doesn't exist expected storage key: %s",
Hex.toHexString(this.address), key.toString());
results.add(formatedString);
}
}
expectedKeys.stream()
.filter(key -> !checked.contains(key))
.forEach(key -> {
String formatedString = String.format("Account: %s: doesn't exist expected storage key: %s",
Hex.toHexString(this.address), key.toString());
results.add(formatedString);
});
return results;
}

View File

@ -7,6 +7,7 @@ import org.ethereum.util.Utils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static org.ethereum.net.eth.EthMessageCodes.BLOCK_HASHES;
@ -45,8 +46,10 @@ public class BlockHashesMessage extends EthMessage {
private void encode() {
List<byte[]> encodedElements = new ArrayList<>();
encodedElements.add(RLP.encodeByte(BLOCK_HASHES.asByte()));
for (byte[] blockHash : blockHashes)
encodedElements.add(RLP.encodeElement(blockHash));
encodedElements.addAll(
blockHashes.stream()
.map(RLP::encodeElement)
.collect(Collectors.toList()));
byte[][] encodedElementArray = encodedElements
.toArray(new byte[encodedElements.size()][]);
this.encoded = RLP.encodeList(encodedElementArray);

View File

@ -7,6 +7,7 @@ import org.ethereum.util.RLPList;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.stream.Collectors;
import static org.ethereum.net.eth.EthMessageCodes.BLOCKS;
@ -45,9 +46,10 @@ public class BlocksMessage extends EthMessage {
List<byte[]> encodedElements = new Vector<>();
encodedElements.add(RLP.encodeByte(BLOCKS.asByte()));
for (Block block : blocks) {
encodedElements.add(block.getEncoded());
}
encodedElements.addAll(
blocks.stream()
.map(Block::getEncoded)
.collect(Collectors.toList()));
byte[][] encodedElementArray = encodedElements
.toArray(new byte[encodedElements.size()][]);

View File

@ -6,6 +6,7 @@ import org.ethereum.util.Utils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static org.ethereum.net.eth.EthMessageCodes.GET_BLOCKS;
@ -43,8 +44,10 @@ public class GetBlocksMessage extends EthMessage {
private void encode() {
List<byte[]> encodedElements = new ArrayList<>();
encodedElements.add(RLP.encodeByte(GET_BLOCKS.asByte()));
for (byte[] hash : blockHashes)
encodedElements.add(RLP.encodeElement(hash));
encodedElements.addAll(
blockHashes.stream()
.map(RLP::encodeElement)
.collect(Collectors.toList()));
byte[][] encodedElementArray = encodedElements
.toArray(new byte[encodedElements.size()][]);
this.encoded = RLP.encodeList(encodedElementArray);

View File

@ -8,6 +8,7 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static org.ethereum.net.eth.EthMessageCodes.TRANSACTIONS;
@ -51,8 +52,10 @@ public class TransactionsMessage extends EthMessage {
private void encode() {
List<byte[]> encodedElements = new ArrayList<>();
encodedElements.add(RLP.encodeByte(TRANSACTIONS.asByte()));
for (Transaction tx : transactions)
encodedElements.add(tx.getEncoded());
encodedElements.addAll(
transactions.stream()
.map(Transaction::getEncoded)
.collect(Collectors.toList()));
byte[][] encodedElementArray = encodedElements
.toArray(new byte[encodedElements.size()][]);
this.encoded = RLP.encodeList(encodedElementArray);

View File

@ -12,10 +12,10 @@ import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static java.util.Arrays.copyOfRange;
import static org.ethereum.crypto.HashUtil.EMPTY_TRIE_HASH;
@ -450,13 +450,9 @@ public class TrieImpl implements Trie {
Set<byte[]> hashSet = collectAction.getCollectedHashes();
Map<ByteArrayWrapper, Node> nodes = this.getCache().getNodes();
Set<ByteArrayWrapper> toRemoveSet = new HashSet<>();
for (ByteArrayWrapper key : nodes.keySet()) {
if (!hashSet.contains(key.getData())) {
toRemoveSet.add(key);
}
}
Set<ByteArrayWrapper> toRemoveSet = nodes.keySet().stream()
.filter(key -> !hashSet.contains(key.getData()))
.collect(Collectors.toSet());
for (ByteArrayWrapper key : toRemoveSet) {