1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.  Oracle designates this
9 * particular file as subject to the "Classpath" exception as provided
10 * by Oracle in the LICENSE file that accompanied this code.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23 * or visit www.oracle.com if you need additional information or have any
24 * questions.
25 */
26
27/*
28 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
29 * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved
30 *
31 *   The original version of this source code and documentation is copyrighted
32 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
33 * materials are provided under terms of a License Agreement between Taligent
34 * and Sun. This technology is protected by multiple US and International
35 * patents. This notice and attribution to Taligent may not be removed.
36 *   Taligent is a registered trademark of Taligent, Inc.
37 *
38 */
39
40package java.text;
41
42/**
43 * The <code>CollationElementIterator</code> class is used as an iterator
44 * to walk through each character of an international string. Use the iterator
45 * to return the ordering priority of the positioned character. The ordering
46 * priority of a character, which we refer to as a key, defines how a character
47 * is collated in the given collation object.
48 *
49 * <p>
50 * For example, consider the following in Spanish:
51 * <blockquote>
52 * <pre>
53 * "ca" -> the first key is key('c') and second key is key('a').
54 * "cha" -> the first key is key('ch') and second key is key('a').
55 * </pre>
56 * </blockquote>
57 * And in German,
58 * <blockquote>
59 * <pre>
60 * "\u00e4b"-> the first key is key('a'), the second key is key('e'), and
61 * the third key is key('b').
62 * </pre>
63 * </blockquote>
64 * The key of a character is an integer composed of primary order(short),
65 * secondary order(byte), and tertiary order(byte). Java strictly defines
66 * the size and signedness of its primitive data types. Therefore, the static
67 * functions <code>primaryOrder</code>, <code>secondaryOrder</code>, and
68 * <code>tertiaryOrder</code> return <code>int</code>, <code>short</code>,
69 * and <code>short</code> respectively to ensure the correctness of the key
70 * value.
71 *
72 * <p>
73 * Example of the iterator usage,
74 * <blockquote>
75 * <pre>
76 *
77 *  String testString = "This is a test";
78 *  Collator col = Collator.getInstance();
79 *  if (col instanceof RuleBasedCollator) {
80 *      RuleBasedCollator ruleBasedCollator = (RuleBasedCollator)col;
81 *      CollationElementIterator collationElementIterator = ruleBasedCollator.getCollationElementIterator(testString);
82 *      int primaryOrder = CollationElementIterator.primaryOrder(collationElementIterator.next());
83 *          :
84 *  }
85 * </pre>
86 * </blockquote>
87 *
88 * <p>
89 * <code>CollationElementIterator.next</code> returns the collation order
90 * of the next character. A collation order consists of primary order,
91 * secondary order and tertiary order. The data type of the collation
92 * order is <strong>int</strong>. The first 16 bits of a collation order
93 * is its primary order; the next 8 bits is the secondary order and the
94 * last 8 bits is the tertiary order.
95 *
96 * <p><b>Note:</b> <code>CollationElementIterator</code> is a part of
97 * <code>RuleBasedCollator</code> implementation. It is only usable
98 * with <code>RuleBasedCollator</code> instances.
99 *
100 * @see                Collator
101 * @see                RuleBasedCollator
102 * @author             Helena Shih, Laura Werner, Richard Gillam
103 */
104public final class CollationElementIterator {
105    /**
106     * Null order which indicates the end of string is reached by the
107     * cursor.
108     */
109    public final static int NULLORDER = android.icu.text.CollationElementIterator.NULLORDER;
110
111    private android.icu.text.CollationElementIterator icuIterator;
112
113    CollationElementIterator(android.icu.text.CollationElementIterator iterator) {
114        icuIterator = iterator;
115    }
116
117    /**
118     * Resets the cursor to the beginning of the string.  The next call
119     * to next() will return the first collation element in the string.
120     */
121    public void reset() {
122        icuIterator.reset();
123    }
124
125    /**
126     * Get the next collation element in the string.  <p>This iterator iterates
127     * over a sequence of collation elements that were built from the string.
128     * Because there isn't necessarily a one-to-one mapping from characters to
129     * collation elements, this doesn't mean the same thing as "return the
130     * collation element [or ordering priority] of the next character in the
131     * string".</p>
132     * <p>This function returns the collation element that the iterator is currently
133     * pointing to and then updates the internal pointer to point to the next element.
134     * previous() updates the pointer first and then returns the element.  This
135     * means that when you change direction while iterating (i.e., call next() and
136     * then call previous(), or call previous() and then call next()), you'll get
137     * back the same element twice.</p>
138     */
139    public int next() {
140        return icuIterator.next();
141    }
142
143    /**
144     * Get the previous collation element in the string.  <p>This iterator iterates
145     * over a sequence of collation elements that were built from the string.
146     * Because there isn't necessarily a one-to-one mapping from characters to
147     * collation elements, this doesn't mean the same thing as "return the
148     * collation element [or ordering priority] of the previous character in the
149     * string".</p>
150     * <p>This function updates the iterator's internal pointer to point to the
151     * collation element preceding the one it's currently pointing to and then
152     * returns that element, while next() returns the current element and then
153     * updates the pointer.  This means that when you change direction while
154     * iterating (i.e., call next() and then call previous(), or call previous()
155     * and then call next()), you'll get back the same element twice.</p>
156     *
157     * @since 1.2
158     */
159    public int previous() {
160        return icuIterator.previous();
161    }
162
163    /**
164     * Return the primary component of a collation element.
165     *
166     * @param order the collation element
167     * @return the element's primary component
168     */
169    public final static int primaryOrder(int order) {
170        return android.icu.text.CollationElementIterator.primaryOrder(order);
171    }
172
173    /**
174     * Return the secondary component of a collation element.
175     *
176     * @param order the collation element
177     * @return the element's secondary component
178     */
179    public final static short secondaryOrder(int order) {
180        return (short) android.icu.text.CollationElementIterator.secondaryOrder(order);
181    }
182
183    /**
184     * Return the tertiary component of a collation element.
185     *
186     * @param order the collation element
187     * @return the element's tertiary component
188     */
189    public final static short tertiaryOrder(int order) {
190        return (short) android.icu.text.CollationElementIterator.tertiaryOrder(order);
191    }
192
193    /**
194     * Returns the character offset in the original text corresponding to the next
195     * collation element.  (That is, getOffset() returns the position in the text
196     * corresponding to the collation element that will be returned by the next
197     * call to next().)  This value will always be the index of the FIRST character
198     * corresponding to the collation element (a contracting character sequence is
199     * when two or more characters all correspond to the same collation element).
200     * This means if you do setOffset(x) followed immediately by getOffset(), getOffset()
201     * won't necessarily return x.
202     *
203     * @return The character offset in the original text corresponding to the collation
204     * element that will be returned by the next call to next().
205     * @since 1.2
206     */
207    public int getOffset() {
208        return icuIterator.getOffset();
209    }
210
211    /**
212     * Sets the iterator to point to the collation element corresponding to
213     * the specified character (the parameter is a CHARACTER offset in the
214     * original string, not an offset into its corresponding sequence of
215     * collation elements).  The value returned by the next call to next()
216     * will be the collation element corresponding to the specified position
217     * in the text.  If that position is in the middle of a contracting
218     * character sequence, the result of the next call to next() is the
219     * collation element for that sequence.  This means that getOffset()
220     * is not guaranteed to return the same value as was passed to a preceding
221     * call to setOffset().
222     *
223     * @param newOffset The new character offset into the original text.
224     * @since 1.2
225     */
226    public void setOffset(int newOffset) {
227        icuIterator.setOffset(newOffset);
228    }
229
230    /**
231     * Return the maximum length of any expansion sequences that end
232     * with the specified comparison order.
233     *
234     * @param order a collation order returned by previous or next.
235     * @return the maximum length of any expansion sequences ending
236     * with the specified order.
237     * @since 1.2
238     */
239    public int getMaxExpansion(int order) {
240        return icuIterator.getMaxExpansion(order);
241    }
242
243    /**
244     * Set a new string over which to iterate.
245     *
246     * @param source the new source text
247     * @since 1.2
248     */
249    public void setText(String source) {
250        icuIterator.setText(source);
251    }
252
253    /**
254     * Set a new string over which to iterate.
255     *
256     * @param source the new source text.
257     * @since 1.2
258     */
259    public void setText(CharacterIterator source) {
260        icuIterator.setText(source);
261    }
262}
263