Example usage for java.util EnumMap EnumMap

List of usage examples for java.util EnumMap EnumMap

Introduction

In this page you can find the example usage for java.util EnumMap EnumMap.

Prototype

public EnumMap(Map<K, ? extends V> m) 

Source Link

Document

Creates an enum map initialized from the specified map.

Usage

From source file:org.yccheok.jstock.gui.JStock.java

private void initStockInfoDatabaseMeta() {

    Runnable runnable = new Runnable() {
        @Override// ww w  . j  a v a2  s . c  om
        public void run() {
            // Read existing stock-info-database-meta.json
            final Map<Country, Long> localStockInfoDatabaseMeta = Utils
                    .loadStockInfoDatabaseMeta(Utils.getStockInfoDatabaseMetaFile());

            final String location = org.yccheok.jstock.network.Utils
                    .getURL(org.yccheok.jstock.network.Utils.Type.STOCK_INFO_DATABASE_META);

            final String json = Utils.downloadAsString(location);

            final Map<Country, Long> latestStockInfoDatabaseMeta = Utils.loadStockInfoDatabaseMeta(json);

            final Map<Country, Long> successStockInfoDatabaseMeta = new EnumMap<Country, Long>(Country.class);

            boolean needToInitDatabase = false;

            // Build up list of stock-info-database.csv that needed to be
            // updated.
            for (Map.Entry<Country, Long> entry : latestStockInfoDatabaseMeta.entrySet()) {
                if (Thread.currentThread().isInterrupted() || stockInfoDatabaseMetaPool == null) {
                    break;
                }

                Country country = entry.getKey();
                Long latest = entry.getValue();
                Long local = localStockInfoDatabaseMeta.get(country);

                if (false == latest.equals(local)) {
                    final String stocksCSVZipFileLocation = org.yccheok.jstock.engine.Utils
                            .getStocksCSVZipFileLocation(country);

                    final File zipFile = Utils.downloadAsTempFile(stocksCSVZipFileLocation);

                    if (zipFile == null) {
                        continue;
                    }

                    File tempZipDirectory = null;

                    try {
                        tempZipDirectory = java.nio.file.Files.createTempDirectory(null).toFile();

                        if (false == Utils.extractZipFile(zipFile, tempZipDirectory.getAbsolutePath(), true)) {
                            continue;
                        }

                        File file = new File(tempZipDirectory, "stocks.csv");

                        final java.util.List<Stock> stocks = org.yccheok.jstock.engine.Utils
                                .getStocksFromCSVFile(file);

                        if (false == stocks.isEmpty()) {
                            final Pair<StockInfoDatabase, StockNameDatabase> stockDatabase = org.yccheok.jstock.engine.Utils
                                    .toStockDatabase(stocks, country);

                            final boolean success = JStock.saveStockInfoDatabaseAsCSV(country,
                                    stockDatabase.first);

                            if (stockDatabase.second != null) {
                                JStock.saveStockNameDatabaseAsCSV(country, stockDatabase.second);
                            }

                            if (success) {
                                successStockInfoDatabaseMeta.put(country, latest);
                                if (country == jStockOptions.getCountry()) {
                                    needToInitDatabase = true;
                                }
                            }
                        }
                    } catch (IOException ex) {
                        log.error(null, ex);
                    } finally {
                        if (tempZipDirectory != null) {
                            Utils.deleteDir(tempZipDirectory, true);
                        }
                    }
                }
            }

            if (successStockInfoDatabaseMeta.isEmpty()) {
                return;
            }

            // Retain old meta value.
            for (Map.Entry<Country, Long> entry : localStockInfoDatabaseMeta.entrySet()) {
                Country country = entry.getKey();
                Long old = entry.getValue();

                if (false == successStockInfoDatabaseMeta.containsKey(country)) {
                    successStockInfoDatabaseMeta.put(country, old);
                }
            }

            Utils.saveStockInfoDatabaseMeta(Utils.getStockInfoDatabaseMetaFile(), successStockInfoDatabaseMeta);

            if (needToInitDatabase) {
                initDatabase(true);
            }
        }
    };

    stockInfoDatabaseMetaPool.execute(runnable);
}