Search in sources :

Example 1 with HashMapChangedListener

use of io.bitsquare.p2p.storage.HashMapChangedListener in project bitsquare by bitsquare.

the class TradeStatisticsManager method init.

private void init(P2PService p2PService) {
    if (dumpStatistics) {
        this.statisticsJsonStorage.initWithFileName("trade_statistics.json");
        this.fiatCurrencyListJsonStorage.initWithFileName("fiat_currency_list.json");
        ArrayList<CurrencyTuple> fiatCurrencyList = new ArrayList<>(CurrencyUtil.getAllSortedFiatCurrencies().stream().map(e -> new CurrencyTuple(e.getCode(), e.getName(), 8)).collect(Collectors.toList()));
        fiatCurrencyListJsonStorage.queueUpForSave(new PlainTextWrapper(Utilities.objectToJson(fiatCurrencyList)), 2000);
        this.cryptoCurrencyListJsonStorage.initWithFileName("crypto_currency_list.json");
        ArrayList<CurrencyTuple> cryptoCurrencyList = new ArrayList<>(CurrencyUtil.getAllSortedCryptoCurrencies().stream().map(e -> new CurrencyTuple(e.getCode(), e.getName(), 8)).collect(Collectors.toList()));
        cryptoCurrencyList.add(0, new CurrencyTuple("BTC", "Bitcoin", 8));
        cryptoCurrencyListJsonStorage.queueUpForSave(new PlainTextWrapper(Utilities.objectToJson(cryptoCurrencyList)), 2000);
    }
    HashSet<TradeStatistics> persisted = statisticsStorage.initAndGetPersistedWithFileName("TradeStatistics");
    if (persisted != null)
        persisted.stream().forEach(e -> add(e, false));
    p2PService.addHashSetChangedListener(new HashMapChangedListener() {

        @Override
        public void onAdded(ProtectedStorageEntry data) {
            final StoragePayload storagePayload = data.getStoragePayload();
            if (storagePayload instanceof TradeStatistics)
                add((TradeStatistics) storagePayload, true);
        }

        @Override
        public void onRemoved(ProtectedStorageEntry data) {
        // We don't remove items
        }
    });
    // At startup the P2PDataStorage inits earlier, otherwise we ge the listener called.
    p2PService.getP2PDataStorage().getMap().values().forEach(e -> {
        final StoragePayload storagePayload = e.getStoragePayload();
        if (storagePayload instanceof TradeStatistics)
            add((TradeStatistics) storagePayload, false);
    });
}
Also used : CurrencyTuple(io.bitsquare.locale.CurrencyTuple) ObservableSet(javafx.collections.ObservableSet) Utilities(io.bitsquare.common.util.Utilities) Logger(org.slf4j.Logger) P2PService(io.bitsquare.p2p.P2PService) Inject(com.google.inject.Inject) ProtectedStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry) LoggerFactory(org.slf4j.LoggerFactory) FXCollections(javafx.collections.FXCollections) HashMapChangedListener(io.bitsquare.p2p.storage.HashMapChangedListener) StoragePayload(io.bitsquare.p2p.storage.payload.StoragePayload) Collectors(java.util.stream.Collectors) AppOptionKeys(io.bitsquare.app.AppOptionKeys) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) PlainTextWrapper(io.bitsquare.storage.PlainTextWrapper) Named(com.google.inject.name.Named) Storage(io.bitsquare.storage.Storage) CurrencyUtil(io.bitsquare.locale.CurrencyUtil) CurrencyTuple(io.bitsquare.locale.CurrencyTuple) HashMapChangedListener(io.bitsquare.p2p.storage.HashMapChangedListener) ArrayList(java.util.ArrayList) PlainTextWrapper(io.bitsquare.storage.PlainTextWrapper) StoragePayload(io.bitsquare.p2p.storage.payload.StoragePayload) ProtectedStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry)

Aggregations

Inject (com.google.inject.Inject)1 Named (com.google.inject.name.Named)1 AppOptionKeys (io.bitsquare.app.AppOptionKeys)1 Utilities (io.bitsquare.common.util.Utilities)1 CurrencyTuple (io.bitsquare.locale.CurrencyTuple)1 CurrencyUtil (io.bitsquare.locale.CurrencyUtil)1 P2PService (io.bitsquare.p2p.P2PService)1 HashMapChangedListener (io.bitsquare.p2p.storage.HashMapChangedListener)1 StoragePayload (io.bitsquare.p2p.storage.payload.StoragePayload)1 ProtectedStorageEntry (io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry)1 PlainTextWrapper (io.bitsquare.storage.PlainTextWrapper)1 Storage (io.bitsquare.storage.Storage)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 FXCollections (javafx.collections.FXCollections)1 ObservableSet (javafx.collections.ObservableSet)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1