/** * Returns the number format as {@link TemplateNumberFormat} for the given format string and the current locale. * (The current locale is the locale returned by {@link #getLocale()}.) Note that the result will be cached in the * {@link Environment} instance (though at least in 2.3.24 the cache will be flushed if the current locale of the * {@link Environment} is changed). * * @param formatString * A string that you could also use as the value of the {@code numberFormat} configuration setting. Can't * be {@code null}. * * @since 2.3.24 */ public TemplateNumberFormat getTemplateNumberFormat(String formatString) throws TemplateValueFormatException { return getTemplateNumberFormat(formatString, true); }
/** * Returns the number format as {@link TemplateNumberFormat}, for the given format string and locale. To get a * number format for the current locale, use {@link #getTemplateNumberFormat(String)} instead. * * <p> * Note on performance (which was true at least for 2.3.24): Unless the locale happens to be equal to the current * locale, the {@link Environment}-level format cache can't be used, so the format string has to be parsed and the * matching factory has to be get an invoked, which is much more expensive than getting the format from the cache. * Thus the returned format should be stored by the caller for later reuse (but only within the current thread and * in relation to the current {@link Environment}), if it will be needed frequently. * * @param formatString * A string that you could also use as the value of the {@code numberFormat} configuration setting. * @param locale * The locale of the number format; not {@code null}. * * @since 2.3.24 */ public TemplateNumberFormat getTemplateNumberFormat(String formatString, Locale locale) throws TemplateValueFormatException { if (locale.equals(getLocale())) { getTemplateNumberFormat(formatString); } return getTemplateNumberFormatWithoutCache(formatString, locale); }
/** * Format number with the default number format. * * @param exp * The blamed expression if an error occurs; it's only needed for better error messages */ String formatNumberToPlainText(TemplateNumberModel number, Expression exp, boolean useTempModelExc) throws TemplateException { return formatNumberToPlainText(number, getTemplateNumberFormat(exp, useTempModelExc), exp, useTempModelExc); }
/** * Returns the current number format ({@link #getNumberFormat()}) as {@link TemplateNumberFormat}. * * <p> * Performance notes: The result is stored for reuse, so calling this method frequently is usually not a problem. * However, at least as of this writing (2.3.24), changing the current locale {@link #setLocale(Locale)} or changing * the current number format ({@link #setNumberFormat(String)}) will drop the stored value, so it will have to be * recalculated. * * @since 2.3.24 */ public TemplateNumberFormat getTemplateNumberFormat() throws TemplateValueFormatException { TemplateNumberFormat format = cachedTemplateNumberFormat; if (format == null) { format = getTemplateNumberFormat(getNumberFormat(), false); cachedTemplateNumberFormat = format; } return format; }
NumberFormatter(TemplateNumberModel numberModel, Environment env) throws TemplateException { this.env = env; // As we format lazily, we need a snapshot of the format inputs: this.numberModel = numberModel; number = EvalUtil.modelToNumber(numberModel, target); // for BackwardCompatibleTemplateNumberFormat-s try { defaultFormat = env.getTemplateNumberFormat(stringBI.this, true); } catch (TemplateException e) { // `e` should always be a TemplateModelException here, but to be sure: throw _CoreAPI.ensureIsTemplateModelException("Failed to get default number format", e); } }
@Override public TemplateNumberFormat get(String params, Locale locale, Environment env) throws TemplateValueFormatException { TemplateFormatUtil.checkHasNoParameters(params); try { String targetFormatString; if (localizedTargetFormatStrings != null) { Locale lookupLocale = locale; targetFormatString = localizedTargetFormatStrings.get(lookupLocale); while (targetFormatString == null && (lookupLocale = _CoreLocaleUtils.getLessSpecificLocale(lookupLocale)) != null) { targetFormatString = localizedTargetFormatStrings.get(lookupLocale); } } else { targetFormatString = null; } if (targetFormatString == null) { targetFormatString = this.defaultTargetFormatString; } return env.getTemplateNumberFormat(targetFormatString, locale); } catch (TemplateValueFormatException e) { throw new AliasTargetTemplateValueFormatException("Failed to create format based on target format string, " + StringUtil.jQuote(params) + ". Reason given: " + e.getMessage(), e); } }
public TemplateModel get(String key) throws TemplateModelException { TemplateNumberFormat format; try { format = env.getTemplateNumberFormat(key, stringBI.this, true); } catch (TemplateException e) { // `e` should always be a TemplateModelException here, but to be sure: throw _CoreAPI.ensureIsTemplateModelException("Failed to get number format", e); } String result; try { if (format instanceof BackwardCompatibleTemplateNumberFormat) { result = env.formatNumberToPlainText(number, (BackwardCompatibleTemplateNumberFormat) format, target); } else { result = env.formatNumberToPlainText(numberModel, format, target, true); } } catch (TemplateException e) { // `e` should always be a TemplateModelException here, but to be sure: throw _CoreAPI.ensureIsTemplateModelException("Failed to format number", e); } return new SimpleScalar(result); }
/** * Convenience wrapper around {@link #getTemplateNumberFormat(String)} to be called during expression evaluation. * * @param exp * The blamed expression if an error occurs; it's only needed for better error messages */ TemplateNumberFormat getTemplateNumberFormat(String formatString, Expression exp, boolean useTempModelExc) throws TemplateException { TemplateNumberFormat format; try { format = getTemplateNumberFormat(formatString); } catch (TemplateValueFormatException e) { _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder( "Failed to get number format object for the ", new _DelayedJQuote(formatString), " number format string: ", e.getMessage()) .blame(exp); throw useTempModelExc ? new _TemplateModelException(e, this, desc) : new _MiscTemplateException(e, this, desc); } return format; }
/** * Convenience wrapper around {@link #getTemplateNumberFormat()} to be called during expression evaluation. */ TemplateNumberFormat getTemplateNumberFormat(Expression exp, boolean useTempModelExc) throws TemplateException { TemplateNumberFormat format; try { format = getTemplateNumberFormat(); } catch (TemplateValueFormatException e) { _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder( "Failed to get number format object for the current number format string, ", new _DelayedJQuote(getNumberFormat()), ": ", e.getMessage()) .blame(exp); throw useTempModelExc ? new _TemplateModelException(e, this, desc) : new _MiscTemplateException(e, this, desc); } return format; }
if (tm instanceof TemplateNumberModel) { TemplateNumberModel tnm = (TemplateNumberModel) tm; TemplateNumberFormat format = env.getTemplateNumberFormat(exp, false); try { return assertFormatResultNotNull(format.format(tnm));
if (tm instanceof TemplateNumberModel) { TemplateNumberModel tnm = (TemplateNumberModel) tm; TemplateNumberFormat format = env.getTemplateNumberFormat(exp, false); try { return ensureFormatResultString(format.format(tnm), exp, env);
/** * Returns the number format as {@link TemplateNumberFormat} for the given format string and the current locale. * (The current locale is the locale returned by {@link #getLocale()}.) Note that the result will be cached in the * {@link Environment} instance (though at least in 2.3.24 the cache will be flushed if the current locale of the * {@link Environment} is changed). * * @param formatString * A string that you could also use as the value of the {@code numberFormat} configuration setting. Can't * be {@code null}. * * @since 2.3.24 */ public TemplateNumberFormat getTemplateNumberFormat(String formatString) throws TemplateValueFormatException { return getTemplateNumberFormat(formatString, true); }
/** * Returns the number format as {@link TemplateNumberFormat} for the given format string and the current locale. * (The current locale is the locale returned by {@link #getLocale()}.) Note that the result will be cached in the * {@link Environment} instance (though at least in 2.3.24 the cache will be flushed if the current locale of the * {@link Environment} is changed). * * @param formatString * A string that you could also use as the value of the {@code numberFormat} configuration setting. Can't * be {@code null}. * * @since 2.3.24 */ public TemplateNumberFormat getTemplateNumberFormat(String formatString) throws TemplateValueFormatException { return getTemplateNumberFormat(formatString, true); }
/** * Format number with the default number format. * * @param exp * The blamed expression if an error occurs; it's only needed for better error messages */ String formatNumberToPlainText(TemplateNumberModel number, Expression exp, boolean useTempModelExc) throws TemplateException { return formatNumberToPlainText(number, getTemplateNumberFormat(exp, useTempModelExc), exp, useTempModelExc); }
/** * Format number with the default number format. * * @param exp * The blamed expression if an error occurs; it's only needed for better error messages */ String formatNumberToPlainText(TemplateNumberModel number, Expression exp, boolean useTempModelExc) throws TemplateException { return formatNumberToPlainText(number, getTemplateNumberFormat(exp, useTempModelExc), exp, useTempModelExc); }
/** * Returns the current number format ({@link #getNumberFormat()}) as {@link TemplateNumberFormat}. * * <p> * Performance notes: The result is stored for reuse, so calling this method frequently is usually not a problem. * However, at least as of this writing (2.3.24), changing the current locale {@link #setLocale(Locale)} or changing * the current number format ({@link #setNumberFormat(String)}) will drop the stored value, so it will have to be * recalculated. * * @since 2.3.24 */ public TemplateNumberFormat getTemplateNumberFormat() throws TemplateValueFormatException { TemplateNumberFormat format = cachedTemplateNumberFormat; if (format == null) { format = getTemplateNumberFormat(getNumberFormat(), false); cachedTemplateNumberFormat = format; } return format; }
NumberFormatter(TemplateNumberModel numberModel, Environment env) throws TemplateException { this.env = env; // As we format lazily, we need a snapshot of the format inputs: this.numberModel = numberModel; number = EvalUtil.modelToNumber(numberModel, target); // for BackwardCompatibleTemplateNumberFormat-s try { defaultFormat = env.getTemplateNumberFormat(stringBI.this, true); } catch (TemplateException e) { // `e` should always be a TemplateModelException here, but to be sure: throw _CoreAPI.ensureIsTemplateModelException("Failed to get default number format", e); } }
NumberFormatter(TemplateNumberModel numberModel, Environment env) throws TemplateException { this.env = env; // As we format lazily, we need a snapshot of the format inputs: this.numberModel = numberModel; number = EvalUtil.modelToNumber(numberModel, target); // for BackwardCompatibleTemplateNumberFormat-s try { defaultFormat = env.getTemplateNumberFormat(stringBI.this, true); } catch (TemplateException e) { // `e` should always be a TemplateModelException here, but to be sure: throw _CoreAPI.ensureIsTemplateModelException("Failed to get default number format", e); } }
/** * Convenience wrapper around {@link #getTemplateNumberFormat()} to be called during expression evaluation. */ TemplateNumberFormat getTemplateNumberFormat(Expression exp, boolean useTempModelExc) throws TemplateException { TemplateNumberFormat format; try { format = getTemplateNumberFormat(); } catch (TemplateValueFormatException e) { _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder( "Failed to get number format object for the current number format string, ", new _DelayedJQuote(getNumberFormat()), ": ", e.getMessage()) .blame(exp); throw useTempModelExc ? new _TemplateModelException(e, this, desc) : new _MiscTemplateException(e, this, desc); } return format; }
/** * Convenience wrapper around {@link #getTemplateNumberFormat()} to be called during expression evaluation. */ TemplateNumberFormat getTemplateNumberFormat(Expression exp, boolean useTempModelExc) throws TemplateException { TemplateNumberFormat format; try { format = getTemplateNumberFormat(); } catch (TemplateValueFormatException e) { _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder( "Failed to get number format object for the current number format string, ", new _DelayedJQuote(getNumberFormat()), ": ", e.getMessage()) .blame(exp); throw useTempModelExc ? new _TemplateModelException(e, this, desc) : new _MiscTemplateException(e, this, desc); } return format; }