mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-03-10 09:20:04 +01:00
Transaction: use Stream.reduce()
in getInputSum()
This commit is contained in:
parent
36129e53ef
commit
74b7b11681
1 changed files with 10 additions and 11 deletions
|
@ -382,20 +382,19 @@ public class Transaction extends BaseMessage {
|
|||
return IntMath.divide(getWeight(), 4, RoundingMode.CEILING); // round up
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the sum of the inputs, regardless of who owns them.
|
||||
* Gets the sum of all transaction inputs, regardless of who owns them.
|
||||
* <p>
|
||||
* <b>Warning:</b> Inputs with {@code null} {@link TransactionInput#getValue()} are silently skipped. Before completing
|
||||
* or signing a transaction you should verify that there are no inputs with {@code null} values.
|
||||
* @return The sum of all inputs with non-null values.
|
||||
*/
|
||||
public Coin getInputSum() {
|
||||
Coin inputTotal = Coin.ZERO;
|
||||
|
||||
for (TransactionInput input: inputs) {
|
||||
Coin inputValue = input.getValue();
|
||||
if (inputValue != null) {
|
||||
inputTotal = inputTotal.add(inputValue);
|
||||
}
|
||||
}
|
||||
|
||||
return inputTotal;
|
||||
return inputs.stream()
|
||||
.map(TransactionInput::getValue)
|
||||
.filter(Objects::nonNull)
|
||||
.reduce(Coin.ZERO, Coin::add);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue