src/java.base/share/classes/java/text/MessageFormat.java
changeset 58242 94bb65cb37d3
parent 57956 e0b8b019d2f5
child 58288 48e480e56aad
equal deleted inserted replaced
58241:33de7752835c 58242:94bb65cb37d3
   360      * creates a list of subformats for the format elements contained in it.
   360      * creates a list of subformats for the format elements contained in it.
   361      * Patterns and their interpretation are specified in the
   361      * Patterns and their interpretation are specified in the
   362      * <a href="#patterns">class description</a>.
   362      * <a href="#patterns">class description</a>.
   363      *
   363      *
   364      * @param pattern the pattern for this message format
   364      * @param pattern the pattern for this message format
   365      * @exception IllegalArgumentException if the pattern is invalid
   365      * @throws    IllegalArgumentException if the pattern is invalid
   366      * @exception NullPointerException if {@code pattern} is
   366      * @throws    NullPointerException if {@code pattern} is
   367      *            {@code null}
   367      *            {@code null}
   368      */
   368      */
   369     public MessageFormat(String pattern) {
   369     public MessageFormat(String pattern) {
   370         this.locale = Locale.getDefault(Locale.Category.FORMAT);
   370         this.locale = Locale.getDefault(Locale.Category.FORMAT);
   371         applyPattern(pattern);
   371         applyPattern(pattern);
   379      * Patterns and their interpretation are specified in the
   379      * Patterns and their interpretation are specified in the
   380      * <a href="#patterns">class description</a>.
   380      * <a href="#patterns">class description</a>.
   381      *
   381      *
   382      * @param pattern the pattern for this message format
   382      * @param pattern the pattern for this message format
   383      * @param locale the locale for this message format
   383      * @param locale the locale for this message format
   384      * @exception IllegalArgumentException if the pattern is invalid
   384      * @throws    IllegalArgumentException if the pattern is invalid
   385      * @exception NullPointerException if {@code pattern} is
   385      * @throws    NullPointerException if {@code pattern} is
   386      *            {@code null}
   386      *            {@code null}
   387      * @since 1.4
   387      * @since 1.4
   388      */
   388      */
   389     public MessageFormat(String pattern, Locale locale) {
   389     public MessageFormat(String pattern, Locale locale) {
   390         this.locale = locale;
   390         this.locale = locale;
   428      * for the format elements contained in it.
   428      * for the format elements contained in it.
   429      * Patterns and their interpretation are specified in the
   429      * Patterns and their interpretation are specified in the
   430      * <a href="#patterns">class description</a>.
   430      * <a href="#patterns">class description</a>.
   431      *
   431      *
   432      * @param pattern the pattern for this message format
   432      * @param pattern the pattern for this message format
   433      * @exception IllegalArgumentException if the pattern is invalid
   433      * @throws    IllegalArgumentException if the pattern is invalid
   434      * @exception NullPointerException if {@code pattern} is
   434      * @throws    NullPointerException if {@code pattern} is
   435      *            {@code null}
   435      *            {@code null}
   436      */
   436      */
   437     @SuppressWarnings("fallthrough") // fallthrough in switch is expected, suppress it
   437     @SuppressWarnings("fallthrough") // fallthrough in switch is expected, suppress it
   438     public void applyPattern(String pattern) {
   438     public void applyPattern(String pattern) {
   439             StringBuilder[] segments = new StringBuilder[4];
   439             StringBuilder[] segments = new StringBuilder[4];
   612      * corresponding new format is ignored. If fewer formats are provided
   612      * corresponding new format is ignored. If fewer formats are provided
   613      * than needed, then only the formats for argument indices less
   613      * than needed, then only the formats for argument indices less
   614      * than <code>newFormats.length</code> are replaced.
   614      * than <code>newFormats.length</code> are replaced.
   615      *
   615      *
   616      * @param newFormats the new formats to use
   616      * @param newFormats the new formats to use
   617      * @exception NullPointerException if <code>newFormats</code> is null
   617      * @throws    NullPointerException if <code>newFormats</code> is null
   618      * @since 1.4
   618      * @since 1.4
   619      */
   619      */
   620     public void setFormatsByArgumentIndex(Format[] newFormats) {
   620     public void setFormatsByArgumentIndex(Format[] newFormats) {
   621         for (int i = 0; i <= maxOffset; i++) {
   621         for (int i = 0; i <= maxOffset; i++) {
   622             int j = argumentNumbers[i];
   622             int j = argumentNumbers[i];
   644      * order of elements in the <code>arguments</code> array passed to
   644      * order of elements in the <code>arguments</code> array passed to
   645      * the <code>format</code> methods or the result array returned by
   645      * the <code>format</code> methods or the result array returned by
   646      * the <code>parse</code> methods.
   646      * the <code>parse</code> methods.
   647      *
   647      *
   648      * @param newFormats the new formats to use
   648      * @param newFormats the new formats to use
   649      * @exception NullPointerException if <code>newFormats</code> is null
   649      * @throws    NullPointerException if <code>newFormats</code> is null
   650      */
   650      */
   651     public void setFormats(Format[] newFormats) {
   651     public void setFormats(Format[] newFormats) {
   652         int runsToCopy = newFormats.length;
   652         int runsToCopy = newFormats.length;
   653         if (runsToCopy > maxOffset + 1) {
   653         if (runsToCopy > maxOffset + 1) {
   654             runsToCopy = maxOffset + 1;
   654             runsToCopy = maxOffset + 1;
   696      * method, which accesses format elements based on the argument
   696      * method, which accesses format elements based on the argument
   697      * index they specify.
   697      * index they specify.
   698      *
   698      *
   699      * @param formatElementIndex the index of a format element within the pattern
   699      * @param formatElementIndex the index of a format element within the pattern
   700      * @param newFormat the format to use for the specified format element
   700      * @param newFormat the format to use for the specified format element
   701      * @exception ArrayIndexOutOfBoundsException if {@code formatElementIndex} is equal to or
   701      * @throws    ArrayIndexOutOfBoundsException if {@code formatElementIndex} is equal to or
   702      *            larger than the number of format elements in the pattern string
   702      *            larger than the number of format elements in the pattern string
   703      */
   703      */
   704     public void setFormat(int formatElementIndex, Format newFormat) {
   704     public void setFormat(int formatElementIndex, Format newFormat) {
   705 
   705 
   706         if (formatElementIndex > maxOffset) {
   706         if (formatElementIndex > maxOffset) {
   827      * @param result where text is appended.
   827      * @param result where text is appended.
   828      * @param pos keeps track on the position of the first replaced argument
   828      * @param pos keeps track on the position of the first replaced argument
   829                   in the output string.
   829                   in the output string.
   830      * @return the string buffer passed in as {@code result}, with formatted
   830      * @return the string buffer passed in as {@code result}, with formatted
   831      * text appended
   831      * text appended
   832      * @exception IllegalArgumentException if an argument in the
   832      * @throws    IllegalArgumentException if an argument in the
   833      *            <code>arguments</code> array is not of the type
   833      *            <code>arguments</code> array is not of the type
   834      *            expected by the format element(s) that use it.
   834      *            expected by the format element(s) that use it.
   835      * @exception NullPointerException if {@code result} is {@code null}
   835      * @throws    NullPointerException if {@code result} is {@code null}
   836      */
   836      */
   837     public final StringBuffer format(Object[] arguments, StringBuffer result,
   837     public final StringBuffer format(Object[] arguments, StringBuffer result,
   838                                      FieldPosition pos)
   838                                      FieldPosition pos)
   839     {
   839     {
   840         return subformat(arguments, result, pos, null);
   840         return subformat(arguments, result, pos, null);
   848      * </blockquote>
   848      * </blockquote>
   849      *
   849      *
   850      * @param pattern   the pattern string
   850      * @param pattern   the pattern string
   851      * @param arguments object(s) to format
   851      * @param arguments object(s) to format
   852      * @return the formatted string
   852      * @return the formatted string
   853      * @exception IllegalArgumentException if the pattern is invalid,
   853      * @throws    IllegalArgumentException if the pattern is invalid,
   854      *            or if an argument in the <code>arguments</code> array
   854      *            or if an argument in the <code>arguments</code> array
   855      *            is not of the type expected by the format element(s)
   855      *            is not of the type expected by the format element(s)
   856      *            that use it.
   856      *            that use it.
   857      * @exception NullPointerException if {@code pattern} is {@code null}
   857      * @throws    NullPointerException if {@code pattern} is {@code null}
   858      */
   858      */
   859     public static String format(String pattern, Object ... arguments) {
   859     public static String format(String pattern, Object ... arguments) {
   860         MessageFormat temp = new MessageFormat(pattern);
   860         MessageFormat temp = new MessageFormat(pattern);
   861         return temp.format(arguments);
   861         return temp.format(arguments);
   862     }
   862     }
   873      *
   873      *
   874      * @param arguments an array of objects to be formatted and substituted.
   874      * @param arguments an array of objects to be formatted and substituted.
   875      * @param result where text is appended.
   875      * @param result where text is appended.
   876      * @param pos keeps track on the position of the first replaced argument
   876      * @param pos keeps track on the position of the first replaced argument
   877      *            in the output string.
   877      *            in the output string.
   878      * @exception IllegalArgumentException if an argument in the
   878      * @throws    IllegalArgumentException if an argument in the
   879      *            <code>arguments</code> array is not of the type
   879      *            <code>arguments</code> array is not of the type
   880      *            expected by the format element(s) that use it.
   880      *            expected by the format element(s) that use it.
   881      * @exception NullPointerException if {@code result} is {@code null}
   881      * @throws    NullPointerException if {@code result} is {@code null}
   882      */
   882      */
   883     public final StringBuffer format(Object arguments, StringBuffer result,
   883     public final StringBuffer format(Object arguments, StringBuffer result,
   884                                      FieldPosition pos)
   884                                      FieldPosition pos)
   885     {
   885     {
   886         return subformat((Object[]) arguments, result, pos, null);
   886         return subformat((Object[]) arguments, result, pos, null);
   913      * This allows you to not only find where an argument is placed in the
   913      * This allows you to not only find where an argument is placed in the
   914      * resulting String, but also which fields it contains in turn.
   914      * resulting String, but also which fields it contains in turn.
   915      *
   915      *
   916      * @param arguments an array of objects to be formatted and substituted.
   916      * @param arguments an array of objects to be formatted and substituted.
   917      * @return AttributedCharacterIterator describing the formatted value.
   917      * @return AttributedCharacterIterator describing the formatted value.
   918      * @exception NullPointerException if <code>arguments</code> is null.
   918      * @throws    NullPointerException if <code>arguments</code> is null.
   919      * @exception IllegalArgumentException if an argument in the
   919      * @throws    IllegalArgumentException if an argument in the
   920      *            <code>arguments</code> array is not of the type
   920      *            <code>arguments</code> array is not of the type
   921      *            expected by the format element(s) that use it.
   921      *            expected by the format element(s) that use it.
   922      * @since 1.4
   922      * @since 1.4
   923      */
   923      */
   924     public AttributedCharacterIterator formatToCharacterIterator(Object arguments) {
   924     public AttributedCharacterIterator formatToCharacterIterator(Object arguments) {
   966      * If the source is null, return an empty array.
   966      * If the source is null, return an empty array.
   967      *
   967      *
   968      * @param source the string to parse
   968      * @param source the string to parse
   969      * @param pos    the parse position
   969      * @param pos    the parse position
   970      * @return an array of parsed objects
   970      * @return an array of parsed objects
   971      * @exception NullPointerException if {@code pos} is {@code null}
   971      * @throws    NullPointerException if {@code pos} is {@code null}
   972      *            for a non-null {@code source} string.
   972      *            for a non-null {@code source} string.
   973      */
   973      */
   974     public Object[] parse(String source, ParsePosition pos) {
   974     public Object[] parse(String source, ParsePosition pos) {
   975         if (source == null) {
   975         if (source == null) {
   976             Object[] empty = {};
   976             Object[] empty = {};
  1055      * See the {@link #parse(String, ParsePosition)} method for more information
  1055      * See the {@link #parse(String, ParsePosition)} method for more information
  1056      * on message parsing.
  1056      * on message parsing.
  1057      *
  1057      *
  1058      * @param source A <code>String</code> whose beginning should be parsed.
  1058      * @param source A <code>String</code> whose beginning should be parsed.
  1059      * @return An <code>Object</code> array parsed from the string.
  1059      * @return An <code>Object</code> array parsed from the string.
  1060      * @exception ParseException if the beginning of the specified string
  1060      * @throws    ParseException if the beginning of the specified string
  1061      *            cannot be parsed.
  1061      *            cannot be parsed.
  1062      */
  1062      */
  1063     public Object[] parse(String source) throws ParseException {
  1063     public Object[] parse(String source) throws ParseException {
  1064         ParsePosition pos  = new ParsePosition(0);
  1064         ParsePosition pos  = new ParsePosition(0);
  1065         Object[] result = parse(source, pos);
  1065         Object[] result = parse(source, pos);
  1251      * subformats as necessary. If {@code characterIterators} is {@code null}
  1251      * subformats as necessary. If {@code characterIterators} is {@code null}
  1252      * and {@code fp} is {@code non-null} and identifies
  1252      * and {@code fp} is {@code non-null} and identifies
  1253      * {@code Field.ARGUMENT} as the field attribute, the location of
  1253      * {@code Field.ARGUMENT} as the field attribute, the location of
  1254      * the first replaced argument will be set in it.
  1254      * the first replaced argument will be set in it.
  1255      *
  1255      *
  1256      * @exception IllegalArgumentException if an argument in the
  1256      * @throws    IllegalArgumentException if an argument in the
  1257      *            <code>arguments</code> array is not of the type
  1257      *            <code>arguments</code> array is not of the type
  1258      *            expected by the format element(s) that use it.
  1258      *            expected by the format element(s) that use it.
  1259      */
  1259      */
  1260     private StringBuffer subformat(Object[] arguments, StringBuffer result,
  1260     private StringBuffer subformat(Object[] arguments, StringBuffer result,
  1261                                    FieldPosition fp, List<AttributedCharacterIterator> characterIterators) {
  1261                                    FieldPosition fp, List<AttributedCharacterIterator> characterIterators) {