/** * Formats the model to markup instead of to plain text if the result markup will be more than just plain text * escaped, otherwise falls back to formatting to plain text. If the markup result would be just the result of * {@link #formatToPlainText(TemplateNumberModel)} escaped, it must return the {@link String} that * {@link #formatToPlainText(TemplateNumberModel)} does. * * <p> * The implementation in {@link TemplateNumberFormat} simply calls {@link #formatToPlainText(TemplateNumberModel)}. * * @return A {@link String} or a {@link TemplateMarkupOutputModel}; not {@code null}. */ public Object format(TemplateNumberModel numberModel) throws TemplateValueFormatException, TemplateModelException { return formatToPlainText(numberModel); }
public static TemplateException newCantFormatNumberException(TemplateNumberFormat format, Expression dataSrcExp, TemplateValueFormatException e, boolean useTempModelExc) { _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder( "Failed to format number with format ", new _DelayedJQuote(format.getDescription()), ": ", e.getMessage()) .blame(dataSrcExp); return useTempModelExc ? new _TemplateModelException(e, (Environment) null, desc) : new _MiscTemplateException(e, (Environment) null, desc); }
@Override public void setLocale(Locale locale) { Locale prevLocale = getLocale(); super.setLocale(locale); if (!locale.equals(prevLocale)) { cachedTemplateNumberFormats = null; if (cachedTemplateNumberFormat != null && cachedTemplateNumberFormat.isLocaleBound()) { cachedTemplateNumberFormat = null; } if (cachedTempDateFormatArray != null) { for (int i = 0; i < CACHED_TDFS_LENGTH; i++) { final TemplateDateFormat f = cachedTempDateFormatArray[i]; if (f != null && f.isLocaleBound()) { cachedTempDateFormatArray[i] = null; } } } cachedTempDateFormatsByFmtStrArray = null; cachedCollator = null; } }
@Test void formal() throws TemplateValueFormatException { final TemplateNumberFormat f = InterestNumberFormatFactory.INSTANCE.get("", Locale.ENGLISH, Environment.getCurrentEnvironment()); assertSoftly(softly -> { softly.assertThat(f.isLocaleBound()).isTrue(); softly.assertThat(f.getDescription()) .isNotNull() .isNotEmpty(); softly.assertThatThrownBy(() -> InterestNumberFormatFactory.INSTANCE.get("someparam", Locale.ENGLISH, Environment .getCurrentEnvironment())) .isInstanceOf(InvalidFormatParametersException.class); }); } }
TemplateNumberFormat format = env.getTemplateNumberFormat(exp, false); try { return assertFormatResultNotNull(format.format(tnm)); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, false);
TemplateNumberFormat format = env.getTemplateNumberFormat(exp, false); try { return ensureFormatResultString(format.format(tnm), exp, env); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, false);
/** * Format number with the number format specified as the parameter, with the current locale. * * @param exp * The blamed expression if an error occurs; it's only needed for better error messages */ String formatNumberToPlainText( TemplateNumberModel number, TemplateNumberFormat format, Expression exp, boolean useTempModelExc) throws TemplateException { try { return EvalUtil.assertFormatResultNotNull(format.formatToPlainText(number)); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, useTempModelExc); } }
TemplateNumberFormat format = env.getTemplateNumberFormat(exp, false); try { return ensureFormatResultString(format.format(tnm), exp, env); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, false);
public static TemplateException newCantFormatNumberException(TemplateNumberFormat format, Expression dataSrcExp, TemplateValueFormatException e, boolean useTempModelExc) { _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder( "Failed to format number with format ", new _DelayedJQuote(format.getDescription()), ": ", e.getMessage()) .blame(dataSrcExp); return useTempModelExc ? new _TemplateModelException(e, (Environment) null, desc) : new _MiscTemplateException(e, (Environment) null, desc); }
@Override public void setLocale(Locale locale) { Locale prevLocale = getLocale(); super.setLocale(locale); if (!locale.equals(prevLocale)) { cachedTemplateNumberFormats = null; if (cachedTemplateNumberFormat != null && cachedTemplateNumberFormat.isLocaleBound()) { cachedTemplateNumberFormat = null; } if (cachedTempDateFormatArray != null) { for (int i = 0; i < CACHED_TDFS_LENGTH; i++) { final TemplateDateFormat f = cachedTempDateFormatArray[i]; if (f != null && f.isLocaleBound()) { cachedTempDateFormatArray[i] = null; } } } cachedTempDateFormatsByFmtStrArray = null; cachedCollator = null; } }
/** * Formats the model to markup instead of to plain text if the result markup will be more than just plain text * escaped, otherwise falls back to formatting to plain text. If the markup result would be just the result of * {@link #formatToPlainText(TemplateNumberModel)} escaped, it must return the {@link String} that * {@link #formatToPlainText(TemplateNumberModel)} does. * * <p> * The implementation in {@link TemplateNumberFormat} simply calls {@link #formatToPlainText(TemplateNumberModel)}. * * @return A {@link String} or a {@link TemplateMarkupOutputModel}; not {@code null}. */ public Object format(TemplateNumberModel numberModel) throws TemplateValueFormatException, TemplateModelException { return formatToPlainText(numberModel); }
TemplateNumberFormat format = env.getTemplateNumberFormat(exp, false); try { return assertFormatResultNotNull(format.format(tnm)); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, false);
public static TemplateException newCantFormatNumberException(TemplateNumberFormat format, Expression dataSrcExp, TemplateValueFormatException e, boolean useTempModelExc) { _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder( "Failed to format number with format ", new _DelayedJQuote(format.getDescription()), ": ", e.getMessage()) .blame(dataSrcExp); return useTempModelExc ? new _TemplateModelException(e, (Environment) null, desc) : new _MiscTemplateException(e, (Environment) null, desc); }
@Override public void setLocale(Locale locale) { Locale prevLocale = getLocale(); super.setLocale(locale); if (!locale.equals(prevLocale)) { cachedTemplateNumberFormats = null; if (cachedTemplateNumberFormat != null && cachedTemplateNumberFormat.isLocaleBound()) { cachedTemplateNumberFormat = null; } if (cachedTempDateFormatArray != null) { for (int i = 0; i < CACHED_TDFS_LENGTH; i++) { final TemplateDateFormat f = cachedTempDateFormatArray[i]; if (f != null && f.isLocaleBound()) { cachedTempDateFormatArray[i] = null; } } } cachedTempDateFormatsByFmtStrArray = null; cachedCollator = null; } }
/** * Formats the model to markup instead of to plain text if the result markup will be more than just plain text * escaped, otherwise falls back to formatting to plain text. If the markup result would be just the result of * {@link #formatToPlainText(TemplateNumberModel)} escaped, it must return the {@link String} that * {@link #formatToPlainText(TemplateNumberModel)} does. * * <p> * The implementation in {@link TemplateNumberFormat} simply calls {@link #formatToPlainText(TemplateNumberModel)}. * * @return A {@link String} or a {@link TemplateMarkupOutputModel}; not {@code null}. */ public Object format(TemplateNumberModel numberModel) throws TemplateValueFormatException, TemplateModelException { return formatToPlainText(numberModel); }
TemplateNumberFormat format = env.getTemplateNumberFormat(exp, false); try { return ensureFormatResultString(format.format(tnm), exp, env); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, false);
/** * Format number with the number format specified as the parameter, with the current locale. * * @param exp * The blamed expression if an error occurs; it's only needed for better error messages */ String formatNumberToPlainText( TemplateNumberModel number, TemplateNumberFormat format, Expression exp, boolean useTempModelExc) throws TemplateException { try { return EvalUtil.assertFormatResultNotNull(format.formatToPlainText(number)); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, useTempModelExc); } }
TemplateNumberFormat format = env.getTemplateNumberFormat(exp, false); try { return assertFormatResultNotNull(format.format(tnm)); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, false);
/** * Format number with the number format specified as the parameter, with the current locale. * * @param exp * The blamed expression if an error occurs; it's only needed for better error messages */ String formatNumberToPlainText( TemplateNumberModel number, TemplateNumberFormat format, Expression exp, boolean useTempModelExc) throws TemplateException { try { return EvalUtil.assertFormatResultNotNull(format.formatToPlainText(number)); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, useTempModelExc); } }
@Test void formattingEnglish() throws TemplateValueFormatException, TemplateModelException { final BigDecimal n = new BigDecimal("0.0001"); final TemplateNumberFormat f = InterestNumberFormatFactory.INSTANCE.get("", Locale.ENGLISH, Environment.getCurrentEnvironment()); final TemplateNumberModel m = () -> n; final Object result = f.formatToPlainText(m); assertThat(result).isEqualTo("0.01%"); }