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

View file

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