CoinSelector: new static method fromPredicate() to create CoinSelector

Use it in `ForwardingService`.
This commit is contained in:
Sean Gilligan 2023-08-25 14:53:09 -07:00 committed by Andreas Schildbach
parent 21200c15cc
commit d45cccc8d6
2 changed files with 16 additions and 3 deletions

View File

@ -17,9 +17,13 @@
package org.bitcoinj.wallet;
import org.bitcoinj.base.Coin;
import org.bitcoinj.base.internal.StreamUtils;
import org.bitcoinj.core.TransactionOutput;
import java.util.List;
import java.util.function.Predicate;
import static java.util.stream.Collectors.collectingAndThen;
/**
* A CoinSelector is responsible for picking some outputs to spend, from the list of all possible outputs. It
@ -34,4 +38,15 @@ public interface CoinSelector {
* of {@link DefaultCoinSelector}.
*/
CoinSelection select(Coin target, List<TransactionOutput> candidates);
/**
* Create a {@code CoinSelector} from a predicate function that filters a single {@link TransactionOutput}
* @param predicate Returns true if a "coin" ({@code TransactionOutput}) should be included.
* @return A CoinSelector that only returns coins matching the predicate
*/
static CoinSelector fromPredicate(Predicate<TransactionOutput> predicate) {
return (target, candidates) -> candidates.stream()
.filter(predicate)
.collect(collectingAndThen(StreamUtils.toUnmodifiableList(), CoinSelection::new));
}
}

View File

@ -194,8 +194,6 @@ public class ForwardingService implements Closeable {
*/
static CoinSelector forwardingCoinSelector(Sha256Hash parentTxId) {
Objects.requireNonNull(parentTxId);
return (target, candidates) -> candidates.stream()
.filter(output -> Objects.equals(output.getParentTransactionHash(), parentTxId))
.collect(collectingAndThen(toList(), CoinSelection::new));
return CoinSelector.fromPredicate(output -> Objects.equals(output.getParentTransactionHash(), parentTxId));
}
}