38 |
|
|
39 |
package java.text; |
package java.text; |
40 |
|
|
|
/** |
|
|
* This class walks through the character collation elements of a |
|
|
* <code>String</code> as defined by the collation rules in an instance of |
|
|
* <code>RuleBasedCollator</code>. There is no public constructor for |
|
|
* this class. An instance is created by calling the |
|
|
* <code>getCollationElementIterator</code> method on |
|
|
* <code>RuleBasedCollator</code>. |
|
|
* |
|
|
* @author Aaron M. Renn (arenn@urbanophile.com) |
|
|
*/ |
|
41 |
public final class CollationElementIterator |
public final class CollationElementIterator |
42 |
{ |
{ |
43 |
|
/** |
44 |
/* |
* This is a constant value that is returned to indicate that the end of |
45 |
* Static Variables |
* the string was encountered. |
46 |
*/ |
*/ |
47 |
|
public static final int NULLORDER = -1; |
48 |
/** |
|
49 |
* This is a constant value that is returned to indicate that the end of |
/** |
50 |
* the string was encountered. |
* This is the RuleBasedCollator this object was created from. |
51 |
*/ |
*/ |
52 |
public static final int NULLORDER = -1; |
private RuleBasedCollator rbc; |
53 |
|
|
54 |
/*************************************************************************/ |
/** |
55 |
|
* This is the String that is being iterated over. |
56 |
/* |
*/ |
57 |
* Instance Variables |
private String str; |
58 |
*/ |
|
59 |
|
/** |
60 |
/** |
* This is the index into the String where we are currently scanning. |
61 |
* This is the RuleBasedCollator this object was created from. |
*/ |
62 |
*/ |
private int pos; |
63 |
private RuleBasedCollator rbc; |
|
64 |
|
/** |
65 |
/** |
* This method returns the collation ordering value of the next character |
66 |
* This is the String that is being iterated over. |
* in the string. This method will return <code>NULLORDER</code> if the |
67 |
*/ |
* end of the string was reached. |
68 |
private String str; |
* |
69 |
|
* @return The collation ordering value. |
70 |
/** |
*/ |
71 |
* This is the index into the String where we are currently scanning. |
public int next () |
72 |
*/ |
{ |
73 |
private int pos; |
++pos; |
74 |
|
if (pos >= str.length()) |
75 |
/*************************************************************************/ |
return(NULLORDER); |
76 |
|
|
77 |
/* |
String s = str.charAt(pos) + ""; |
78 |
* Static Methods |
return(rbc.getCollationElementValue(s)); |
79 |
*/ |
} |
80 |
|
|
81 |
/** |
/** |
82 |
* This method returns the primary order value for the given collation |
* This method returns the primary order value for the given collation |
83 |
* value. |
* value. |
84 |
* |
* |
85 |
* @param value The collation value returned from <code>next()</code> or <code>previous()</code>. |
* @param value The collation value returned from <code>next()</code> or <code>previous()</code>. |
86 |
* |
* |
87 |
* @return The primary order value of the specified collation value. This is the high 16 bits. |
* @return The primary order value of the specified collation value. This is the high 16 bits. |
88 |
*/ |
*/ |
89 |
public static final int primaryOrder (int order) |
public static final int primaryOrder (int order) |
90 |
{ |
{ |
91 |
// From the JDK 1.2 spec. |
// From the JDK 1.2 spec. |
92 |
return order >>> 16; |
return order >>> 16; |
93 |
} |
} |
94 |
|
|
95 |
/*************************************************************************/ |
/** |
96 |
|
* This method resets the internal position pointer to read from the |
97 |
/** |
* beginning of the <code>String again. |
98 |
* This method returns the secondary order value for the given collation |
*/ |
99 |
* value. |
public void reset () |
100 |
* |
{ |
101 |
* @param value The collation value returned from <code>next()</code> or <code>previous()</code>. |
pos = 0; |
102 |
* |
} |
103 |
* @return The secondary order value of the specified collation value. This is the bits 8-15. |
|
104 |
*/ |
/** |
105 |
public static final short secondaryOrder (int order) |
* This method returns the secondary order value for the given collation |
106 |
{ |
* value. |
107 |
// From the JDK 1.2 spec. |
* |
108 |
return (short) ((order >>> 8) & 255); |
* @param value The collation value returned from <code>next()</code> or <code>previous()</code>. |
109 |
} |
* |
110 |
|
* @return The secondary order value of the specified collation value. This is the bits 8-15. |
111 |
/*************************************************************************/ |
*/ |
112 |
|
public static final short secondaryOrder (int order) |
113 |
/** |
{ |
114 |
* This method returns the tertiary order value for the given collation |
// From the JDK 1.2 spec. |
115 |
* value. |
return (short) ((order >>> 8) & 255); |
116 |
* |
} |
117 |
* @param value The collation value returned from <code>next()</code> or <code>previous()</code>. |
|
118 |
* |
/** |
119 |
* @return The tertiary order value of the specified collation value. This is the low eight bits. |
* This method returns the tertiary order value for the given collation |
120 |
*/ |
* value. |
121 |
public static final short tertiaryOrder (int order) |
* |
122 |
{ |
* @param value The collation value returned from <code>next()</code> or <code>previous()</code>. |
123 |
// From the JDK 1.2 spec. |
* |
124 |
return (short) (order & 255); |
* @return The tertiary order value of the specified collation value. This is the low eight bits. |
125 |
} |
*/ |
126 |
|
public static final short tertiaryOrder (int order) |
127 |
/*************************************************************************/ |
{ |
128 |
|
// From the JDK 1.2 spec. |
129 |
/* |
return (short) (order & 255); |
130 |
* Constructors |
} |
131 |
*/ |
|
132 |
|
/** |
133 |
/** |
* This method initializes a new instance of <code>CollationElementIterator</code> |
134 |
* This method initializes a new instance of <code>CollationElementIterator</code> |
* to iterate over the specified <code>String</code> using the rules in the |
135 |
* to iterate over the specified <code>String</code> using the rules in the |
* specified <code>RuleBasedCollator</code>. |
136 |
* specified <code>RuleBasedCollator</code>. |
* |
137 |
* |
* @param rbc The <code>RuleBasedCollation</code> used for calculating collation values |
138 |
* @param rbc The <code>RuleBasedCollation</code> used for calculating collation values |
* @param str The <code>String</code> to iterate over. |
139 |
* @param str The <code>String</code> to iterate over. |
*/ |
140 |
*/ |
CollationElementIterator(RuleBasedCollator rbc, String str) |
141 |
CollationElementIterator(RuleBasedCollator rbc, String str) |
{ |
142 |
{ |
this.rbc = rbc; |
143 |
this.rbc = rbc; |
this.str = str; |
144 |
this.str = str; |
} |
145 |
} |
|
146 |
|
/** |
147 |
/*************************************************************************/ |
* This method sets the <code>String</code> that it is iterating over |
148 |
|
* to the specified <code>String</code>. |
149 |
/* |
* |
150 |
* Instance Methods |
* @param The new <code>String</code> to iterate over. |
151 |
*/ |
*/ |
152 |
|
public void setText(String str) |
153 |
/** |
{ |
154 |
* This method sets the <code>String</code> that it is iterating over |
this.str = str; |
155 |
* to the specified <code>String</code>. |
pos = 0; |
156 |
* |
} |
157 |
* @param The new <code>String</code> to iterate over. |
|
158 |
*/ |
/** |
159 |
public void |
* This method sets the <code>String</code> that it is iterating over |
160 |
setText(String str) |
* to the <code>String</code> represented by the specified |
161 |
{ |
* <code>CharacterIterator</code>. |
162 |
this.str = str; |
* |
163 |
pos = 0; |
* @param ci The <code>CharacterIterator</code> containing the new <code>String</code> to iterate over. |
164 |
} |
*/ |
165 |
|
public void setText(CharacterIterator ci) |
166 |
/*************************************************************************/ |
{ |
167 |
|
StringBuffer sb = new StringBuffer(""); |
168 |
/** |
|
169 |
* This method sets the <code>String</code> that it is iterating over |
// For now assume we read from the beginning of the string. |
170 |
* to the <code>String</code> represented by the specified |
char c = ci.first(); |
171 |
* <code>CharacterIterator</code>. |
while (c != CharacterIterator.DONE) |
172 |
* |
{ |
173 |
* @param ci The <code>CharacterIterator</code> containing the new <code>String</code> to iterate over. |
sb.append(c); |
174 |
*/ |
c = ci.next(); |
175 |
public void |
} |
176 |
setText(CharacterIterator ci) |
|
177 |
{ |
setText(sb.toString()); |
178 |
StringBuffer sb = new StringBuffer(""); |
} |
179 |
|
|
180 |
// For now assume we read from the beginning of the string. |
/** |
181 |
char c = ci.first(); |
* This method returns the current offset into the <code>String</code> |
182 |
while (c != CharacterIterator.DONE) |
* that is being iterated over. |
183 |
{ |
* |
184 |
sb.append(c); |
* @return The iteration index position. |
185 |
c = ci.next(); |
*/ |
186 |
} |
public int getOffset() |
187 |
|
{ |
188 |
setText(sb.toString()); |
return(pos); |
189 |
} |
} |
190 |
|
|
191 |
/*************************************************************************/ |
/** |
192 |
|
* This method sets the iteration index position into the current |
193 |
/** |
* <code>String</code> to the specified value. This value must not |
194 |
* This method returns the current offset into the <code>String</code> |
* be negative and must not be greater than the last index position |
195 |
* that is being iterated over. |
* in the <code>String</code>. |
196 |
* |
* |
197 |
* @return The iteration index position. |
* @param offset The new iteration index position. |
198 |
*/ |
* |
199 |
public int |
* @exception IllegalArgumentException If the new offset is not valid. |
200 |
getOffset() |
*/ |
201 |
{ |
public void setOffset(int offset) |
202 |
return(pos); |
{ |
203 |
} |
if (offset < 0) |
204 |
|
throw new IllegalArgumentException("Negative offset: " + offset); |
205 |
/*************************************************************************/ |
|
206 |
|
if ((str.length() > 0) && (offset > 0)) |
207 |
/** |
throw new IllegalArgumentException("Offset too large: " + offset); |
208 |
* This method sets the iteration index position into the current |
else if (offset > (str.length() - 1)) |
209 |
* <code>String</code> to the specified value. This value must not |
throw new IllegalArgumentException("Offset too large: " + offset); |
210 |
* be negative and must not be greater than the last index position |
|
211 |
* in the <code>String</code>. |
pos = offset; |
212 |
* |
} |
213 |
* @param offset The new iteration index position. |
|
214 |
* |
/** |
215 |
* @exception IllegalArgumentException If the new offset is not valid. |
* This method returns the maximum length of any expansion sequence that |
216 |
*/ |
* ends with the specified collation order value. (Whatever that means). |
217 |
public void |
* |
218 |
setOffset(int offset) |
* @param value The collation order value |
219 |
{ |
* |
220 |
if (offset < 0) |
* @param The maximum length of an expansion sequence. |
221 |
throw new IllegalArgumentException("Negative offset: " + offset); |
*/ |
222 |
|
public int getMaxExpansion(int value) |
223 |
if ((str.length() > 0) && (offset > 0)) |
{ |
224 |
throw new IllegalArgumentException("Offset too large: " + offset); |
//************ Implement me!!!!!!!!! |
225 |
else if (offset > (str.length() - 1)) |
return(5); |
226 |
throw new IllegalArgumentException("Offset too large: " + offset); |
} |
227 |
|
|
228 |
pos = offset; |
/** |
229 |
} |
* This method returns the collation ordering value of the previous character |
230 |
|
* in the string. This method will return <code>NULLORDER</code> if the |
231 |
/*************************************************************************/ |
* beginning of the string was reached. |
232 |
|
* |
233 |
/** |
* @return The collation ordering value. |
234 |
* This method returns the maximum length of any expansion sequence that |
*/ |
235 |
* ends with the specified collation order value. (Whatever that means). |
public int previous() |
236 |
* |
{ |
237 |
* @param value The collation order value |
--pos; |
238 |
* |
if (pos < 0) |
239 |
* @param The maximum length of an expansion sequence. |
return(NULLORDER); |
240 |
*/ |
|
241 |
public int |
String s = str.charAt(pos) + ""; |
242 |
getMaxExpansion(int value) |
return(rbc.getCollationElementValue(s)); |
243 |
{ |
} |
|
//************ Implement me!!!!!!!!! |
|
|
return(5); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* This method resets the internal position pointer to read from the |
|
|
* beginning of the <code>String again. |
|
|
*/ |
|
|
public void |
|
|
reset() |
|
|
{ |
|
|
pos = 0; |
|
244 |
} |
} |
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* This method returns the collation ordering value of the next character |
|
|
* in the string. This method will return <code>NULLORDER</code> if the |
|
|
* end of the string was reached. |
|
|
* |
|
|
* @return The collation ordering value. |
|
|
*/ |
|
|
public int |
|
|
next() |
|
|
{ |
|
|
++pos; |
|
|
if (pos >= str.length()) |
|
|
return(NULLORDER); |
|
|
|
|
|
String s = str.charAt(pos) + ""; |
|
|
return(rbc.getCollationElementValue(s)); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* This method returns the collation ordering value of the previous character |
|
|
* in the string. This method will return <code>NULLORDER</code> if the |
|
|
* beginning of the string was reached. |
|
|
* |
|
|
* @return The collation ordering value. |
|
|
*/ |
|
|
public int |
|
|
previous() |
|
|
{ |
|
|
--pos; |
|
|
if (pos < 0) |
|
|
return(NULLORDER); |
|
|
|
|
|
String s = str.charAt(pos) + ""; |
|
|
return(rbc.getCollationElementValue(s)); |
|
|
} |
|
|
|
|
|
} // class CollationElementIterator |
|
|
|
|