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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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