src/java.base/share/classes/java/text/BreakIterator.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58288 48e480e56aad
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
     2
 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The original version of this source code and documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * subsidiary of IBM. These materials are provided under terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * of a License Agreement between Taligent and Sun. This technology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * is protected by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
package java.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.text.spi.BreakIteratorProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Locale;
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
    46
import sun.util.locale.provider.LocaleProviderAdapter;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
    47
import sun.util.locale.provider.LocaleServiceProviderPool;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    51
 * The {@code BreakIterator} class implements methods for finding
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    52
 * the location of boundaries in text. Instances of {@code BreakIterator}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * maintain a current position and scan over text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * returning the index of characters where boundaries occur.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    55
 * Internally, {@code BreakIterator} scans text using a
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    56
 * {@code CharacterIterator}, and is thus able to scan text held
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    57
 * by any object implementing that protocol. A {@code StringCharacterIterator}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    58
 * is used to scan {@code String} objects passed to {@code setText}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * You use the factory methods provided by this class to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * instances of various types of break iterators. In particular,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    63
 * use {@code getWordInstance}, {@code getLineInstance},
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    64
 * {@code getSentenceInstance}, and {@code getCharacterInstance}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    65
 * to create {@code BreakIterator}s that perform
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * word, line, sentence, and character boundary analysis respectively.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    67
 * A single {@code BreakIterator} can work only on one unit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * (word, line, sentence, and so on). You must use a different iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * for each unit boundary analysis you wish to perform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 25859
diff changeset
    71
 * <p><a id="line"></a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * Line boundary analysis determines where a text string can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * broken when line-wrapping. The mechanism correctly handles
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * punctuation and hyphenated words. Actual line breaking needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * to also consider the available line width and is handled by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * higher-level software.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 25859
diff changeset
    78
 * <p><a id="sentence"></a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * Sentence boundary analysis allows selection with correct interpretation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * of periods within numbers and abbreviations, and trailing punctuation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * marks such as quotation marks and parentheses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 25859
diff changeset
    83
 * <p><a id="word"></a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * Word boundary analysis is used by search and replace functions, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * well as within text editing applications that allow the user to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * select words with a double click. Word selection provides correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * interpretation of punctuation marks within and following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * words. Characters that are not part of a word, such as symbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * or punctuation marks, have word-breaks on both sides.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 25859
diff changeset
    91
 * <p><a id="character"></a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * Character boundary analysis allows users to interact with characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * as they expect to, for example, when moving the cursor through a text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * string. Character boundary analysis provides correct navigation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * through character strings, regardless of how the character is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * The boundaries returned may be those of supplementary characters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * combining character sequences, or ligature clusters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * For example, an accented character might be stored as a base character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * and a diacritical mark. What users consider to be a character can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * differ between languages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   103
 * The {@code BreakIterator} instances returned by the factory methods
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * of this class are intended for use with natural languages only, not for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * programming language text. It is however possible to define subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * that tokenize a programming language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <strong>Examples</strong>:<P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * Creating and using text boundaries:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *      if (args.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *          String stringToExamine = args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *          //print each word in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *          BreakIterator boundary = BreakIterator.getWordInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *          boundary.setText(stringToExamine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *          printEachForward(boundary, stringToExamine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *          //print each sentence in reverse order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *          boundary = BreakIterator.getSentenceInstance(Locale.US);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *          boundary.setText(stringToExamine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *          printEachBackward(boundary, stringToExamine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *          printFirst(boundary, stringToExamine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *          printLast(boundary, stringToExamine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * Print each element in order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * public static void printEachForward(BreakIterator boundary, String source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *     int start = boundary.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *     for (int end = boundary.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *          end != BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *          start = end, end = boundary.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *          System.out.println(source.substring(start,end));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * Print each element in reverse order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * public static void printEachBackward(BreakIterator boundary, String source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *     int end = boundary.last();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *     for (int start = boundary.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *          start != BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *          end = start, start = boundary.previous()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *         System.out.println(source.substring(start,end));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * Print first element:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * public static void printFirst(BreakIterator boundary, String source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 *     int start = boundary.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *     int end = boundary.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *     System.out.println(source.substring(start,end));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * Print last element:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * public static void printLast(BreakIterator boundary, String source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *     int end = boundary.last();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *     int start = boundary.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *     System.out.println(source.substring(start,end));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * Print the element at a specified position:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * public static void printAt(BreakIterator boundary, int pos, String source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *     int end = boundary.following(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *     int start = boundary.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *     System.out.println(source.substring(start,end));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * Find the next word:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * <blockquote>
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 14342
diff changeset
   194
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * public static int nextWordStartAfter(int pos, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *     BreakIterator wb = BreakIterator.getWordInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *     wb.setText(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *     int last = wb.following(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *     int current = wb.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *     while (current != BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *         for (int p = last; p < current; p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *             if (Character.isLetter(text.codePointAt(p)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *                 return last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *         last = current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 *         current = wb.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *     return BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * }
19054
a64012cb49d6 8021108: Clean up doclint warnings and errors in java.text package
peytoia
parents: 14342
diff changeset
   210
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * (The iterator returned by BreakIterator.getWordInstance() is unique in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * the break positions it returns don't represent both the start and end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * thing being iterated over.  That is, a sentence-break iterator returns breaks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * that each represent the end of one sentence and the beginning of the next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * With the word-break iterator, the characters between two boundaries might be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * word, or they might be the punctuation or whitespace between two words.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * above code uses a simple heuristic to determine which boundary is the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * of a word: If the characters between this boundary and the next boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * include at least one letter (this can be an alphabetical letter, a CJK ideograph,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * a Hangul syllable, a Kana character, etc.), then the text between this boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * and the next is a word; otherwise, it's the material between words.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 *
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 44844
diff changeset
   224
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * @see CharacterIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
public abstract class BreakIterator implements Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Constructor. BreakIterator is stateless and has no default behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    protected BreakIterator()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Create a copy of this iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @return A copy of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   242
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public Object clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   249
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * DONE is returned by previous(), next(), next(int), preceding(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * and following(int) when either the first or last text boundary has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public static final int DONE = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Returns the first boundary. The iterator's current position is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * to the first text boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @return The character index of the first text boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public abstract int first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Returns the last boundary. The iterator's current position is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * to the last text boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @return The character index of the last text boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public abstract int last();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Returns the nth boundary from the current boundary. If either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * the first or last text boundary has been reached, it returns
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   277
     * {@code BreakIterator.DONE} and the current position is set to either
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * the first or last text boundary depending on which one is reached. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * the iterator's current position is set to the new boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * For example, if the iterator's current position is the mth text boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * and three more boundaries exist from the current boundary to the last text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * boundary, the next(2) call will return m + 2. The new text position is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * to the (m + 2)th text boundary. A next(4) call would return
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   284
     * {@code BreakIterator.DONE} and the last text boundary would become the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * new text position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param n which boundary to return.  A value of 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * does nothing.  Negative values move to previous boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * and positive values move to later boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @return The character index of the nth boundary from the current position
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   290
     * or {@code BreakIterator.DONE} if either first or last text boundary
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public abstract int next(int n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Returns the boundary following the current boundary. If the current boundary
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   297
     * is the last text boundary, it returns {@code BreakIterator.DONE} and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * the iterator's current position is unchanged. Otherwise, the iterator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * current position is set to the boundary following the current boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @return The character index of the next text boundary or
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   301
     * {@code BreakIterator.DONE} if the current boundary is the last text
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Equivalent to next(1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @see #next(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public abstract int next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Returns the boundary preceding the current boundary. If the current boundary
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   310
     * is the first text boundary, it returns {@code BreakIterator.DONE} and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * the iterator's current position is unchanged. Otherwise, the iterator's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * current position is set to the boundary preceding the current boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @return The character index of the previous text boundary or
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   314
     * {@code BreakIterator.DONE} if the current boundary is the first text
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public abstract int previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Returns the first boundary following the specified character offset. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * specified offset equals to the last text boundary, it returns
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   322
     * {@code BreakIterator.DONE} and the iterator's current position is unchanged.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Otherwise, the iterator's current position is set to the returned boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * The value returned is always greater than the offset or the value
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   325
     * {@code BreakIterator.DONE}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @param offset the character offset to begin scanning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @return The first boundary after the specified offset or
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   328
     * {@code BreakIterator.DONE} if the last text boundary is passed in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * as the offset.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   330
     * @throws     IllegalArgumentException if the specified offset is less than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * the first text boundary or greater than the last text boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public abstract int following(int offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Returns the last boundary preceding the specified character offset. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * specified offset equals to the first text boundary, it returns
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   338
     * {@code BreakIterator.DONE} and the iterator's current position is unchanged.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Otherwise, the iterator's current position is set to the returned boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * The value returned is always less than the offset or the value
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   341
     * {@code BreakIterator.DONE}.
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19054
diff changeset
   342
     * @param offset the character offset to begin scanning.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @return The last boundary before the specified offset or
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   344
     * {@code BreakIterator.DONE} if the first text boundary is passed in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * as the offset.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   346
     * @throws      IllegalArgumentException if the specified offset is less than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * the first text boundary or greater than the last text boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public int preceding(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        // NOTE:  This implementation is here solely because we can't add new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        // abstract methods to an existing class.  There is almost ALWAYS a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        // better, faster way to do this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        int pos = following(offset);
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   355
        while (pos >= offset && pos != DONE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            pos = previous();
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   357
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Returns true if the specified character offset is a text boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param offset the character offset to check.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   364
     * @return {@code true} if "offset" is a boundary position,
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   365
     * {@code false} otherwise.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   366
     * @throws      IllegalArgumentException if the specified offset is less than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * the first text boundary or greater than the last text boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public boolean isBoundary(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        // NOTE: This implementation probably is wrong for most situations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // because it fails to take into account the possibility that a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // CharacterIterator passed to setText() may not have a begin offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // of 0.  But since the abstract BreakIterator doesn't have that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        // knowledge, it assumes the begin offset is 0.  If you subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        // BreakIterator, copy the SimpleTextBoundary implementation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        // function into your subclass.  [This should have been abstract at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // this level, but it's too late to fix that now.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (offset == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        int boundary = following(offset - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (boundary == DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return boundary == offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Returns character index of the text boundary that was most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * recently returned by next(), next(int), previous(), first(), last(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * following(int) or preceding(int). If any of these methods returns
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   393
     * {@code BreakIterator.DONE} because either first or last text boundary
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * has been reached, it returns the first or last text boundary depending on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * which one is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @return The text boundary returned from the above methods, first or last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * text boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @see #next()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @see #next(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @see #previous()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @see #first()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @see #last()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @see #following(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @see #preceding(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public abstract int current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Get the text being scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @return the text being scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public abstract CharacterIterator getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Set a new text string to be scanned.  The current scan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * position is reset to first().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param newText new text to scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    public void setText(String newText)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        setText(new StringCharacterIterator(newText));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Set a new text for scanning.  The current scan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * position is reset to first().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param newText new text to scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public abstract void setText(CharacterIterator newText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    private static final int CHARACTER_INDEX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    private static final int WORD_INDEX = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    private static final int LINE_INDEX = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    private static final int SENTENCE_INDEX = 3;
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10883
diff changeset
   435
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10883
diff changeset
   436
    @SuppressWarnings("unchecked")
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10883
diff changeset
   437
    private static final SoftReference<BreakIteratorCache>[] iterCache = (SoftReference<BreakIteratorCache>[]) new SoftReference<?>[4];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   440
     * Returns a new {@code BreakIterator} instance
10883
d77f8ddbc904 7090046: Lots of invalid link in java.text.BreakIterator comments
peytoia
parents: 10419
diff changeset
   441
     * for <a href="BreakIterator.html#word">word breaks</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * for the {@linkplain Locale#getDefault() default locale}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return A break iterator for word breaks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public static BreakIterator getWordInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return getWordInstance(Locale.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   451
     * Returns a new {@code BreakIterator} instance
10883
d77f8ddbc904 7090046: Lots of invalid link in java.text.BreakIterator comments
peytoia
parents: 10419
diff changeset
   452
     * for <a href="BreakIterator.html#word">word breaks</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * for the given locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @param locale the desired locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @return A break iterator for word breaks
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   456
     * @throws    NullPointerException if {@code locale} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    public static BreakIterator getWordInstance(Locale locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   460
        return getBreakInstance(locale, WORD_INDEX);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   464
     * Returns a new {@code BreakIterator} instance
10883
d77f8ddbc904 7090046: Lots of invalid link in java.text.BreakIterator comments
peytoia
parents: 10419
diff changeset
   465
     * for <a href="BreakIterator.html#line">line breaks</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * for the {@linkplain Locale#getDefault() default locale}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @return A break iterator for line breaks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    public static BreakIterator getLineInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        return getLineInstance(Locale.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   475
     * Returns a new {@code BreakIterator} instance
10883
d77f8ddbc904 7090046: Lots of invalid link in java.text.BreakIterator comments
peytoia
parents: 10419
diff changeset
   476
     * for <a href="BreakIterator.html#line">line breaks</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * for the given locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @param locale the desired locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @return A break iterator for line breaks
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   480
     * @throws    NullPointerException if {@code locale} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public static BreakIterator getLineInstance(Locale locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   484
        return getBreakInstance(locale, LINE_INDEX);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   488
     * Returns a new {@code BreakIterator} instance
10883
d77f8ddbc904 7090046: Lots of invalid link in java.text.BreakIterator comments
peytoia
parents: 10419
diff changeset
   489
     * for <a href="BreakIterator.html#character">character breaks</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * for the {@linkplain Locale#getDefault() default locale}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @return A break iterator for character breaks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    public static BreakIterator getCharacterInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        return getCharacterInstance(Locale.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   499
     * Returns a new {@code BreakIterator} instance
10883
d77f8ddbc904 7090046: Lots of invalid link in java.text.BreakIterator comments
peytoia
parents: 10419
diff changeset
   500
     * for <a href="BreakIterator.html#character">character breaks</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * for the given locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @param locale the desired locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @return A break iterator for character breaks
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   504
     * @throws    NullPointerException if {@code locale} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public static BreakIterator getCharacterInstance(Locale locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   508
        return getBreakInstance(locale, CHARACTER_INDEX);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   512
     * Returns a new {@code BreakIterator} instance
10883
d77f8ddbc904 7090046: Lots of invalid link in java.text.BreakIterator comments
peytoia
parents: 10419
diff changeset
   513
     * for <a href="BreakIterator.html#sentence">sentence breaks</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * for the {@linkplain Locale#getDefault() default locale}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @return A break iterator for sentence breaks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public static BreakIterator getSentenceInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return getSentenceInstance(Locale.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   523
     * Returns a new {@code BreakIterator} instance
10883
d77f8ddbc904 7090046: Lots of invalid link in java.text.BreakIterator comments
peytoia
parents: 10419
diff changeset
   524
     * for <a href="BreakIterator.html#sentence">sentence breaks</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * for the given locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @param locale the desired locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @return A break iterator for sentence breaks
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   528
     * @throws    NullPointerException if {@code locale} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    public static BreakIterator getSentenceInstance(Locale locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   532
        return getBreakInstance(locale, SENTENCE_INDEX);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   535
    private static BreakIterator getBreakInstance(Locale locale, int type) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        if (iterCache[type] != null) {
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10883
diff changeset
   537
            BreakIteratorCache cache = iterCache[type].get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (cache != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                if (cache.getLocale().equals(locale)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    return cache.createBreakInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   545
        BreakIterator result = createBreakInstance(locale, type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        BreakIteratorCache cache = new BreakIteratorCache(locale, result);
12848
da701d422d2c 7117230: clean up warnings in java.text
dbhole
parents: 10883
diff changeset
   547
        iterCache[type] = new SoftReference<>(cache);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   551
    private static BreakIterator createBreakInstance(Locale locale,
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   552
                                                     int type) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   553
        LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(BreakIteratorProvider.class, locale);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   554
        BreakIterator iterator = createBreakInstance(adapter, locale, type);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   555
        if (iterator == null) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   556
            iterator = createBreakInstance(LocaleProviderAdapter.forJRE(), locale, type);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   557
        }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   558
        return iterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   561
    private static BreakIterator createBreakInstance(LocaleProviderAdapter adapter, Locale locale, int type) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   562
        BreakIteratorProvider breakIteratorProvider = adapter.getBreakIteratorProvider();
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   563
        BreakIterator iterator = null;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   564
        switch (type) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   565
        case CHARACTER_INDEX:
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   566
            iterator = breakIteratorProvider.getCharacterInstance(locale);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   567
            break;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   568
        case WORD_INDEX:
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   569
            iterator = breakIteratorProvider.getWordInstance(locale);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   570
            break;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   571
        case LINE_INDEX:
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   572
            iterator = breakIteratorProvider.getLineInstance(locale);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   573
            break;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   574
        case SENTENCE_INDEX:
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   575
            iterator = breakIteratorProvider.getSentenceInstance(locale);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   576
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12848
diff changeset
   578
        return iterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Returns an array of all locales for which the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   583
     * {@code get*Instance} methods of this class can return
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * localized instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * The returned array represents the union of locales supported by the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * runtime and by installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * {@link java.text.spi.BreakIteratorProvider BreakIteratorProvider} implementations.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   588
     * It must contain at least a {@code Locale}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * instance equal to {@link java.util.Locale#US Locale.US}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @return An array of locales for which localized
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   592
     *         {@code BreakIterator} instances are available.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    public static synchronized Locale[] getAvailableLocales()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        LocaleServiceProviderPool pool =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            LocaleServiceProviderPool.getPool(BreakIteratorProvider.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        return pool.getAvailableLocales();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    private static final class BreakIteratorCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        private BreakIterator iter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        private Locale locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        BreakIteratorCache(Locale locale, BreakIterator iter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            this.locale = locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            this.iter = (BreakIterator) iter.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        Locale getLocale() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            return locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        BreakIterator createBreakInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            return (BreakIterator) iter.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
}