Beispiel #1
0
 /**
  * Parses a currency from the given {@link ParseContext}. Depending on the current {@link
  * CurrencyStyle} it interprets the next non empty token, either as
  *
  * <ul>
  *   <li>currency code
  *   <li>currency symbol
  * </ul>
  *
  * Parsing of localized currency names or numeric code is not supported.
  *
  * @throws UnsupportedOperationException if the {@link CurrencyStyle} is configured to us currency
  *     names, or numeric codes for formatting.
  */
 @Override
 public void parse(ParseContext context) throws MonetaryParseException {
   String token = context.lookupNextToken();
   while (token != null) {
     if (token.trim().isEmpty()) {
       context.consume(token);
       token = context.lookupNextToken();
       continue;
     }
     break;
   }
   try {
     CurrencyUnit cur = null;
     switch (style) {
       case CODE:
         cur = MonetaryCurrencies.getCurrency(token);
         context.consume(token);
         break;
       case SYMBOL:
         if (token.startsWith("$")) {
           cur = MonetaryCurrencies.getCurrency("USD");
           context.consume("$");
         } else if (token.startsWith("€")) {
           cur = MonetaryCurrencies.getCurrency("EUR");
           context.consume("€");
         } else if (token.startsWith("£")) {
           cur = MonetaryCurrencies.getCurrency("GBP");
           context.consume("£");
         }
         cur = MonetaryCurrencies.getCurrency(token);
         context.consume(token);
         break;
       case NAME:
       case NUMERIC_CODE:
       default:
         throw new UnsupportedOperationException("Not yet implemented");
     }
     if (cur != null) {
       context.setParsedCurrency(cur);
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
 @Override
 public CurrencyUnit parse(String text, Locale locale) {
   return MonetaryCurrencies.getCurrency(text);
 }
/** This enumerated class provides keys for the currency symbols and values. */
public enum CurrencySymbolKeys implements CurrencySupplier {
  // Declared in order they should appear in a chooser.
  /** No symbol. */
  NONE(I18NHelper.getSharedProperty("none"), "", MonetaryCurrencies.getCurrency("XXX")),
  /** The Euro. */
  EURO("\u20ac ", MonetaryCurrencies.getCurrency("EUR")),
  /** The US Dollar. */
  DOLLAR("$ ", MonetaryCurrencies.getCurrency("USD")),
  /** The English Pound. */
  POUND("\u00a3 ", MonetaryCurrencies.getCurrency("GBP")),
  /** The South African Rand. */
  RAND("R ", MonetaryCurrencies.getCurrency("ZAR")),
  /** The Brazilian Real. */
  REAL("R$ ", MonetaryCurrencies.getCurrency("BRL")),
  /** The Russian Ruble. */
  RUBLE(" \u0440.", MonetaryCurrencies.getCurrency("RUB"));

  //////////////////////////////////////////////////////////////////////////////
  // Start of public methods.
  //////////////////////////////////////////////////////////////////////////////

  /**
   * This method returns a string for the enum constant.
   *
   * @return A string.
   */
  public String getSymbol() {
    return itsSymbol;
  }

  /**
   * This method returns a currency unit for the enum constant.
   *
   * @return A currency.
   */
  public CurrencyUnit getCurrency() {
    return itsCurrency;
  }

  /**
   * This method returns the currency symbol.
   *
   * @return The currency symbol.
   */
  @Override
  public String toString() {
    return itsIdentifier;
  }

  //////////////////////////////////////////////////////////////////////////////
  // Start of private methods.
  //////////////////////////////////////////////////////////////////////////////

  private CurrencySymbolKeys(String symbol, CurrencyUnit currency) {
    this(symbol, symbol, currency);
  }

  private CurrencySymbolKeys(String identifier, String symbol, CurrencyUnit currency) {
    itsIdentifier = identifier;
    itsSymbol = symbol;
    itsCurrency = currency;
  }

  //////////////////////////////////////////////////////////////////////////////
  // Start of class members.
  //////////////////////////////////////////////////////////////////////////////

  private final String itsIdentifier;
  private final String itsSymbol;
  private final CurrencyUnit itsCurrency;
}