803 * <th scope="row" style="text-weight-normal"><code>instanceof Number</code> 804 * <td><code>NumberFormat.getInstance(getLocale()).format(argument)</code> 805 * <tr> 806 * <th scope="row" style="text-weight-normal"><code>instanceof Date</code> 807 * <td><code>DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, getLocale()).format(argument)</code> 808 * <tr> 809 * <th scope="row" style="text-weight-normal"><code>instanceof String</code> 810 * <td><code>argument</code> 811 * <tr> 812 * <th scope="row" style="text-weight-normal"><i>any</i> 813 * <td><code>argument.toString()</code> 814 * </tbody> 815 * </table> 816 * <p> 817 * If <code>pos</code> is non-null, and refers to 818 * <code>Field.ARGUMENT</code>, the location of the first formatted 819 * string will be returned. 820 * 821 * @param arguments an array of objects to be formatted and substituted. 822 * @param result where text is appended. 823 * @param pos On input: an alignment field, if desired. 824 * On output: the offsets of the alignment field. 825 * @return the string buffer passed in as {@code result}, with formatted 826 * text appended 827 * @exception IllegalArgumentException if an argument in the 828 * <code>arguments</code> array is not of the type 829 * expected by the format element(s) that use it. 830 * @exception NullPointerException if {@code result} is {@code null} 831 */ 832 public final StringBuffer format(Object[] arguments, StringBuffer result, 833 FieldPosition pos) 834 { 835 return subformat(arguments, result, pos, null); 836 } 837 838 /** 839 * Creates a MessageFormat with the given pattern and uses it 840 * to format the given arguments. This is equivalent to 841 * <blockquote> 842 * <code>(new {@link #MessageFormat(String) MessageFormat}(pattern)).{@link #format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) format}(arguments, new StringBuffer(), null).toString()</code> 843 * </blockquote> 844 * 851 * that use it. 852 * @exception NullPointerException if {@code pattern} is {@code null} 853 */ 854 public static String format(String pattern, Object ... arguments) { 855 MessageFormat temp = new MessageFormat(pattern); 856 return temp.format(arguments); 857 } 858 859 // Overrides 860 /** 861 * Formats an array of objects and appends the <code>MessageFormat</code>'s 862 * pattern, with format elements replaced by the formatted objects, to the 863 * provided <code>StringBuffer</code>. 864 * This is equivalent to 865 * <blockquote> 866 * <code>{@link #format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) format}((Object[]) arguments, result, pos)</code> 867 * </blockquote> 868 * 869 * @param arguments an array of objects to be formatted and substituted. 870 * @param result where text is appended. 871 * @param pos On input: an alignment field, if desired. 872 * On output: the offsets of the alignment field. 873 * @exception IllegalArgumentException if an argument in the 874 * <code>arguments</code> array is not of the type 875 * expected by the format element(s) that use it. 876 * @exception NullPointerException if {@code result} is {@code null} 877 */ 878 public final StringBuffer format(Object arguments, StringBuffer result, 879 FieldPosition pos) 880 { 881 return subformat((Object[]) arguments, result, pos, null); 882 } 883 884 /** 885 * Formats an array of objects and inserts them into the 886 * <code>MessageFormat</code>'s pattern, producing an 887 * <code>AttributedCharacterIterator</code>. 888 * You can use the returned <code>AttributedCharacterIterator</code> 889 * to build the resulting String, as well as to determine information 890 * about the resulting String. 891 * <p> 892 * The text of the returned <code>AttributedCharacterIterator</code> is 1222 private int[] offsets = new int[INITIAL_FORMATS]; 1223 1224 /** 1225 * The argument numbers corresponding to each formatter. (The formatters are stored 1226 * in the order they occur in the pattern, not in the order in which the arguments 1227 * are specified.) 1228 * @serial 1229 */ 1230 private int[] argumentNumbers = new int[INITIAL_FORMATS]; 1231 1232 /** 1233 * One less than the number of entries in <code>offsets</code>. Can also be thought of 1234 * as the index of the highest-numbered element in <code>offsets</code> that is being used. 1235 * All of these arrays should have the same number of elements being used as <code>offsets</code> 1236 * does, and so this variable suffices to tell us how many entries are in all of them. 1237 * @serial 1238 */ 1239 private int maxOffset = -1; 1240 1241 /** 1242 * Internal routine used by format. If <code>characterIterators</code> is 1243 * non-null, AttributedCharacterIterator will be created from the 1244 * subformats as necessary. If <code>characterIterators</code> is null 1245 * and <code>fp</code> is non-null and identifies 1246 * <code>Field.MESSAGE_ARGUMENT</code>, the location of 1247 * the first replaced argument will be set in it. 1248 * 1249 * @exception IllegalArgumentException if an argument in the 1250 * <code>arguments</code> array is not of the type 1251 * expected by the format element(s) that use it. 1252 */ 1253 private StringBuffer subformat(Object[] arguments, StringBuffer result, 1254 FieldPosition fp, List<AttributedCharacterIterator> characterIterators) { 1255 // note: this implementation assumes a fast substring & index. 1256 // if this is not true, would be better to append chars one by one. 1257 int lastOffset = 0; 1258 int last = result.length(); 1259 for (int i = 0; i <= maxOffset; ++i) { 1260 result.append(pattern, lastOffset, offsets[i]); 1261 lastOffset = offsets[i]; 1262 int argumentNumber = argumentNumbers[i]; 1263 if (arguments == null || argumentNumber >= arguments.length) { 1264 result.append('{').append(argumentNumber).append('}'); 1265 continue; 1266 } | 803 * <th scope="row" style="text-weight-normal"><code>instanceof Number</code> 804 * <td><code>NumberFormat.getInstance(getLocale()).format(argument)</code> 805 * <tr> 806 * <th scope="row" style="text-weight-normal"><code>instanceof Date</code> 807 * <td><code>DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, getLocale()).format(argument)</code> 808 * <tr> 809 * <th scope="row" style="text-weight-normal"><code>instanceof String</code> 810 * <td><code>argument</code> 811 * <tr> 812 * <th scope="row" style="text-weight-normal"><i>any</i> 813 * <td><code>argument.toString()</code> 814 * </tbody> 815 * </table> 816 * <p> 817 * If <code>pos</code> is non-null, and refers to 818 * <code>Field.ARGUMENT</code>, the location of the first formatted 819 * string will be returned. 820 * 821 * @param arguments an array of objects to be formatted and substituted. 822 * @param result where text is appended. 823 * @param pos keeps track on the position of the first replaced argument 824 in the output string. 825 * @return the string buffer passed in as {@code result}, with formatted 826 * text appended 827 * @exception IllegalArgumentException if an argument in the 828 * <code>arguments</code> array is not of the type 829 * expected by the format element(s) that use it. 830 * @exception NullPointerException if {@code result} is {@code null} 831 */ 832 public final StringBuffer format(Object[] arguments, StringBuffer result, 833 FieldPosition pos) 834 { 835 return subformat(arguments, result, pos, null); 836 } 837 838 /** 839 * Creates a MessageFormat with the given pattern and uses it 840 * to format the given arguments. This is equivalent to 841 * <blockquote> 842 * <code>(new {@link #MessageFormat(String) MessageFormat}(pattern)).{@link #format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) format}(arguments, new StringBuffer(), null).toString()</code> 843 * </blockquote> 844 * 851 * that use it. 852 * @exception NullPointerException if {@code pattern} is {@code null} 853 */ 854 public static String format(String pattern, Object ... arguments) { 855 MessageFormat temp = new MessageFormat(pattern); 856 return temp.format(arguments); 857 } 858 859 // Overrides 860 /** 861 * Formats an array of objects and appends the <code>MessageFormat</code>'s 862 * pattern, with format elements replaced by the formatted objects, to the 863 * provided <code>StringBuffer</code>. 864 * This is equivalent to 865 * <blockquote> 866 * <code>{@link #format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) format}((Object[]) arguments, result, pos)</code> 867 * </blockquote> 868 * 869 * @param arguments an array of objects to be formatted and substituted. 870 * @param result where text is appended. 871 * @param pos keeps track on the position of the first replaced argument 872 * in the output string. 873 * @exception IllegalArgumentException if an argument in the 874 * <code>arguments</code> array is not of the type 875 * expected by the format element(s) that use it. 876 * @exception NullPointerException if {@code result} is {@code null} 877 */ 878 public final StringBuffer format(Object arguments, StringBuffer result, 879 FieldPosition pos) 880 { 881 return subformat((Object[]) arguments, result, pos, null); 882 } 883 884 /** 885 * Formats an array of objects and inserts them into the 886 * <code>MessageFormat</code>'s pattern, producing an 887 * <code>AttributedCharacterIterator</code>. 888 * You can use the returned <code>AttributedCharacterIterator</code> 889 * to build the resulting String, as well as to determine information 890 * about the resulting String. 891 * <p> 892 * The text of the returned <code>AttributedCharacterIterator</code> is 1222 private int[] offsets = new int[INITIAL_FORMATS]; 1223 1224 /** 1225 * The argument numbers corresponding to each formatter. (The formatters are stored 1226 * in the order they occur in the pattern, not in the order in which the arguments 1227 * are specified.) 1228 * @serial 1229 */ 1230 private int[] argumentNumbers = new int[INITIAL_FORMATS]; 1231 1232 /** 1233 * One less than the number of entries in <code>offsets</code>. Can also be thought of 1234 * as the index of the highest-numbered element in <code>offsets</code> that is being used. 1235 * All of these arrays should have the same number of elements being used as <code>offsets</code> 1236 * does, and so this variable suffices to tell us how many entries are in all of them. 1237 * @serial 1238 */ 1239 private int maxOffset = -1; 1240 1241 /** 1242 * Internal routine used by format. If {@code characterIterators} is 1243 * {@code non-null}, AttributedCharacterIterator will be created from the 1244 * subformats as necessary. If {@code characterIterators} is {@code null} 1245 * and {@code fp} is {@code non-null} and identifies 1246 * {@code Field.ARGUMENT} as the field attribute, the location of 1247 * the first replaced argument will be set in it. 1248 * 1249 * @exception IllegalArgumentException if an argument in the 1250 * <code>arguments</code> array is not of the type 1251 * expected by the format element(s) that use it. 1252 */ 1253 private StringBuffer subformat(Object[] arguments, StringBuffer result, 1254 FieldPosition fp, List<AttributedCharacterIterator> characterIterators) { 1255 // note: this implementation assumes a fast substring & index. 1256 // if this is not true, would be better to append chars one by one. 1257 int lastOffset = 0; 1258 int last = result.length(); 1259 for (int i = 0; i <= maxOffset; ++i) { 1260 result.append(pattern, lastOffset, offsets[i]); 1261 lastOffset = offsets[i]; 1262 int argumentNumber = argumentNumbers[i]; 1263 if (arguments == null || argumentNumber >= arguments.length) { 1264 result.append('{').append(argumentNumber).append('}'); 1265 continue; 1266 } |