Skip to content

Commit

Permalink
Code cleanup: Simplify Optional stream processing
Browse files Browse the repository at this point in the history
Use flatMap(Optional::stream) instead of filter(..isPresent).map(..get)
and avoid a redundantly nested Optional in OpReturnType.

Also replace some unnecessary stream().forEach(..) invocations on lists
in BtcWalletService, as forEach is already part of the List interface.
  • Loading branch information
stejbac committed Apr 27, 2021
1 parent de8213a commit ce73fb8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
16 changes: 7 additions & 9 deletions core/src/main/java/bisq/core/btc/wallet/BtcWalletService.java
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,10 @@ public Transaction completePreparedBsqTx(Transaction preparedBsqTx,
}

Transaction tx = new Transaction(params);
preparedBsqTxInputs.stream().forEach(tx::addInput);
preparedBsqTxInputs.forEach(tx::addInput);

if (forcedChangeValue.isZero()) {
preparedBsqTxOutputs.stream().forEach(tx::addOutput);
preparedBsqTxOutputs.forEach(tx::addOutput);
} else {
//TODO test that case
checkArgument(preparedBsqTxOutputs.size() == 0, "preparedBsqTxOutputs.size must be null in that code branch");
Expand Down Expand Up @@ -598,10 +598,10 @@ private Tuple2<Integer, Integer> getNumInputs(Transaction tx) {
} else if (ScriptPattern.isP2WPKH(connectedOutput.getScriptPubKey())) {
numSegwitInputs++;
} else {
throw new IllegalArgumentException("Inputs should spend a P2PKH, P2PK or P2WPKH ouput");
throw new IllegalArgumentException("Inputs should spend a P2PKH, P2PK or P2WPKH output");
}
}
return new Tuple2(numLegacyInputs, numSegwitInputs);
return new Tuple2<>(numLegacyInputs, numSegwitInputs);
}


Expand Down Expand Up @@ -871,7 +871,7 @@ public void doubleSpendTransaction(String txId, Runnable resultHandler, ErrorMes
log.debug("txToDoubleSpend no. of inputs " + txToDoubleSpend.getInputs().size());

Transaction newTransaction = new Transaction(params);
txToDoubleSpend.getInputs().stream().forEach(input -> {
txToDoubleSpend.getInputs().forEach(input -> {
final TransactionOutput connectedOutput = input.getConnectedOutput();
if (connectedOutput != null &&
connectedOutput.isMine(wallet) &&
Expand Down Expand Up @@ -1073,8 +1073,7 @@ public Transaction getFeeEstimationTransactionForMultipleAddresses(Set<String> f
addressEntryOptional = findAddressEntry(address, AddressEntry.Context.ARBITRATOR);
return addressEntryOptional;
})
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
if (addressEntries.isEmpty())
throw new AddressEntryException("No Addresses for withdraw found in our wallet");
Expand Down Expand Up @@ -1255,8 +1254,7 @@ private SendRequest getSendRequestForMultipleAddresses(Set<String> fromAddresses
addressEntryOptional = findAddressEntry(address, AddressEntry.Context.ARBITRATOR);
return addressEntryOptional;
})
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
if (addressEntries.isEmpty())
throw new AddressEntryException("No Addresses for withdraw found in our wallet");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ private Cycle getFirstCycle() {
// We add the default values from the Param enum to our StateChangeEvent list.
List<DaoPhase> daoPhasesWithDefaultDuration = Arrays.stream(DaoPhase.Phase.values())
.map(this::getPhaseWithDefaultDuration)
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(Optional::stream)
.collect(Collectors.toList());
return new Cycle(genesisBlockHeight, ImmutableList.copyOf(daoPhasesWithDefaultDuration));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ public enum OpReturnType implements ImmutableDaoStateModel {
public static Optional<OpReturnType> getOpReturnType(byte type) {
return Arrays.stream(OpReturnType.values())
.filter(opReturnType -> opReturnType.type == type)
.map(Optional::of)
.findAny()
.orElse(Optional.empty());
.findAny();
}
}

0 comments on commit ce73fb8

Please sign in to comment.