mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8300093: Refactor code examples to use @snippet in java.text.MessageFormat
Reviewed-by: iris, naoto
This commit is contained in:
parent
d7c05d1870
commit
ade08e190c
1 changed files with 24 additions and 12 deletions
|
@ -230,14 +230,16 @@ import java.util.Locale;
|
||||||
* <p>
|
* <p>
|
||||||
* The first example uses the static method {@code MessageFormat.format},
|
* The first example uses the static method {@code MessageFormat.format},
|
||||||
* which internally creates a {@code MessageFormat} for one-time use:
|
* which internally creates a {@code MessageFormat} for one-time use:
|
||||||
* <blockquote><pre>
|
* <blockquote>
|
||||||
|
* {@snippet lang=java :
|
||||||
* int planet = 7;
|
* int planet = 7;
|
||||||
* String event = "a disturbance in the Force";
|
* String event = "a disturbance in the Force";
|
||||||
*
|
*
|
||||||
* String result = MessageFormat.format(
|
* String result = MessageFormat.format(
|
||||||
* "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
|
* "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
|
||||||
* planet, new Date(), event);
|
* planet, new Date(), event);
|
||||||
* </pre></blockquote>
|
* }
|
||||||
|
* </blockquote>
|
||||||
* The output is:
|
* The output is:
|
||||||
* <blockquote><pre>
|
* <blockquote><pre>
|
||||||
* At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7.
|
* At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7.
|
||||||
|
@ -246,7 +248,8 @@ import java.util.Locale;
|
||||||
* <p>
|
* <p>
|
||||||
* The following example creates a {@code MessageFormat} instance that
|
* The following example creates a {@code MessageFormat} instance that
|
||||||
* can be used repeatedly:
|
* can be used repeatedly:
|
||||||
* <blockquote><pre>
|
* <blockquote>
|
||||||
|
* {@snippet lang=java :
|
||||||
* int fileCount = 1273;
|
* int fileCount = 1273;
|
||||||
* String diskName = "MyDisk";
|
* String diskName = "MyDisk";
|
||||||
* Object[] testArgs = {Long.valueOf(fileCount), diskName};
|
* Object[] testArgs = {Long.valueOf(fileCount), diskName};
|
||||||
|
@ -255,7 +258,8 @@ import java.util.Locale;
|
||||||
* "The disk \"{1}\" contains {0} file(s).");
|
* "The disk \"{1}\" contains {0} file(s).");
|
||||||
*
|
*
|
||||||
* System.out.println(form.format(testArgs));
|
* System.out.println(form.format(testArgs));
|
||||||
* </pre></blockquote>
|
* }
|
||||||
|
* </blockquote>
|
||||||
* The output with different values for {@code fileCount}:
|
* The output with different values for {@code fileCount}:
|
||||||
* <blockquote><pre>
|
* <blockquote><pre>
|
||||||
* The disk "MyDisk" contains 0 file(s).
|
* The disk "MyDisk" contains 0 file(s).
|
||||||
|
@ -266,7 +270,8 @@ import java.util.Locale;
|
||||||
* <p>
|
* <p>
|
||||||
* For more sophisticated patterns, you can use a {@code ChoiceFormat}
|
* For more sophisticated patterns, you can use a {@code ChoiceFormat}
|
||||||
* to produce correct forms for singular and plural:
|
* to produce correct forms for singular and plural:
|
||||||
* <blockquote><pre>
|
* <blockquote>
|
||||||
|
* {@snippet lang=java :
|
||||||
* MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
|
* MessageFormat form = new MessageFormat("The disk \"{1}\" contains {0}.");
|
||||||
* double[] filelimits = {0,1,2};
|
* double[] filelimits = {0,1,2};
|
||||||
* String[] filepart = {"no files","one file","{0,number} files"};
|
* String[] filepart = {"no files","one file","{0,number} files"};
|
||||||
|
@ -278,7 +283,8 @@ import java.util.Locale;
|
||||||
* Object[] testArgs = {Long.valueOf(fileCount), diskName};
|
* Object[] testArgs = {Long.valueOf(fileCount), diskName};
|
||||||
*
|
*
|
||||||
* System.out.println(form.format(testArgs));
|
* System.out.println(form.format(testArgs));
|
||||||
* </pre></blockquote>
|
* }
|
||||||
|
* </blockquote>
|
||||||
* The output with different values for {@code fileCount}:
|
* The output with different values for {@code fileCount}:
|
||||||
* <blockquote><pre>
|
* <blockquote><pre>
|
||||||
* The disk "MyDisk" contains no files.
|
* The disk "MyDisk" contains no files.
|
||||||
|
@ -290,10 +296,12 @@ import java.util.Locale;
|
||||||
* You can create the {@code ChoiceFormat} programmatically, as in the
|
* You can create the {@code ChoiceFormat} programmatically, as in the
|
||||||
* above example, or by using a pattern. See {@link ChoiceFormat}
|
* above example, or by using a pattern. See {@link ChoiceFormat}
|
||||||
* for more information.
|
* for more information.
|
||||||
* <blockquote><pre>{@code
|
* <blockquote>
|
||||||
|
* {@snippet lang=java :
|
||||||
* form.applyPattern(
|
* form.applyPattern(
|
||||||
* "There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.");
|
* "There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.");
|
||||||
* }</pre></blockquote>
|
* }
|
||||||
|
* </blockquote>
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* <strong>Note:</strong> As we see above, the string produced
|
* <strong>Note:</strong> As we see above, the string produced
|
||||||
|
@ -305,25 +313,29 @@ import java.util.Locale;
|
||||||
* <p>
|
* <p>
|
||||||
* When a single argument is parsed more than once in the string, the last match
|
* When a single argument is parsed more than once in the string, the last match
|
||||||
* will be the final result of the parsing. For example,
|
* will be the final result of the parsing. For example,
|
||||||
* <blockquote><pre>
|
* <blockquote>
|
||||||
|
* {@snippet lang=java :
|
||||||
* MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");
|
* MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");
|
||||||
* Object[] objs = {Double.valueOf(3.1415)};
|
* Object[] objs = {Double.valueOf(3.1415)};
|
||||||
* String result = mf.format( objs );
|
* String result = mf.format( objs );
|
||||||
* // result now equals "3.14, 3.1"
|
* // result now equals "3.14, 3.1"
|
||||||
* objs = mf.parse(result, new ParsePosition(0));
|
* objs = mf.parse(result, new ParsePosition(0));
|
||||||
* // objs now equals {Double.valueOf(3.1)}
|
* // objs now equals {Double.valueOf(3.1)}
|
||||||
* </pre></blockquote>
|
* }
|
||||||
|
* </blockquote>
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* Likewise, parsing with a {@code MessageFormat} object using patterns containing
|
* Likewise, parsing with a {@code MessageFormat} object using patterns containing
|
||||||
* multiple occurrences of the same argument would return the last match. For
|
* multiple occurrences of the same argument would return the last match. For
|
||||||
* example,
|
* example,
|
||||||
* <blockquote><pre>
|
* <blockquote>
|
||||||
|
* {@snippet lang=java :
|
||||||
* MessageFormat mf = new MessageFormat("{0}, {0}, {0}");
|
* MessageFormat mf = new MessageFormat("{0}, {0}, {0}");
|
||||||
* String forParsing = "x, y, z";
|
* String forParsing = "x, y, z";
|
||||||
* Object[] objs = mf.parse(forParsing, new ParsePosition(0));
|
* Object[] objs = mf.parse(forParsing, new ParsePosition(0));
|
||||||
* // result now equals {new String("z")}
|
* // result now equals {new String("z")}
|
||||||
* </pre></blockquote>
|
* }
|
||||||
|
* </blockquote>
|
||||||
*
|
*
|
||||||
* <h3><a id="synchronization">Synchronization</a></h3>
|
* <h3><a id="synchronization">Synchronization</a></h3>
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue