/** * Replace any message argument identified by given <code>placeholder</code> with given argument values. * @param placeholder Argument placeholder (not null) * @param message Message to process * @param arguments Message arguments * @return Message with resolved arguments, or <code>null</code> if the message was <code>null</code> */ public static String resolveMessageArguments(String placeholder, String message, Object[] arguments) { ObjectUtils.argumentNotNull(placeholder, "Argument placeholder must be not null"); if (message != null && arguments != null && arguments.length > 0) { final Pattern pattern = Pattern.compile(FormatUtils.escapeRegexCharacters(placeholder)); String resolved = message; for (Object argument : arguments) { resolved = pattern.matcher(resolved).replaceFirst((argument != null) ? argument.toString() : ""); } return resolved; } return message; }
/** * Build a {@link NumberFormat} using default {@link Locale} * @param value Value to present * @param disableGrouping <code>true</code> to disable groups separator * @param hideZeroDecimals <code>true</code> to hide decimals when all zero * @return NumberFormat */ private static NumberFormat getNumberFormatForDefaultLocale(Number value, boolean disableGrouping, boolean hideZeroDecimals) { NumberFormat numberFormat; if (value != null && TypeUtils.isDecimalNumber(value.getClass())) { numberFormat = NumberFormat.getInstance(); if (hideZeroDecimals && !FormatUtils.hasDecimals(value.doubleValue())) { numberFormat.setMaximumFractionDigits(0); } } else { numberFormat = NumberFormat.getIntegerInstance(); } if (disableGrouping) { numberFormat.setGroupingUsed(false); } return numberFormat; }
/** * Replace any message argument identified by {@link #getMessageArgumentsPlaceholder()} with given argument values. * @param message Message to process * @param arguments Arguments * @return Message with resolved arguments. */ protected String resolveMessageArguments(String message, Object[] arguments) { return getMessageArgumentsPlaceholder().map(p -> FormatUtils.resolveMessageArguments(p, message, arguments)) .orElse(message); }
String trimmed = FormatUtils.trimAll(value); return (T) (FormatUtils.isHexNumber(trimmed) ? Byte.decode(trimmed) : Byte.valueOf(trimmed)); } else if (TypeUtils.isShort(targetClass)) { return (T) (FormatUtils.isHexNumber(trimmed) ? Short.decode(trimmed) : Short.valueOf(trimmed)); } else if (TypeUtils.isInteger(targetClass)) { return (T) (FormatUtils.isHexNumber(trimmed) ? Integer.decode(trimmed) : Integer.valueOf(trimmed)); } else if (TypeUtils.isLong(targetClass)) { return (T) (FormatUtils.isHexNumber(trimmed) ? Long.decode(trimmed) : Long.valueOf(trimmed)); } else if (BigInteger.class == targetClass) { return (T) (FormatUtils.isHexNumber(trimmed) ? decodeBigInteger(trimmed) : new BigInteger(trimmed)); } else if (TypeUtils.isFloat(targetClass)) { return (T) Float.valueOf(trimmed);
/** * Present given property <code>value</code> as a {@link String}, using current * {@link PropertyValuePresenterRegistry} if available as {@link Context} resource or the default instance * otherwise. If a {@link PropertyValuePresenter} is not available from registry, simple {@link Object#toString()} * representation of the value is returned. * @param value Value to present as a String * @return String presentation of the given <code>value</code> */ default String present(T value) { return PropertyValuePresenterRegistry.get().getPresenter(this).orElse((p, v) -> FormatUtils.toString(v)) .present(this, value); }
.map(ml -> FormatUtils.limit(v, ml, true)).orElse(v)).orElse(null);
@Override public BsonFilterExpression visit(StringMatchFilter filter, MongoResolutionContext<?> context) { return resolveOperationQueryFilter(context, filter, (c, fn) -> { final String value = resolveRightOperandAsNotNullString(filter, context); final String options = filter.isIgnoreCase() ? "i" : null; String regex = FormatUtils.escapeRegexCharacters(value); switch (filter.getMatchMode()) { case CONTAINS: regex = ".*" + regex + ".*"; break; case ENDS_WITH: regex = regex + "$"; break; case STARTS_WITH: regex = "^" + regex; break; default: break; } return Filters.regex(fn, regex, options); }); }
/** * Try to translate given <code>localizable</code> using the {@link LocalizationContext} available as * {@link Context} resource through {@link LocalizationContext#getCurrent()}. The LocalizationContext must be * localized. * @param localizable Localizable to translate * @param lenient if <code>true</code> and a {@link LocalizationContext} is not available or it is not localized, * silently return default {@link Localizable#getMessage()}. If <code>false</code>, a * {@link LocalizationException} is thrown for that situation * @return The localized message, or default {@link Localizable#getMessage()} if a localized LocalizationContext is * not available and lenient is <code>true</code>, or {@link Localizable#getMessageCode()} is * <code>null</code>. */ static String translate(final Localizable localizable, final boolean lenient) { ObjectUtils.argumentNotNull(localizable, "Localizable must be not null"); Optional<LocalizationContext> lc = LocalizationContext.getCurrent().filter(l -> l.isLocalized()); if (!lc.isPresent() && !lenient) { throw new LocalizationException( "A LocalizationContext is not available from context or it is not localized"); } return lc.map(l -> l.getMessage(localizable, lenient)) .orElse(FormatUtils.resolveMessageArguments(MessageProvider.DEFAULT_MESSAGE_ARGUMENT_PLACEHOLDER, localizable.getMessage(), localizable.getMessageArguments())); }
&& !FormatUtils.hasDecimals(number.doubleValue())) { format.setMaximumFractionDigits(0); } else if (decimals > -1) { && !FormatUtils.hasDecimals(number.doubleValue())) { format.setMaximumFractionDigits(0); } else if (decimals > -1) {