package org.knowm.xchange.huobi;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.exceptions.ExchangeException;
import org.knowm.xchange.huobi.dto.marketdata.HuobiAsset;
import org.knowm.xchange.huobi.dto.marketdata.HuobiAssetPair;
import si.mazi.rescu.SynchronizedValueFactory;

public class HuobiUtils {

  private static Map<String, CurrencyPair> assetPairMap = new HashMap<String, CurrencyPair>();
  private static Map<CurrencyPair, String> assetPairMapReverse =
      new HashMap<CurrencyPair, String>();
  private static Map<String, Currency> assetMap = new HashMap<String, Currency>();
  private static Map<Currency, String> assetMapReverse = new HashMap<Currency, String>();

  private HuobiUtils() {}

  public static String createHuobiCurrencyPair(CurrencyPair currencyPair) {
    String pair = assetPairMapReverse.get(currencyPair);
    if ((pair == null) || (pair.length() == 0)) {
      throw new ExchangeException(
          String.format("Huobi doesn't support currency pair %s", currencyPair.toString()));
    }
    return pair;
  }

  public static String createUTCDate(SynchronizedValueFactory<Long> nonce) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    return dateFormat.format(new Date(nonce.createValue()));
  }

  public static void setHuobiAssets(HuobiAsset[] huobiAssets) {
    for (HuobiAsset entry : huobiAssets) {
      assetMap.put(entry.getAsset(), Currency.getInstance(entry.getAsset()));
      assetMapReverse.put(Currency.getInstance(entry.getAsset()), entry.getAsset());
    }
  }

  public static void setHuobiAssetPairs(HuobiAssetPair[] huobiAssetPairs) {
    for (HuobiAssetPair entry : huobiAssetPairs) {
      CurrencyPair pair =
          new CurrencyPair(
              translateHuobiCurrencyCode(entry.getBaseCurrency()),
              translateHuobiCurrencyCode(entry.getQuoteCurrency()));
      assetPairMap.put(entry.getKey(), pair);
      assetPairMapReverse.put(pair, entry.getKey());
    }
  }

  public static Currency translateHuobiCurrencyCode(String currencyIn) {
    Currency currencyOut = assetMap.get(currencyIn);
    if (currencyOut == null) {
      throw new ExchangeException("Huobi does not support the currency code " + currencyIn);
    }
    return currencyOut.getCommonlyUsedCurrency();
  }

  public static CurrencyPair translateHuobiCurrencyPair(String currencyPairIn) {
    CurrencyPair pair = assetPairMap.get(currencyPairIn);
    if (pair == null) {
      if (currencyPairIn.length() == 6) {
        Currency base = Currency.getInstance(currencyPairIn.substring(0, 3));
        if (base.getCommonlyUsedCurrency() != null) {
          base = base.getCommonlyUsedCurrency();
        }
        Currency counter = Currency.getInstance(currencyPairIn.substring(3, 6));
        if (counter.getCommonlyUsedCurrency() != null) {
          counter = counter.getCommonlyUsedCurrency();
        }
        pair = new CurrencyPair(base, counter);
      } else if (currencyPairIn.length() == 7) {
        Currency base = Currency.getInstance(currencyPairIn.substring(0, 4));
        if (base.getCommonlyUsedCurrency() != null) {
          base = base.getCommonlyUsedCurrency();
        }
        Currency counter = Currency.getInstance(currencyPairIn.substring(4, 7));
        if (counter.getCommonlyUsedCurrency() != null) {
          counter = counter.getCommonlyUsedCurrency();
        }
        pair = new CurrencyPair(base, counter);
      }
    }
    return pair;
  }
}

@@ -507,34 +507,167 @@
 ry;%0A
-%0Apublic class HuobiUtils %7B
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Apublic class HuobiUtils %7B%0A  private static Logger logger = LoggerFactory.getLogger(HuobiUtils.class);
 %0A%0A  
@@ -2557,35 +2557,20 @@
     
-throw new ExchangeException
+logger.error
 (%22Hu
@@ -2621,24 +2621,43 @@
 urrencyIn);%0A
+      return null;%0A
     %7D%0A    re

Return null when adapting unknown currencies