1 |
/* CollationElementIterator.java -- Walks through collation elements |
/* CollationElementIterator.java -- Walks through collation elements |
2 |
Copyright (C) 1998, 1999, 2002 Free Software Foundation, Inc. |
Copyright (C) 1998, 1999, 2001, 2002, 2003 Free Software Foundation |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
38 |
|
|
39 |
package java.text; |
package java.text; |
40 |
|
|
41 |
|
/* Written using "Java Class Libraries", 2nd edition, plus online |
42 |
|
* API docs for JDK 1.2 from http://www.javasoft.com. |
43 |
|
* Status: Believed complete and correct to JDK 1.1. |
44 |
|
*/ |
45 |
|
|
46 |
|
/** |
47 |
|
* This class walks through the character collation elements of a |
48 |
|
* <code>String</code> as defined by the collation rules in an instance of |
49 |
|
* <code>RuleBasedCollator</code>. There is no public constructor for |
50 |
|
* this class. An instance is created by calling the |
51 |
|
* <code>getCollationElementIterator</code> method on |
52 |
|
* <code>RuleBasedCollator</code>. |
53 |
|
* |
54 |
|
* @author Aaron M. Renn <arenn@urbanophile.com> |
55 |
|
* @author Tom Tromey <tromey@cygnus.com> |
56 |
|
*/ |
57 |
public final class CollationElementIterator |
public final class CollationElementIterator |
58 |
{ |
{ |
59 |
/** |
/** |
65 |
/** |
/** |
66 |
* This is the RuleBasedCollator this object was created from. |
* This is the RuleBasedCollator this object was created from. |
67 |
*/ |
*/ |
68 |
private RuleBasedCollator rbc; |
private RuleBasedCollator collator; |
69 |
|
|
70 |
/** |
/** |
71 |
* This is the String that is being iterated over. |
* This is the String that is being iterated over. |
72 |
*/ |
*/ |
73 |
private String str; |
private String text; |
74 |
|
|
75 |
/** |
/** |
76 |
* This is the index into the String where we are currently scanning. |
* This is the index into the String where we are currently scanning. |
77 |
*/ |
*/ |
78 |
private int pos; |
private int index; |
79 |
|
|
80 |
|
/** |
81 |
|
* This method initializes a new instance of <code>CollationElementIterator</code> |
82 |
|
* to iterate over the specified <code>String</code> using the rules in the |
83 |
|
* specified <code>RuleBasedCollator</code>. |
84 |
|
* |
85 |
|
* @param collator The <code>RuleBasedCollation</code> used for calculating collation values |
86 |
|
* @param text The <code>String</code> to iterate over. |
87 |
|
*/ |
88 |
|
CollationElementIterator (RuleBasedCollator collator, String text) |
89 |
|
{ |
90 |
|
this.collator = collator; |
91 |
|
this.text = text; |
92 |
|
} |
93 |
|
|
94 |
/** |
/** |
95 |
* This method returns the collation ordering value of the next character |
* This method returns the collation ordering value of the next character |
100 |
*/ |
*/ |
101 |
public int next () |
public int next () |
102 |
{ |
{ |
103 |
++pos; |
++index; |
104 |
if (pos >= str.length()) |
if (index >= text.length ()) |
105 |
return(NULLORDER); |
return NULLORDER; |
106 |
|
|
107 |
String s = str.charAt(pos) + ""; |
String s = text.charAt (index) + ""; |
108 |
return(rbc.getCollationElementValue(s)); |
return collator.getCollationElementValue (s); |
109 |
} |
} |
110 |
|
|
111 |
/** |
/** |
128 |
*/ |
*/ |
129 |
public void reset () |
public void reset () |
130 |
{ |
{ |
131 |
pos = 0; |
index = 0; |
132 |
} |
} |
133 |
|
|
134 |
/** |
/** |
160 |
} |
} |
161 |
|
|
162 |
/** |
/** |
|
* This method initializes a new instance of <code>CollationElementIterator</code> |
|
|
* to iterate over the specified <code>String</code> using the rules in the |
|
|
* specified <code>RuleBasedCollator</code>. |
|
|
* |
|
|
* @param rbc The <code>RuleBasedCollation</code> used for calculating collation values |
|
|
* @param str The <code>String</code> to iterate over. |
|
|
*/ |
|
|
CollationElementIterator(RuleBasedCollator rbc, String str) |
|
|
{ |
|
|
this.rbc = rbc; |
|
|
this.str = str; |
|
|
} |
|
|
|
|
|
/** |
|
163 |
* This method sets the <code>String</code> that it is iterating over |
* This method sets the <code>String</code> that it is iterating over |
164 |
* to the specified <code>String</code>. |
* to the specified <code>String</code>. |
165 |
* |
* |
166 |
* @param The new <code>String</code> to iterate over. |
* @param The new <code>String</code> to iterate over. |
167 |
*/ |
*/ |
168 |
public void setText(String str) |
public void setText (String text) |
169 |
{ |
{ |
170 |
this.str = str; |
this.text = text; |
171 |
pos = 0; |
index = 0; |
172 |
} |
} |
173 |
|
|
174 |
/** |
/** |
178 |
* |
* |
179 |
* @param ci The <code>CharacterIterator</code> containing the new <code>String</code> to iterate over. |
* @param ci The <code>CharacterIterator</code> containing the new <code>String</code> to iterate over. |
180 |
*/ |
*/ |
181 |
public void setText(CharacterIterator ci) |
public void setText (CharacterIterator ci) |
182 |
{ |
{ |
183 |
StringBuffer sb = new StringBuffer(""); |
StringBuffer sb = new StringBuffer (""); |
184 |
|
|
185 |
// For now assume we read from the beginning of the string. |
// For now assume we read from the beginning of the string. |
186 |
char c = ci.first(); |
char c = ci.first (); |
187 |
while (c != CharacterIterator.DONE) |
while (c != CharacterIterator.DONE) |
188 |
{ |
{ |
189 |
sb.append(c); |
sb.append (c); |
190 |
c = ci.next(); |
c = ci.next (); |
191 |
} |
} |
192 |
|
|
193 |
setText(sb.toString()); |
setText (sb.toString ()); |
194 |
} |
} |
195 |
|
|
196 |
/** |
/** |
199 |
* |
* |
200 |
* @return The iteration index position. |
* @return The iteration index position. |
201 |
*/ |
*/ |
202 |
public int getOffset() |
public int getOffset () |
203 |
{ |
{ |
204 |
return(pos); |
return index; |
205 |
} |
} |
206 |
|
|
207 |
/** |
/** |
214 |
* |
* |
215 |
* @exception IllegalArgumentException If the new offset is not valid. |
* @exception IllegalArgumentException If the new offset is not valid. |
216 |
*/ |
*/ |
217 |
public void setOffset(int offset) |
public void setOffset (int offset) |
218 |
{ |
{ |
219 |
if (offset < 0) |
if (offset < 0) |
220 |
throw new IllegalArgumentException("Negative offset: " + offset); |
throw new IllegalArgumentException ("Negative offset: " + offset); |
221 |
|
|
222 |
if ((str.length() > 0) && (offset > 0)) |
if ((text.length () > 0) && (offset > 0)) |
223 |
throw new IllegalArgumentException("Offset too large: " + offset); |
throw new IllegalArgumentException ("Offset too large: " + offset); |
224 |
else if (offset > (str.length() - 1)) |
else if (offset > (text.length () - 1)) |
225 |
throw new IllegalArgumentException("Offset too large: " + offset); |
throw new IllegalArgumentException ("Offset too large: " + offset); |
226 |
|
|
227 |
pos = offset; |
index = offset; |
228 |
} |
} |
229 |
|
|
230 |
/** |
/** |
235 |
* |
* |
236 |
* @param The maximum length of an expansion sequence. |
* @param The maximum length of an expansion sequence. |
237 |
*/ |
*/ |
238 |
public int getMaxExpansion(int value) |
public int getMaxExpansion (int value) |
239 |
{ |
{ |
240 |
//************ Implement me!!!!!!!!! |
//************ Implement me!!!!!!!!! |
241 |
return(5); |
return 5; |
242 |
} |
} |
243 |
|
|
244 |
/** |
/** |
248 |
* |
* |
249 |
* @return The collation ordering value. |
* @return The collation ordering value. |
250 |
*/ |
*/ |
251 |
public int previous() |
public int previous () |
252 |
{ |
{ |
253 |
--pos; |
--index; |
254 |
if (pos < 0) |
if (index < 0) |
255 |
return(NULLORDER); |
return NULLORDER; |
256 |
|
|
257 |
String s = str.charAt(pos) + ""; |
String s = text.charAt (index) + ""; |
258 |
return(rbc.getCollationElementValue(s)); |
return collator.getCollationElementValue (s); |
259 |
} |
} |
260 |
} |
} |