mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-03-10 09:20:04 +01:00
FilteringCoinSelector: use stream in select()
Note: this also prevents mutation of the candidate list which isn't a problem as used within bitcoinj, but could be a problem if used externally.
This commit is contained in:
parent
0597c3383f
commit
7af3124f65
1 changed files with 5 additions and 7 deletions
|
@ -17,12 +17,12 @@
|
||||||
package org.bitcoinj.wallet;
|
package org.bitcoinj.wallet;
|
||||||
|
|
||||||
import org.bitcoinj.base.Coin;
|
import org.bitcoinj.base.Coin;
|
||||||
|
import org.bitcoinj.base.internal.StreamUtils;
|
||||||
import org.bitcoinj.core.TransactionOutPoint;
|
import org.bitcoinj.core.TransactionOutPoint;
|
||||||
import org.bitcoinj.core.TransactionOutput;
|
import org.bitcoinj.core.TransactionOutput;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -40,11 +40,9 @@ public class FilteringCoinSelector implements CoinSelector {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
|
public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
|
||||||
Iterator<TransactionOutput> iter = candidates.iterator();
|
List<TransactionOutput> filtered = candidates.stream()
|
||||||
while (iter.hasNext()) {
|
.filter(output -> !spent.contains(output.getOutPointFor()))
|
||||||
TransactionOutput output = iter.next();
|
.collect(StreamUtils.toUnmodifiableList());
|
||||||
if (spent.contains(output.getOutPointFor())) iter.remove();
|
return delegate.select(target, filtered);
|
||||||
}
|
|
||||||
return delegate.select(target, candidates);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue