private Object[] makeReadable(MessageFormat format, Object[] arguments) { Format[] formats = format.getFormatsByArgumentIndex(); Object[] result = Arrays.copyOf(arguments, Math.min(arguments.length, formats.length)); for (int i = 0; i < result.length; i++) { if (formats[i] == null) { result[i] = StringUtils.nullSafeToString(arguments[i]); } } return result; }
private SoyErrorKind(MessageFormat messageFormat) { this.messageFormat = messageFormat; this.requiredArgs = messageFormat.getFormatsByArgumentIndex().length; }
private SoyErrorKind(MessageFormat messageFormat) { this.messageFormat = messageFormat; this.requiredArgs = messageFormat.getFormatsByArgumentIndex().length; }
/** * Returns the formats used for the values passed into * <code>format</code> methods or returned from <code>parse</code> * methods. The indices of elements in the returned array * correspond to the argument indices used in the previously set * pattern string. * The order of formats in the returned array thus corresponds to * the order of elements in the <code>arguments</code> array passed * to the <code>format</code> methods or the result array returned * by the <code>parse</code> methods. * <p> * If an argument index is used for more than one format element * in the pattern string, then the format used for the last such * format element is returned in the array. If an argument index * is not used for any format element in the pattern string, then * null is returned in the array. * * This method is only supported when exclusively numbers are used for * argument names. Otherwise an IllegalArgumentException is thrown. * * @return the formats used for the arguments within the pattern * @throws IllegalArgumentException if this format uses named arguments * @stable ICU 3.0 */ public Format[] getFormatsByArgumentIndex() { return messageFormat.getFormatsByArgumentIndex(); }
/** * Returns the formats used for the values passed into * <code>format</code> methods or returned from <code>parse</code> * methods. The indices of elements in the returned array * correspond to the argument indices used in the previously set * pattern string. * The order of formats in the returned array thus corresponds to * the order of elements in the <code>arguments</code> array passed * to the <code>format</code> methods or the result array returned * by the <code>parse</code> methods. * <p> * If an argument index is used for more than one format element * in the pattern string, then the format used for the last such * format element is returned in the array. If an argument index * is not used for any format element in the pattern string, then * null is returned in the array. * * This method is only supported when exclusively numbers are used for * argument names. Otherwise an IllegalArgumentException is thrown. * * @return the formats used for the arguments within the pattern * @throws IllegalArgumentException if this format uses named arguments * @stable ICU 3.0 */ public Format[] getFormatsByArgumentIndex() { return messageFormat.getFormatsByArgumentIndex(); }
MessageFormat simpleChoiceTest = new MessageFormat("{0}"); System.out.println(simpleChoiceTest.getFormatsByArgumentIndex()[0]); //Prints null MessageFormat explicitChoiceTest = new MessageFormat("{0,number,currency}"); System.out.println(explicitChoiceTest.getFormatsByArgumentIndex()[0]); //Prints java.text.DecimalFormat@67500
/** * Parameter check when invoking method. * * @param method * @param args */ protected static void checkParam(Method method, Object[] args) { TableErrorCode.ErrCode errCode = method.getAnnotation(TableErrorCode.ErrCode.class); String errDetail = errCode.details(); String errCause = errCode.cause(); MessageFormat format1 = new MessageFormat(errDetail); MessageFormat format2 = new MessageFormat(errCause); if (args == null || args.length == 0) { if ((format1.getFormatsByArgumentIndex() != null && format1.getFormatsByArgumentIndex().length > 0) || (format2.getFormatsByArgumentIndex() != null && format2.getFormatsByArgumentIndex().length > 0)) { throw new AssertionError("mismatched parameter length between " + method.getName() + " and its annotation @ErrCode"); } } else { if ((format1.getFormatsByArgumentIndex() != null && format1.getFormatsByArgumentIndex().length > args.length) || format1.getFormatsByArgumentIndex() == null || (format2.getFormatsByArgumentIndex() != null && format2.getFormatsByArgumentIndex().length > args.length)) { throw new AssertionError("mismatched parameter length between " + method.getName() + " and its annotation @ErrCode"); } } }
/** * Parameter check when invoking method. * * @param method * @param args */ protected static void checkParam(Method method, Object[] args) { ErrCode errCode = method.getAnnotation(ErrCode.class); String errDetail = errCode.details(); String errCause = errCode.cause(); MessageFormat format1 = new MessageFormat(errDetail); MessageFormat format2 = new MessageFormat(errCause); if (args == null || args.length == 0) { if ((format1.getFormatsByArgumentIndex() != null && format1.getFormatsByArgumentIndex().length > 0) || (format2.getFormatsByArgumentIndex() != null && format2.getFormatsByArgumentIndex().length > 0)) { throw new AssertionError("mismatched parameter length between " + method.getName() + " and its annotation @ErrCode"); } } else { if ((format1.getFormatsByArgumentIndex() != null && format1.getFormatsByArgumentIndex().length > args.length) || format1.getFormatsByArgumentIndex() == null || (format2.getFormatsByArgumentIndex() != null && format2.getFormatsByArgumentIndex().length > args.length)) { throw new AssertionError("mismatched parameter length between " + method.getName() + " and its annotation @ErrCode"); } } }
/** * @param projectionInterface * @param optionalParams * @return */ @SuppressWarnings("unchecked") Map<String, String> filterRequestParamsFromParams(final String url, final Object... optionalParams) { Map<String, String> requestParams = new HashMap<String, String>(); Format[] formats = new MessageFormat(url).getFormatsByArgumentIndex(); for (int i = 0; i < optionalParams.length; ++i) { if (i >= formats.length) { if ((optionalParams[i] instanceof Map<?, ?>)) { requestParams.putAll((Map<String, String>) optionalParams[i]); } continue; } if (formats[i] == null) { if ((optionalParams[i] instanceof Map<?, ?>)) { requestParams.putAll((Map<String, String>) optionalParams[i]); } } } return requestParams; }
/** * @param projectionInterface * @param optionalParams * @return */ @SuppressWarnings("unchecked") Map<String, String> filterRequestParamsFromParams(final String url, final Object... optionalParams) { Map<String, String> requestParams = new HashMap<String, String>(); Format[] formats = new MessageFormat(url).getFormatsByArgumentIndex(); for (int i = 0; i < optionalParams.length; ++i) { if (i >= formats.length) { if ((optionalParams[i] instanceof Map<?, ?>)) { requestParams.putAll((Map<String, String>) optionalParams[i]); } continue; } if (formats[i] == null) { if ((optionalParams[i] instanceof Map<?, ?>)) { requestParams.putAll((Map<String, String>) optionalParams[i]); } } } return requestParams; }
Format[] formats = mf.getFormatsByArgumentIndex();
Format[] formats = mf.getFormatsByArgumentIndex();
Format[] formats = mf.getFormatsByArgumentIndex();
String raw = raw(); MessageFormat format = new MessageFormat(raw); final Format[] formats = format.getFormatsByArgumentIndex(); final List<Class> types = new ArrayList<Class>(); final Class<?>[] parameterTypes = method.getParameterTypes();
public Object handle(TemplateContext context, VariableResolver varResolver, String expr) { List<Object> parameterList; try { parameterList = new Parameters(expr) .getEvaluatedParameterList(context); } catch (IllegalSyntaxException ex) { throw new IllegalArgumentException(ex); } if (parameterList.size() < 2) { throw new IllegalArgumentException( "The number of parameters are more than 1: " + expr); } MessageFormat mf = new MessageFormat(StringUtils.asString(parameterList .get(0)), findLocale(context)); Format[] formats = mf.getFormatsByArgumentIndex(); Object[] parameters = parameterList.subList(1, parameterList.size()) .toArray(new Object[0]); for (int i = 0; i < formats.length; i++) { if (formats[i] instanceof DateFormat) { ((DateFormat) formats[i]).setTimeZone(findTimeZone(context)); } if (i < parameters.length) { adjustFormatAndParameter(context, mf, parameters, formats, i); } } return mf.format(parameters); }
/** * Sets the date shown in the TodayPanel. * * PENDING JW ... quick api hack for testing. Don't recreate the panel if * it had been used * * @param linkDay the date used in the TodayPanel */ public void setLinkDay(Date linkDay) { this.linkDate = linkDay; Format[] formats = getLinkFormat().getFormatsByArgumentIndex(); for (Format format : formats) { if (format instanceof DateFormat) { ((DateFormat) format).setTimeZone(getTimeZone()); } } setLinkPanel(new TodayPanel()); }
/** * Sets the date shown in the TodayPanel. * * PENDING JW ... quick api hack for testing. Don't recreate the panel if * it had been used * * @param linkDay the date used in the TodayPanel */ public void setLinkDay(Date linkDay) { this.linkDate = linkDay; Format[] formats = getLinkFormat().getFormatsByArgumentIndex(); for (Format format : formats) { if (format instanceof DateFormat) { ((DateFormat) format).setTimeZone(getTimeZone()); } } setLinkPanel(new TodayPanel()); }
/** * Sets the date shown in the TodayPanel. * * PENDING JW ... quick api hack for testing. Don't recreate the panel if * it had been used * * @param linkDay the date used in the TodayPanel */ public void setLinkDay(Date linkDay) { this.linkDate = linkDay; Format[] formats = getLinkFormat().getFormatsByArgumentIndex(); for (Format format : formats) { if (format instanceof DateFormat) { ((DateFormat) format).setTimeZone(getTimeZone()); } } setLinkPanel(new TodayPanel()); }
/** * Sets the date shown in the TodayPanel. * * PENDING JW ... quick api hack for testing. Don't recreate the panel if * it had been used * * @param linkDay the date used in the TodayPanel */ public void setLinkDay(Date linkDay) { this.linkDate = linkDay; Format[] formats = getLinkFormat().getFormatsByArgumentIndex(); for (Format format : formats) { if (format instanceof DateFormat) { ((DateFormat) format).setTimeZone(getTimeZone()); } } setLinkPanel(new TodayPanel()); }
/** * Sets the date shown in the TodayPanel. * * PENDING JW ... quick api hack for testing. Don't recreate the panel if * it had been used * * @param linkDay the date used in the TodayPanel */ public void setLinkDay(Date linkDay) { this.linkDate = linkDay; Format[] formats = getLinkFormat().getFormatsByArgumentIndex(); for (Format format : formats) { if (format instanceof DateFormat) { ((DateFormat) format).setTimeZone(getTimeZone()); } } setLinkPanel(new TodayPanel()); }