8300706: Use @snippet in java.text

Reviewed-by: naoto
This commit is contained in:
Justin Lu 2023-01-24 02:05:05 +00:00 committed by Naoto Sato
parent b5ee3d1f2a
commit 0323609f44
5 changed files with 58 additions and 62 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -115,7 +115,7 @@ import sun.util.locale.provider.LocaleServiceProviderPool;
* <strong>Examples</strong>:<P> * <strong>Examples</strong>:<P>
* Creating and using text boundaries: * Creating and using text boundaries:
* <blockquote> * <blockquote>
* <pre> * {@snippet lang=java :
* public static void main(String args[]) { * public static void main(String args[]) {
* if (args.length == 1) { * if (args.length == 1) {
* String stringToExamine = args[0]; * String stringToExamine = args[0];
@ -131,12 +131,12 @@ import sun.util.locale.provider.LocaleServiceProviderPool;
* printLast(boundary, stringToExamine); * printLast(boundary, stringToExamine);
* } * }
* } * }
* </pre> * }
* </blockquote> * </blockquote>
* *
* Print each element in order: * Print each element in order:
* <blockquote> * <blockquote>
* <pre> * {@snippet lang=java :
* public static void printEachForward(BreakIterator boundary, String source) { * public static void printEachForward(BreakIterator boundary, String source) {
* int start = boundary.first(); * int start = boundary.first();
* for (int end = boundary.next(); * for (int end = boundary.next();
@ -145,12 +145,12 @@ import sun.util.locale.provider.LocaleServiceProviderPool;
* System.out.println(source.substring(start,end)); * System.out.println(source.substring(start,end));
* } * }
* } * }
* </pre> * }
* </blockquote> * </blockquote>
* *
* Print each element in reverse order: * Print each element in reverse order:
* <blockquote> * <blockquote>
* <pre> * {@snippet lang=java :
* public static void printEachBackward(BreakIterator boundary, String source) { * public static void printEachBackward(BreakIterator boundary, String source) {
* int end = boundary.last(); * int end = boundary.last();
* for (int start = boundary.previous(); * for (int start = boundary.previous();
@ -159,45 +159,45 @@ import sun.util.locale.provider.LocaleServiceProviderPool;
* System.out.println(source.substring(start,end)); * System.out.println(source.substring(start,end));
* } * }
* } * }
* </pre> * }
* </blockquote> * </blockquote>
* *
* Print first element: * Print first element:
* <blockquote> * <blockquote>
* <pre> * {@snippet lang=java :
* public static void printFirst(BreakIterator boundary, String source) { * public static void printFirst(BreakIterator boundary, String source) {
* int start = boundary.first(); * int start = boundary.first();
* int end = boundary.next(); * int end = boundary.next();
* System.out.println(source.substring(start,end)); * System.out.println(source.substring(start,end));
* } * }
* </pre> * }
* </blockquote> * </blockquote>
* *
* Print last element: * Print last element:
* <blockquote> * <blockquote>
* <pre> * {@snippet lang=java :
* public static void printLast(BreakIterator boundary, String source) { * public static void printLast(BreakIterator boundary, String source) {
* int end = boundary.last(); * int end = boundary.last();
* int start = boundary.previous(); * int start = boundary.previous();
* System.out.println(source.substring(start,end)); * System.out.println(source.substring(start,end));
* } * }
* </pre> * }
* </blockquote> * </blockquote>
* *
* Print the element at a specified position: * Print the element at a specified position:
* <blockquote> * <blockquote>
* <pre> *{@snippet lang=java :
* public static void printAt(BreakIterator boundary, int pos, String source) { * public static void printAt(BreakIterator boundary, int pos, String source) {
* int end = boundary.following(pos); * int end = boundary.following(pos);
* int start = boundary.previous(); * int start = boundary.previous();
* System.out.println(source.substring(start,end)); * System.out.println(source.substring(start,end));
* } * }
* </pre> * }
* </blockquote> * </blockquote>
* *
* Find the next word: * Find the next word:
* <blockquote> * <blockquote>
* <pre>{@code * {@snippet lang=java :
* public static int nextWordStartAfter(int pos, String text) { * public static int nextWordStartAfter(int pos, String text) {
* BreakIterator wb = BreakIterator.getWordInstance(); * BreakIterator wb = BreakIterator.getWordInstance();
* wb.setText(text); * wb.setText(text);
@ -213,7 +213,7 @@ import sun.util.locale.provider.LocaleServiceProviderPool;
* } * }
* return BreakIterator.DONE; * return BreakIterator.DONE;
* } * }
* }</pre> * }
* (The iterator returned by BreakIterator.getWordInstance() is unique in that * (The iterator returned by BreakIterator.getWordInstance() is unique in that
* the break positions it returns don't represent both the start and end of the * the break positions it returns don't represent both the start and end of the
* thing being iterated over. That is, a sentence-break iterator returns breaks * thing being iterated over. That is, a sentence-break iterator returns breaks

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -62,27 +62,27 @@ package java.text;
* <P>Examples:<P> * <P>Examples:<P>
* *
* Traverse the text from start to finish * Traverse the text from start to finish
* <pre>{@code * {@snippet lang=java :
* public void traverseForward(CharacterIterator iter) { * public void traverseForward(CharacterIterator iter) {
* for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) { * for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
* processChar(c); * processChar(c);
* } * }
* } * }
* }</pre> * }
* *
* Traverse the text backwards, from end to start * Traverse the text backwards, from end to start
* <pre>{@code * {@snippet lang=java :
* public void traverseBackward(CharacterIterator iter) { * public void traverseBackward(CharacterIterator iter) {
* for (char c = iter.last(); c != CharacterIterator.DONE; c = iter.previous()) { * for (char c = iter.last(); c != CharacterIterator.DONE; c = iter.previous()) {
* processChar(c); * processChar(c);
* } * }
* } * }
* }</pre> * }
* *
* Traverse both forward and backward from a given position in the text. * Traverse both forward and backward from a given position in the text.
* Calls to notBoundary() in this example represents some * Calls to notBoundary() in this example represents some
* additional stopping criteria. * additional stopping criteria.
* <pre>{@code * {@snippet lang=java :
* public void traverseOut(CharacterIterator iter, int pos) { * public void traverseOut(CharacterIterator iter, int pos) {
* for (char c = iter.setIndex(pos); * for (char c = iter.setIndex(pos);
* c != CharacterIterator.DONE && notBoundary(c); * c != CharacterIterator.DONE && notBoundary(c);
@ -96,7 +96,7 @@ package java.text;
* int start = iter.getIndex(); * int start = iter.getIndex();
* processSection(start, end); * processSection(start, end);
* } * }
* }</pre> * }
* *
* @since 1.1 * @since 1.1
* @see StringCharacterIterator * @see StringCharacterIterator

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -78,9 +78,9 @@ import sun.util.locale.provider.TimeZoneNameUtility;
* If you decide to create a date-time formatter with a specific * If you decide to create a date-time formatter with a specific
* format pattern for a specific locale, you can do so with: * format pattern for a specific locale, you can do so with:
* <blockquote> * <blockquote>
* <pre> * {@snippet lang=java :
* new SimpleDateFormat(aPattern, DateFormatSymbols.getInstance(aLocale)). * new SimpleDateFormat(aPattern, DateFormatSymbols.getInstance(aLocale));
* </pre> * }
* </blockquote> * </blockquote>
* *
* <p>If the locale contains "rg" (region override) * <p>If the locale contains "rg" (region override)

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -70,12 +70,12 @@ import sun.util.locale.provider.ResourceBundleBasedAdapter;
* {@code DecimalFormat}. If you need to customize the format object, do * {@code DecimalFormat}. If you need to customize the format object, do
* something like this: * something like this:
* *
* <blockquote><pre> * <blockquote>{@snippet lang=java :
* NumberFormat f = NumberFormat.getInstance(loc); * NumberFormat numFormat = NumberFormat.getInstance(loc);
* if (f instanceof DecimalFormat) { * if (numFormat instanceof DecimalFormat decFormat) {
* ((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true); * decFormat.setDecimalSeparatorAlwaysShown(true);
* } * }
* </pre></blockquote> * }</blockquote>
* *
* <p>A {@code DecimalFormat} comprises a <em>pattern</em> and a set of * <p>A {@code DecimalFormat} comprises a <em>pattern</em> and a set of
* <em>symbols</em>. The pattern may be set directly using * <em>symbols</em>. The pattern may be set directly using
@ -338,31 +338,27 @@ import sun.util.locale.provider.ResourceBundleBasedAdapter;
* *
* <h3>Example</h3> * <h3>Example</h3>
* *
* <blockquote><pre><strong>{@code * <blockquote>{@snippet lang=java :
* // Print out a number using the localized number, integer, currency, * // Print out a number using the localized number, integer, currency,
* // and percent format for each locale}</strong>{@code * // and percent format for each locale
* Locale[] locales = NumberFormat.getAvailableLocales(); * Locale[] locales = NumberFormat.getAvailableLocales();
* double myNumber = -1234.56; * double myNumber = -1234.56;
* NumberFormat form; * NumberFormat form;
* for (int j = 0; j < 4; ++j) { * for (int j = 0; j < 4; ++j) {
* System.out.println("FORMAT"); * System.out.println("FORMAT");
* for (int i = 0; i < locales.length; ++i) { * for (Locale locale : locales) {
* if (locales[i].getCountry().length() == 0) { * if (locale.getCountry().length() == 0) {
* continue; // Skip language-only locales * continue; // Skip language-only locales
* } * }
* System.out.print(locales[i].getDisplayName()); * System.out.print(locale.getDisplayName());
* switch (j) { * form = switch (j) {
* case 0: * case 0 -> NumberFormat.getInstance(locale);
* form = NumberFormat.getInstance(locales[i]); break; * case 1 -> NumberFormat.getIntegerInstance(locale);
* case 1: * case 2 -> NumberFormat.getCurrencyInstance(locale);
* form = NumberFormat.getIntegerInstance(locales[i]); break; * default -> NumberFormat.getPercentInstance(locale);
* case 2: * };
* form = NumberFormat.getCurrencyInstance(locales[i]); break; * if (form instanceof DecimalFormat decForm) {
* default: * System.out.print(": " + decForm.toPattern());
* form = NumberFormat.getPercentInstance(locales[i]); break;
* }
* if (form instanceof DecimalFormat) {
* System.out.print(": " + ((DecimalFormat) form).toPattern());
* } * }
* System.out.print(" -> " + form.format(myNumber)); * System.out.print(" -> " + form.format(myNumber));
* try { * try {
@ -370,7 +366,7 @@ import sun.util.locale.provider.ResourceBundleBasedAdapter;
* } catch (ParseException e) {} * } catch (ParseException e) {}
* } * }
* } * }
* }</pre></blockquote> * }</blockquote>
* *
* @see <a href="http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html">Java Tutorial</a> * @see <a href="http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html">Java Tutorial</a>
* @see NumberFormat * @see NumberFormat

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -71,28 +71,28 @@ import sun.util.locale.provider.LocaleServiceProviderPool;
* To format a number for the current Locale, use one of the factory * To format a number for the current Locale, use one of the factory
* class methods: * class methods:
* <blockquote> * <blockquote>
* <pre>{@code * {@snippet lang=java :
* myString = NumberFormat.getInstance().format(myNumber); * myString = NumberFormat.getInstance().format(myNumber);
* }</pre> * }
* </blockquote> * </blockquote>
* If you are formatting multiple numbers, it is * If you are formatting multiple numbers, it is
* more efficient to get the format and use it multiple times so that * more efficient to get the format and use it multiple times so that
* the system doesn't have to fetch the information about the local * the system doesn't have to fetch the information about the local
* language and country conventions multiple times. * language and country conventions multiple times.
* <blockquote> * <blockquote>
* <pre>{@code * {@snippet lang=java :
* NumberFormat nf = NumberFormat.getInstance(); * NumberFormat nf = NumberFormat.getInstance();
* for (int i = 0; i < myNumber.length; ++i) { * for (int i = 0; i < myNumber.length; ++i) {
* output.println(nf.format(myNumber[i]) + "; "); * output.println(nf.format(myNumber[i]) + "; ");
* } * }
* }</pre> * }
* </blockquote> * </blockquote>
* To format a number for a different Locale, specify it in the * To format a number for a different Locale, specify it in the
* call to {@code getInstance}. * call to {@code getInstance}.
* <blockquote> * <blockquote>
* <pre>{@code * {@snippet lang=java :
* NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH); * NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
* }</pre> * }
* </blockquote> * </blockquote>
* *
* <p>If the locale contains "nu" (numbers) and/or "rg" (region override) * <p>If the locale contains "nu" (numbers) and/or "rg" (region override)
@ -103,9 +103,9 @@ import sun.util.locale.provider.LocaleServiceProviderPool;
* *
* <p>You can also use a {@code NumberFormat} to parse numbers: * <p>You can also use a {@code NumberFormat} to parse numbers:
* <blockquote> * <blockquote>
* <pre>{@code * {@snippet lang=java :
* myNumber = nf.parse(myString); * myNumber = nf.parse(myString);
* }</pre> * }
* </blockquote> * </blockquote>
* Use {@code getInstance} or {@code getNumberInstance} to get the * Use {@code getInstance} or {@code getNumberInstance} to get the
* normal number format. Use {@code getIntegerInstance} to get an * normal number format. Use {@code getIntegerInstance} to get an