mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2024-11-19 01:40:26 +01:00
CoinSelector: new static method fromPredicate()
to create CoinSelector
Use it in `ForwardingService`.
This commit is contained in:
parent
21200c15cc
commit
d45cccc8d6
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user