synchronize closed and failed trade managers

This commit is contained in:
woodser 2023-02-21 11:12:08 -05:00
parent 28c07e966b
commit 86511b4e21
2 changed files with 67 additions and 37 deletions

View file

@ -109,15 +109,19 @@ public class ClosedTradableManager implements PersistedDataHost {
}
public void add(Tradable tradable) {
if (closedTradables.add(tradable)) {
maybeClearSensitiveData();
requestPersistence();
synchronized (closedTradables) {
if (closedTradables.add(tradable)) {
maybeClearSensitiveData();
requestPersistence();
}
}
}
public void remove(Tradable tradable) {
if (closedTradables.remove(tradable)) {
requestPersistence();
synchronized (closedTradables) {
if (closedTradables.remove(tradable)) {
requestPersistence();
}
}
}
@ -126,7 +130,9 @@ public class ClosedTradableManager implements PersistedDataHost {
}
public ObservableList<Tradable> getObservableList() {
return closedTradables.getObservableList();
synchronized (closedTradables) {
return closedTradables.getObservableList();
}
}
public List<Tradable> getTradableList() {
@ -134,43 +140,55 @@ public class ClosedTradableManager implements PersistedDataHost {
}
public List<Trade> getClosedTrades() {
return ImmutableList.copyOf(getObservableList().stream()
.filter(e -> e instanceof Trade)
.map(e -> (Trade) e)
.collect(Collectors.toList()));
synchronized (closedTradables) {
return ImmutableList.copyOf(getObservableList().stream()
.filter(e -> e instanceof Trade)
.map(e -> (Trade) e)
.collect(Collectors.toList()));
}
}
public List<OpenOffer> getCanceledOpenOffers() {
return ImmutableList.copyOf(getObservableList().stream()
.filter(e -> (e instanceof OpenOffer) && ((OpenOffer) e).getState().equals(CANCELED))
.map(e -> (OpenOffer) e)
.collect(Collectors.toList()));
synchronized (closedTradables) {
return ImmutableList.copyOf(getObservableList().stream()
.filter(e -> (e instanceof OpenOffer) && ((OpenOffer) e).getState().equals(CANCELED))
.map(e -> (OpenOffer) e)
.collect(Collectors.toList()));
}
}
public Optional<Tradable> getTradableById(String id) {
return closedTradables.stream().filter(e -> e.getId().equals(id)).findFirst();
synchronized (closedTradables) {
return closedTradables.stream().filter(e -> e.getId().equals(id)).findFirst();
}
}
public Optional<Tradable> getTradeById(String id) {
return closedTradables.stream().filter(e -> e instanceof Trade && e.getId().equals(id)).findFirst();
synchronized (closedTradables) {
return closedTradables.stream().filter(e -> e instanceof Trade && e.getId().equals(id)).findFirst();
}
}
public void maybeClearSensitiveData() {
log.info("checking closed trades eligibility for having sensitive data cleared");
closedTradables.stream()
synchronized (closedTradables) {
log.info("checking closed trades eligibility for having sensitive data cleared");
closedTradables.stream()
.filter(e -> e instanceof Trade)
.map(e -> (Trade) e)
.filter(e -> canTradeHaveSensitiveDataCleared(e.getId()))
.forEach(Trade::maybeClearSensitiveData);
requestPersistence();
requestPersistence();
}
}
public boolean canTradeHaveSensitiveDataCleared(String tradeId) {
Instant safeDate = getSafeDateForSensitiveDataClearing();
return closedTradables.stream()
.filter(e -> e.getId().equals(tradeId))
.filter(e -> e.getDate().toInstant().isBefore(safeDate))
.count() > 0;
synchronized (closedTradables) {
return closedTradables.stream()
.filter(e -> e.getId().equals(tradeId))
.filter(e -> e.getDate().toInstant().isBefore(safeDate))
.count() > 0;
}
}
public Instant getSafeDateForSensitiveDataClearing() {

View file

@ -88,14 +88,18 @@ public class FailedTradesManager implements PersistedDataHost {
}
public void add(Trade trade) {
if (failedTrades.add(trade)) {
requestPersistence();
synchronized (failedTrades) {
if (failedTrades.add(trade)) {
requestPersistence();
}
}
}
public void removeTrade(Trade trade) {
if (failedTrades.remove(trade)) {
requestPersistence();
synchronized (failedTrades) {
if (failedTrades.remove(trade)) {
requestPersistence();
}
}
}
@ -104,26 +108,34 @@ public class FailedTradesManager implements PersistedDataHost {
}
public ObservableList<Trade> getObservableList() {
return failedTrades.getObservableList();
synchronized (failedTrades) {
return failedTrades.getObservableList();
}
}
public Optional<Trade> getTradeById(String id) {
return failedTrades.stream().filter(e -> e.getId().equals(id)).findFirst();
synchronized (failedTrades) {
return failedTrades.stream().filter(e -> e.getId().equals(id)).findFirst();
}
}
public Stream<Trade> getTradesStreamWithFundsLockedIn() {
return failedTrades.stream()
.filter(Trade::isFundsLockedIn);
synchronized (failedTrades) {
return failedTrades.stream()
.filter(Trade::isFundsLockedIn);
}
}
public void unFailTrade(Trade trade) {
if (unFailTradeCallback == null)
return;
synchronized (failedTrades) {
if (unFailTradeCallback == null)
return;
if (unFailTradeCallback.apply(trade)) {
log.info("Unfailing trade {}", trade.getId());
if (failedTrades.remove(trade)) {
requestPersistence();
if (unFailTradeCallback.apply(trade)) {
log.info("Unfailing trade {}", trade.getId());
if (failedTrades.remove(trade)) {
requestPersistence();
}
}
}
}