Collapse foreach loops using Stream#collect
This commit is contained in:
parent
d099100adf
commit
003249cb77
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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()][]);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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) {
|
||||
|
||||
|
|
Loading…
Reference in New Issue