@Test public void testParse_SYMBOL_EUR() { CurrencyToken token = new CurrencyToken(SYMBOL, AmountFormatContextBuilder.of(FRANCE).build()); ParseContext context = new ParseContext("€"); token.parse(context); assertEquals(context.getIndex(), 1); }
@Override public void parse(ParseContext context) throws MonetaryParseException { String token = context.lookupNextToken(); while (Objects.nonNull(token)) { if (token.trim().isEmpty()) { context.consume(token); token = context.lookupNextToken(); continue; } break; } if (token == null){ throw new MonetaryParseException("Error parsing CurrencyUnit: no input.", "", -1); } try { CurrencyUnit cur; String[] providers = this.context.get("currencyProviderName", String.class)!=null? new String[]{this.context.get("currencyProviderName", String.class)}:new String[0]; switch (style) { case CODE: if (!Monetary.isCurrencyAvailable(token, providers)) { String subCurrency = parseCurrencyCode(token); cur = Monetary.getCurrency(subCurrency, providers); context.consume(subCurrency); } else { cur = Monetary.getCurrency(token, providers); context.consume(token); } break; case SYMBOL: if (token.startsWith("$")) { throw new MonetaryParseException("$ is not a unique currency symbol.", token, context.getErrorIndex()); } else if (token.startsWith("€")) { cur = Monetary.getCurrency("EUR", providers); context.consume('€'); } else if (token.startsWith("£")) { cur = Monetary.getCurrency("GBP", providers); context.consume('£'); } else { String code = token; for (Currency juc : Currency.getAvailableCurrencies()) { if (token.equals(juc.getSymbol())) { code = juc.getCurrencyCode(); break; } } cur = Monetary.getCurrency(code, providers); context.consume(token); } context.setParsedCurrency(cur); break; case NAME: case NUMERIC_CODE: default: throw new UnsupportedOperationException("Not yet implemented"); } if (Objects.nonNull(cur)) { context.setParsedCurrency(cur); }else{ CurrencyUnit fixed = this.context.get(CurrencyUnit.class); if(fixed!=null){ context.setParsedCurrency(fixed); } } } catch (MonetaryParseException e) { context.setError(); context.setErrorMessage(e.getMessage()); throw e; } catch (Exception e) { context.setError(); context.setErrorMessage(e.getMessage()); Logger.getLogger(getClass().getName()).log(FINEST, "Could not parse CurrencyUnit from \"" + token + "\"", e); throw new MonetaryParseException("Could not parse CurrencyUnit. " + e.getMessage(), token, -1); } }
CurrencyToken implements FormatToken { @Override public void parse(ParseContext context) throws MonetaryParseException { String token = context.lookupNextToken(); while (Objects.nonNull(token)) { if (token.trim().isEmpty()) { context.consume(token); token = context.lookupNextToken(); continue; } break; } if (token == null){ throw new MonetaryParseException("Error parsing CurrencyUnit: no input.", "", -1); } try { CurrencyUnit cur; String[] providers = this.context.get("currencyProviderName", String.class)!=null? new String[]{this.context.get("currencyProviderName", String.class)}:new String[0]; switch (style) { case CODE: if (!Monetary.isCurrencyAvailable(token, providers)) { String subCurrency = parseCurrencyCode(token); cur = Monetary.getCurrency(subCurrency, providers); context.consume(subCurrency); } else { cur = Monetary.getCurrency(token, providers); context.consume(token); } break; case SYMBOL: if (token.startsWith("$")) { throw new MonetaryParseException("$ is not a unique currency symbol.", token, context.getErrorIndex()); } else if (token.startsWith("€")) { cur = Monetary.getCurrency("EUR", providers); context.consume('€'); } else if (token.startsWith("£")) { cur = Monetary.getCurrency("GBP", providers); context.consume('£'); } else { String code = token; for (Currency juc : Currency.getAvailableCurrencies()) { if (token.equals(juc.getSymbol())) { code = juc.getCurrencyCode(); break; } } cur = Monetary.getCurrency(code, providers); context.consume(token); } context.setParsedCurrency(cur); break; case NAME: case NUMERIC_CODE: default: throw new UnsupportedOperationException("Not yet implemented"); } if (Objects.nonNull(cur)) { context.setParsedCurrency(cur); }else{ CurrencyUnit fixed = this.context.get(CurrencyUnit.class); if(fixed!=null){ context.setParsedCurrency(fixed); } } } catch (MonetaryParseException e) { context.setError(); context.setErrorMessage(e.getMessage()); throw e; } catch (Exception e) { context.setError(); context.setErrorMessage(e.getMessage()); Logger.getLogger(getClass().getName()).log(FINEST, "Could not parse CurrencyUnit from \"" + token + "\"", e); throw new MonetaryParseException("Could not parse CurrencyUnit. " + e.getMessage(), token, -1); } } }
CurrencyToken implements FormatToken { @Override public void parse(ParseContext context) throws MonetaryParseException { String token = context.lookupNextToken(); while (Objects.nonNull(token)) { if (token.trim().isEmpty()) { context.consume(token); token = context.lookupNextToken(); continue; } break; } if (token == null){ throw new MonetaryParseException("Error parsing CurrencyUnit: no input.", "", -1); } try { CurrencyUnit cur; String[] providers = this.context.get("currencyProviderName", String.class)!=null? new String[]{this.context.get("currencyProviderName", String.class)}:new String[0]; switch (style) { case CODE: if (!Monetary.isCurrencyAvailable(token, providers)) { String subCurrency = parseCurrencyCode(token); cur = Monetary.getCurrency(subCurrency, providers); context.consume(subCurrency); } else { cur = Monetary.getCurrency(token, providers); context.consume(token); } break; case SYMBOL: if (token.startsWith("$")) { throw new MonetaryParseException("$ is not a unique currency symbol.", token, context.getErrorIndex()); } else if (token.startsWith("€")) { cur = Monetary.getCurrency("EUR", providers); context.consume('€'); } else if (token.startsWith("£")) { cur = Monetary.getCurrency("GBP", providers); context.consume('£'); } else { String code = token; for (Currency juc : Currency.getAvailableCurrencies()) { if (token.equals(juc.getSymbol())) { code = juc.getCurrencyCode(); break; } } cur = Monetary.getCurrency(code, providers); context.consume(token); } context.setParsedCurrency(cur); break; case NAME: case NUMERIC_CODE: default: throw new UnsupportedOperationException("Not yet implemented"); } if (Objects.nonNull(cur)) { context.setParsedCurrency(cur); }else{ CurrencyUnit fixed = this.context.get(CurrencyUnit.class); if(fixed!=null){ context.setParsedCurrency(fixed); } } } catch (MonetaryParseException e) { context.setError(); context.setErrorMessage(e.getMessage()); throw e; } catch (Exception e) { context.setError(); context.setErrorMessage(e.getMessage()); Logger.getLogger(getClass().getName()).log(FINEST, "Could not parse CurrencyUnit from \"" + token + "\"", e); throw new MonetaryParseException("Could not parse CurrencyUnit. " + e.getMessage(), token, -1); } } CurrencyToken(CurrencyStyle style, AmountFormatContext context); }
CurrencyToken implements FormatToken { @Override public void parse(ParseContext context) throws MonetaryParseException { String token = context.lookupNextToken(); while (Objects.nonNull(token)) { if (token.trim().isEmpty()) { context.consume(token); token = context.lookupNextToken(); continue; } break; } if (token == null){ throw new MonetaryParseException("Error parsing CurrencyUnit: no input.", "", -1); } try { CurrencyUnit cur; String[] providers = this.context.get("currencyProviderName", String.class)!=null? new String[]{this.context.get("currencyProviderName", String.class)}:new String[0]; switch (style) { case CODE: if (!Monetary.isCurrencyAvailable(token, providers)) { String subCurrency = parseCurrencyCode(token); cur = Monetary.getCurrency(subCurrency, providers); context.consume(subCurrency); } else { cur = Monetary.getCurrency(token, providers); context.consume(token); } break; case SYMBOL: if (token.startsWith("$")) { throw new MonetaryParseException("$ is not a unique currency symbol.", token, context.getErrorIndex()); } else if (token.startsWith("€")) { cur = Monetary.getCurrency("EUR", providers); context.consume('€'); } else if (token.startsWith("£")) { cur = Monetary.getCurrency("GBP", providers); context.consume('£'); } else { String code = token; for (Currency juc : Currency.getAvailableCurrencies()) { if (token.equals(juc.getSymbol())) { code = juc.getCurrencyCode(); break; } } cur = Monetary.getCurrency(code, providers); context.consume(token); } context.setParsedCurrency(cur); break; case NAME: case NUMERIC_CODE: default: throw new UnsupportedOperationException("Not yet implemented"); } if (Objects.nonNull(cur)) { context.setParsedCurrency(cur); }else{ CurrencyUnit fixed = this.context.get(CurrencyUnit.class); if(fixed!=null){ context.setParsedCurrency(fixed); } } } catch (MonetaryParseException e) { context.setError(); context.setErrorMessage(e.getMessage()); throw e; } catch (Exception e) { context.setError(); context.setErrorMessage(e.getMessage()); Logger.getLogger(getClass().getName()).log(FINEST, "Could not parse CurrencyUnit from \"" + token + "\"", e); throw new MonetaryParseException("Could not parse CurrencyUnit. " + e.getMessage(), token, -1); } } CurrencyToken(CurrencyStyle style, AmountFormatContext context); CurrencyToken setCurrencyStyle(CurrencyStyle style); CurrencyStyle getCurrencyStyle(); @Override void parse(ParseContext context); @Override void print(Appendable appendable, MonetaryAmount amount); @Override String toString(); }
CurrencyToken implements FormatToken { @Override public void parse(ParseContext context) throws MonetaryParseException { String token = context.lookupNextToken(); while (Objects.nonNull(token)) { if (token.trim().isEmpty()) { context.consume(token); token = context.lookupNextToken(); continue; } break; } if (token == null){ throw new MonetaryParseException("Error parsing CurrencyUnit: no input.", "", -1); } try { CurrencyUnit cur; String[] providers = this.context.get("currencyProviderName", String.class)!=null? new String[]{this.context.get("currencyProviderName", String.class)}:new String[0]; switch (style) { case CODE: if (!Monetary.isCurrencyAvailable(token, providers)) { String subCurrency = parseCurrencyCode(token); cur = Monetary.getCurrency(subCurrency, providers); context.consume(subCurrency); } else { cur = Monetary.getCurrency(token, providers); context.consume(token); } break; case SYMBOL: if (token.startsWith("$")) { throw new MonetaryParseException("$ is not a unique currency symbol.", token, context.getErrorIndex()); } else if (token.startsWith("€")) { cur = Monetary.getCurrency("EUR", providers); context.consume('€'); } else if (token.startsWith("£")) { cur = Monetary.getCurrency("GBP", providers); context.consume('£'); } else { String code = token; for (Currency juc : Currency.getAvailableCurrencies()) { if (token.equals(juc.getSymbol())) { code = juc.getCurrencyCode(); break; } } cur = Monetary.getCurrency(code, providers); context.consume(token); } context.setParsedCurrency(cur); break; case NAME: case NUMERIC_CODE: default: throw new UnsupportedOperationException("Not yet implemented"); } if (Objects.nonNull(cur)) { context.setParsedCurrency(cur); }else{ CurrencyUnit fixed = this.context.get(CurrencyUnit.class); if(fixed!=null){ context.setParsedCurrency(fixed); } } } catch (MonetaryParseException e) { context.setError(); context.setErrorMessage(e.getMessage()); throw e; } catch (Exception e) { context.setError(); context.setErrorMessage(e.getMessage()); Logger.getLogger(getClass().getName()).log(FINEST, "Could not parse CurrencyUnit from \"" + token + "\"", e); throw new MonetaryParseException("Could not parse CurrencyUnit. " + e.getMessage(), token, -1); } } CurrencyToken(CurrencyStyle style, AmountFormatContext context); CurrencyToken setCurrencyStyle(CurrencyStyle style); CurrencyStyle getCurrencyStyle(); @Override void parse(ParseContext context); @Override void print(Appendable appendable, MonetaryAmount amount); @Override String toString();  }