CoinSelection: require a List of outputs, rather than a Collection

Rationale:

* The comment says that it is a "list"
* Immutable Lists are the collection that maps most easily/logically to and from Stream
* Lists are easier to enforce reproducibility (mainly for unit tests)
This commit is contained in:
Sean Gilligan 2022-07-19 10:05:25 -07:00 committed by Andreas Schildbach
parent 900af2bde9
commit e52e4d0e72

View file

@ -19,6 +19,7 @@ package org.bitcoinj.wallet;
import org.bitcoinj.base.Coin; import org.bitcoinj.base.Coin;
import org.bitcoinj.core.TransactionOutput; import org.bitcoinj.core.TransactionOutput;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -31,23 +32,23 @@ import java.util.List;
*/ */
public class CoinSelection { public class CoinSelection {
public final Coin valueGathered; public final Coin valueGathered;
public final Collection<TransactionOutput> gathered; public final List<TransactionOutput> gathered;
public CoinSelection(Collection<TransactionOutput> gathered) { public CoinSelection(List<TransactionOutput> gathered) {
this.valueGathered = sumOutputValues(gathered); this.valueGathered = sumOutputValues(gathered);
this.gathered = gathered; this.gathered = gathered;
} }
/** /**
* @deprecated use {@link #CoinSelection(Collection)} * @deprecated use {@link #CoinSelection(List)}
*/ */
@Deprecated @Deprecated
public CoinSelection(Coin valueGathered, Collection<TransactionOutput> gathered) { public CoinSelection(Coin valueGathered, Collection<TransactionOutput> gathered) {
// ignore valueGathered // ignore valueGathered
this(gathered); this(new ArrayList<>(gathered));
} }
private static Coin sumOutputValues(Collection<TransactionOutput> outputs) { private static Coin sumOutputValues(List<TransactionOutput> outputs) {
return outputs.stream() return outputs.stream()
.map(TransactionOutput::getValue) .map(TransactionOutput::getValue)
.reduce(Coin.ZERO, Coin::add); .reduce(Coin.ZERO, Coin::add);