@Test public void parseTest() throws java.text.ParseException { NbtFormat us = NbtFormat.getSymbolInstance(Locale.US); NbtFormat usCoded = NbtFormat.getCodeInstance(Locale.US); assertEquals(valueOf(20000), us.parseObject("NBT2")); assertEquals(valueOf(20000), us.parseObject("NBT2")); assertEquals(valueOf(20000), us.parseObject("𝑵2")); assertEquals(valueOf(20000), us.parseObject("𝑵2")); assertEquals(valueOf(20000), us.parseObject("2")); assertEquals(valueOf(20000), usCoded.parseObject("NBT 2")); assertEquals(valueOf(20000), usCoded.parseObject("NBT 2")); assertEquals(valueOf(20000), us.parseObject("𝑵2.0")); assertEquals(valueOf(20000), us.parseObject("𝑵2.0")); assertEquals(valueOf(20000), us.parseObject("2.0")); assertEquals(valueOf(20000), us.parseObject("NBT2.0")); assertEquals(valueOf(20000), us.parseObject("NBT2.0")); assertEquals(valueOf(20000), usCoded.parseObject("𝑵 2")); assertEquals(valueOf(20000), usCoded.parseObject("𝑵 2")); assertEquals(valueOf(20000), usCoded.parseObject(" 2")); assertEquals(valueOf(20000), usCoded.parseObject("NBT 2")); assertEquals(valueOf(20000), usCoded.parseObject("NBT 2")); assertEquals(valueOf(20222242000L), us.parseObject("2,022,224.20")); assertEquals(valueOf(20222242000L), us.parseObject("𝑵2,022,224.20")); assertEquals(valueOf(20222242000L), us.parseObject("𝑵2,022,224.20")); assertEquals(valueOf(20222242000L), us.parseObject("NBT2,022,224.20")); assertEquals(valueOf(20222242000L), us.parseObject("NBT2,022,224.20")); assertEquals(valueOf(22020000L), us.parseObject("2,202.0")); assertEquals(valueOf(210000000000L), us.parseObject("21000000.0000")); us = NbtFormat.builder().locale(US).style(SYMBOL).symbol("£").code("XYZ").build(); usCoded = NbtFormat.builder().locale(US).scale(0).symbol("£").code("XYZ"). pattern("¤ #,##0.00").build(); assertEquals(valueOf(20000), us.parseObject("XYZ2")); assertEquals(valueOf(20000), us.parseObject("NBT2")); assertEquals(valueOf(20000), us.parseObject("NBT2")); assertEquals(valueOf(20000), us.parseObject("£2")); assertEquals(valueOf(20000), us.parseObject("𝑵2")); assertEquals(valueOf(20000), us.parseObject("𝑵2")); assertEquals(valueOf(20000), us.parseObject("2")); assertEquals(valueOf(20000), usCoded.parseObject("XYZ 2")); assertEquals(valueOf(20000), usCoded.parseObject("NBT 2")); assertEquals(valueOf(20000), usCoded.parseObject("NBT 2")); assertEquals(valueOf(20000), us.parseObject("£2.0")); assertEquals(valueOf(20000), us.parseObject("𝑵2.0")); assertEquals(valueOf(20000), us.parseObject("𝑵2.0")); assertEquals(valueOf(20000), us.parseObject("2.0")); assertEquals(valueOf(20000), us.parseObject("XYZ2.0")); assertEquals(valueOf(20000), us.parseObject("NBT2.0")); assertEquals(valueOf(20000), us.parseObject("NBT2.0")); assertEquals(valueOf(20000), usCoded.parseObject("£ 2")); assertEquals(valueOf(20000), usCoded.parseObject("𝑵 2")); assertEquals(valueOf(20000), usCoded.parseObject("𝑵 2")); assertEquals(valueOf(20000), usCoded.parseObject(" 2")); assertEquals(valueOf(20000), usCoded.parseObject("XYZ 2")); assertEquals(valueOf(20000), usCoded.parseObject("NBT 2")); assertEquals(valueOf(20000), usCoded.parseObject("NBT 2")); assertEquals(valueOf(20222242000L), us.parseObject("2,022,224.20")); assertEquals(valueOf(20222242000L), us.parseObject("£2,022,224.20")); assertEquals(valueOf(20222242000L), us.parseObject("𝑵2,022,224.20")); assertEquals(valueOf(20222242000L), us.parseObject("𝑵2,022,224.20")); assertEquals(valueOf(20222242000L), us.parseObject("XYZ2,022,224.20")); assertEquals(valueOf(20222242000L), us.parseObject("NBT2,022,224.20")); assertEquals(valueOf(20222242000L), us.parseObject("NBT2,022,224.20")); assertEquals(valueOf(22020000L), us.parseObject("2,202.0")); assertEquals(valueOf(210000000000L), us.parseObject("21000000.000000")); try { NbtFormat.getInstance().parse("abc"); fail("bad parse must raise exception"); } catch (ParseException e) {} }
public Coin parse(String source, ParsePosition pos) { DecimalFormatSymbols anteSigns = null; int parseScale = COIN_SCALE; Coin coin = null; synchronized (numberFormat) { if (numberFormat.toPattern().contains("¤")) { for(ScaleMatcher d : denomMatchers()) { Matcher matcher = d.pattern.matcher(source); if (matcher.find()) { anteSigns = setSymbolAndCode(numberFormat, matcher.group()); parseScale = d.scale; break; } } if (parseScale == COIN_SCALE) { Matcher matcher = coinPattern.matcher(source); matcher.find(); anteSigns = setSymbolAndCode(numberFormat, matcher.group()); } } else parseScale = scale(); Number number = numberFormat.parse(source, pos); if (number != null) try { coin = Coin.valueOf( ((BigDecimal)number).movePointRight(offSatoshis(parseScale)).setScale(0, HALF_UP).longValue() ); } catch (IllegalArgumentException e) { pos.setIndex(0); } if (anteSigns != null) numberFormat.setDecimalFormatSymbols(anteSigns); } return coin; }
NbtFormat extends Format { public Coin parse(String source, ParsePosition pos) { DecimalFormatSymbols anteSigns = null; int parseScale = COIN_SCALE; Coin coin = null; synchronized (numberFormat) { if (numberFormat.toPattern().contains("¤")) { for(ScaleMatcher d : denomMatchers()) { Matcher matcher = d.pattern.matcher(source); if (matcher.find()) { anteSigns = setSymbolAndCode(numberFormat, matcher.group()); parseScale = d.scale; break; } } if (parseScale == COIN_SCALE) { Matcher matcher = coinPattern.matcher(source); matcher.find(); anteSigns = setSymbolAndCode(numberFormat, matcher.group()); } } else parseScale = scale(); Number number = numberFormat.parse(source, pos); if (number != null) try { coin = Coin.valueOf( ((BigDecimal)number).movePointRight(offSatoshis(parseScale)).setScale(0, HALF_UP).longValue() ); } catch (IllegalArgumentException e) { pos.setIndex(0); } if (anteSigns != null) numberFormat.setDecimalFormatSymbols(anteSigns); } return coin; } }
NbtFormat extends Format { public Coin parse(String source, ParsePosition pos) { DecimalFormatSymbols anteSigns = null; int parseScale = COIN_SCALE; Coin coin = null; synchronized (numberFormat) { if (numberFormat.toPattern().contains("¤")) { for(ScaleMatcher d : denomMatchers()) { Matcher matcher = d.pattern.matcher(source); if (matcher.find()) { anteSigns = setSymbolAndCode(numberFormat, matcher.group()); parseScale = d.scale; break; } } if (parseScale == COIN_SCALE) { Matcher matcher = coinPattern.matcher(source); matcher.find(); anteSigns = setSymbolAndCode(numberFormat, matcher.group()); } } else parseScale = scale(); Number number = numberFormat.parse(source, pos); if (number != null) try { coin = Coin.valueOf( ((BigDecimal)number).movePointRight(offSatoshis(parseScale)).setScale(0, HALF_UP).longValue() ); } catch (IllegalArgumentException e) { pos.setIndex(0); } if (anteSigns != null) numberFormat.setDecimalFormatSymbols(anteSigns); } return coin; } protected  NbtFormat(DecimalFormat numberFormat, int minDecimals, List<Integer> groups); }
NbtFormat extends Format { public Coin parse(String source, ParsePosition pos) { DecimalFormatSymbols anteSigns = null; int parseScale = COIN_SCALE; Coin coin = null; synchronized (numberFormat) { if (numberFormat.toPattern().contains("¤")) { for(ScaleMatcher d : denomMatchers()) { Matcher matcher = d.pattern.matcher(source); if (matcher.find()) { anteSigns = setSymbolAndCode(numberFormat, matcher.group()); parseScale = d.scale; break; } } if (parseScale == COIN_SCALE) { Matcher matcher = coinPattern.matcher(source); matcher.find(); anteSigns = setSymbolAndCode(numberFormat, matcher.group()); } } else parseScale = scale(); Number number = numberFormat.parse(source, pos); if (number != null) try { coin = Coin.valueOf( ((BigDecimal)number).movePointRight(offSatoshis(parseScale)).setScale(0, HALF_UP).longValue() ); } catch (IllegalArgumentException e) { pos.setIndex(0); } if (anteSigns != null) numberFormat.setDecimalFormatSymbols(anteSigns); } return coin; } protected  NbtFormat(DecimalFormat numberFormat, int minDecimals, List<Integer> groups); static Builder builder(); static NbtFormat getInstance(); static NbtFormat getSymbolInstance(); static NbtFormat getCodeInstance(); static NbtFormat getSymbolInstance(int fractionPlaces); static NbtFormat getCodeInstance(int minDecimals); static NbtFormat getInstance(Locale locale); static NbtFormat getCodeInstance(Locale locale); static NbtFormat getInstance(Locale locale, int minDecimals); static NbtFormat getCodeInstance(Locale locale, int minDecimals); static NbtFormat getSymbolInstance(Locale locale); static NbtFormat getSymbolInstance(Locale locale, int fractionPlaces); static NbtFormat getInstance(Style style); static NbtFormat getInstance(Style style, int fractionPlaces); static NbtFormat getInstance(Style style, Locale locale); static NbtFormat getInstance(Style style, Locale locale, int fractionPlaces); static NbtFormat getCoinInstance(); static NbtFormat getCoinInstance(int minFractionPlaces, int... groups); static NbtFormat getCoinInstance(Locale locale); static NbtFormat getCoinInstance(Locale locale, int scale, int... groups); static NbtFormat getInstance(int scale, int minDecimals, int... groups); static NbtFormat getInstance(int scale); static NbtFormat getInstance(int scale, Locale locale); static NbtFormat getInstance(int scale, Locale locale, int minDecimals, int... groups); static NbtFormat getInstance(int scale, Locale locale, int minDecimals, List<Integer> groups); @Override AttributedCharacterIterator formatToCharacterIterator(Object obj); @Override StringBuffer format(Object qty, StringBuffer toAppendTo, FieldPosition pos); String format(Object qty, int minDecimals, int... fractionGroups); StringBuffer format(Object qty, StringBuffer toAppendTo, FieldPosition pos,
                                            int minDecimals, int... fractionGroups); @Override final Object parseObject(String source, ParsePosition pos); Coin parse(String source, ParsePosition pos); Coin parse(String source); static Locale[] getAvailableLocales(); String coinSymbol(); String coinCode(); String pattern(); DecimalFormatSymbols symbols(); @Override boolean equals(Object o); @Override int hashCode(); }
NbtFormat extends Format { public Coin parse(String source, ParsePosition pos) { DecimalFormatSymbols anteSigns = null; int parseScale = COIN_SCALE; Coin coin = null; synchronized (numberFormat) { if (numberFormat.toPattern().contains("¤")) { for(ScaleMatcher d : denomMatchers()) { Matcher matcher = d.pattern.matcher(source); if (matcher.find()) { anteSigns = setSymbolAndCode(numberFormat, matcher.group()); parseScale = d.scale; break; } } if (parseScale == COIN_SCALE) { Matcher matcher = coinPattern.matcher(source); matcher.find(); anteSigns = setSymbolAndCode(numberFormat, matcher.group()); } } else parseScale = scale(); Number number = numberFormat.parse(source, pos); if (number != null) try { coin = Coin.valueOf( ((BigDecimal)number).movePointRight(offSatoshis(parseScale)).setScale(0, HALF_UP).longValue() ); } catch (IllegalArgumentException e) { pos.setIndex(0); } if (anteSigns != null) numberFormat.setDecimalFormatSymbols(anteSigns); } return coin; } protected  NbtFormat(DecimalFormat numberFormat, int minDecimals, List<Integer> groups); static Builder builder(); static NbtFormat getInstance(); static NbtFormat getSymbolInstance(); static NbtFormat getCodeInstance(); static NbtFormat getSymbolInstance(int fractionPlaces); static NbtFormat getCodeInstance(int minDecimals); static NbtFormat getInstance(Locale locale); static NbtFormat getCodeInstance(Locale locale); static NbtFormat getInstance(Locale locale, int minDecimals); static NbtFormat getCodeInstance(Locale locale, int minDecimals); static NbtFormat getSymbolInstance(Locale locale); static NbtFormat getSymbolInstance(Locale locale, int fractionPlaces); static NbtFormat getInstance(Style style); static NbtFormat getInstance(Style style, int fractionPlaces); static NbtFormat getInstance(Style style, Locale locale); static NbtFormat getInstance(Style style, Locale locale, int fractionPlaces); static NbtFormat getCoinInstance(); static NbtFormat getCoinInstance(int minFractionPlaces, int... groups); static NbtFormat getCoinInstance(Locale locale); static NbtFormat getCoinInstance(Locale locale, int scale, int... groups); static NbtFormat getInstance(int scale, int minDecimals, int... groups); static NbtFormat getInstance(int scale); static NbtFormat getInstance(int scale, Locale locale); static NbtFormat getInstance(int scale, Locale locale, int minDecimals, int... groups); static NbtFormat getInstance(int scale, Locale locale, int minDecimals, List<Integer> groups); @Override AttributedCharacterIterator formatToCharacterIterator(Object obj); @Override StringBuffer format(Object qty, StringBuffer toAppendTo, FieldPosition pos); String format(Object qty, int minDecimals, int... fractionGroups); StringBuffer format(Object qty, StringBuffer toAppendTo, FieldPosition pos,
                                            int minDecimals, int... fractionGroups); @Override final Object parseObject(String source, ParsePosition pos); Coin parse(String source, ParsePosition pos); Coin parse(String source); static Locale[] getAvailableLocales(); String coinSymbol(); String coinCode(); String pattern(); DecimalFormatSymbols symbols(); @Override boolean equals(Object o); @Override int hashCode(); static final int COIN_SCALE; static final int MILLICOIN_SCALE; static final int MICROCOIN_SCALE; }