StringCharacterIterator.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* StringCharacterIterator.java -- Iterate over a character range in a string
  2. Copyright (C) 1998, 1999, 2001, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.text;
  32. /**
  33. * This class iterates over a range of characters in a <code>String</code>.
  34. * For a given range of text, a beginning and ending index,
  35. * as well as a current index are defined. These values can be queried
  36. * by the methods in this interface. Additionally, various methods allow
  37. * the index to be set.
  38. *
  39. * @author Aaron M. Renn (arenn@urbanophile.com)
  40. * @author Tom Tromey (tromey@cygnus.com)
  41. */
  42. public final class StringCharacterIterator implements CharacterIterator
  43. {
  44. /**
  45. * This is the string to iterate over
  46. */
  47. private String text;
  48. /**
  49. * This is the value of the start position of the text range.
  50. */
  51. private int begin;
  52. /**
  53. * This is the value of the ending position of the text range.
  54. */
  55. private int end;
  56. /**
  57. * This is the current value of the scan index.
  58. */
  59. private int index;
  60. /**
  61. * This method initializes a new instance of
  62. * <code>StringCharacterIterator</code> to iterate over the entire
  63. * text of the specified <code>String</code>. The initial index
  64. * value will be set to the first character in the string.
  65. *
  66. * @param text The <code>String</code> to iterate through (<code>null</code>
  67. * not permitted).
  68. *
  69. * @throws NullPointerException if <code>text</code> is <code>null</code>.
  70. */
  71. public StringCharacterIterator (String text)
  72. {
  73. this (text, 0, text.length (), 0);
  74. }
  75. /*************************************************************************/
  76. /**
  77. * This method initializes a new instance of
  78. * <code>StringCharacterIterator</code> to iterate over the entire
  79. * text of the specified <code>String</code>. The initial index
  80. * value will be set to the specified value.
  81. *
  82. * @param text The <code>String</code> to iterate through.
  83. * @param index The initial index position.
  84. */
  85. public StringCharacterIterator (String text, int index)
  86. {
  87. this (text, 0, text.length (), index);
  88. }
  89. /*************************************************************************/
  90. /**
  91. * This method initializes a new instance of
  92. * <code>StringCharacterIterator</code> that iterates over the text
  93. * in a subrange of the specified <code>String</code>. The
  94. * beginning and end of the range are specified by the caller, as is
  95. * the initial index position.
  96. *
  97. * @param text The <code>String</code> to iterate through.
  98. * @param begin The beginning position in the character range.
  99. * @param end The ending position in the character range.
  100. * @param index The initial index position.
  101. *
  102. * @throws IllegalArgumentException If any of the range values are
  103. * invalid.
  104. */
  105. public StringCharacterIterator (String text, int begin, int end, int index)
  106. {
  107. int len = text.length ();
  108. if ((begin < 0) || (begin > len))
  109. throw new IllegalArgumentException ("Bad begin position");
  110. if ((end < begin) || (end > len))
  111. throw new IllegalArgumentException ("Bad end position");
  112. if ((index < begin) || (index > end))
  113. throw new IllegalArgumentException ("Bad initial index position");
  114. this.text = text;
  115. this.begin = begin;
  116. this.end = end;
  117. this.index = index;
  118. }
  119. /**
  120. * This is a package level constructor that copies the text out of
  121. * an existing StringCharacterIterator and resets the beginning and
  122. * ending index.
  123. *
  124. * @param sci The StringCharacterIterator to copy the info from
  125. * @param begin The beginning index of the range we are interested in.
  126. * @param end The ending index of the range we are interested in.
  127. */
  128. StringCharacterIterator (StringCharacterIterator sci, int begin, int end)
  129. {
  130. this (sci.text, begin, end, begin);
  131. }
  132. /**
  133. * This method returns the character at the current index position
  134. *
  135. * @return The character at the current index position.
  136. */
  137. public char current ()
  138. {
  139. return (index < end) ? text.charAt (index) : DONE;
  140. }
  141. /*************************************************************************/
  142. /**
  143. * This method increments the current index and then returns the
  144. * character at the new index value. If the index is already at
  145. * <code>getEndIndex () - 1</code>, it will not be incremented.
  146. *
  147. * @return The character at the position of the incremented index
  148. * value, or <code>DONE</code> if the index has reached
  149. * getEndIndex () - 1.
  150. */
  151. public char next ()
  152. {
  153. if (index == end)
  154. return DONE;
  155. ++index;
  156. return current ();
  157. }
  158. /*************************************************************************/
  159. /**
  160. * This method decrements the current index and then returns the
  161. * character at the new index value. If the index value is already
  162. * at the beginning index, it will not be decremented.
  163. *
  164. * @return The character at the position of the decremented index
  165. * value, or <code>DONE</code> if index was already equal to the
  166. * beginning index value.
  167. */
  168. public char previous ()
  169. {
  170. if (index == begin)
  171. return DONE;
  172. --index;
  173. return current ();
  174. }
  175. /*************************************************************************/
  176. /**
  177. * This method sets the index value to the beginning of the range and returns
  178. * the character there.
  179. *
  180. * @return The character at the beginning of the range, or
  181. * <code>DONE</code> if the range is empty.
  182. */
  183. public char first ()
  184. {
  185. index = begin;
  186. return current ();
  187. }
  188. /*************************************************************************/
  189. /**
  190. * This method sets the index value to <code>getEndIndex () - 1</code> and
  191. * returns the character there. If the range is empty, then the index value
  192. * will be set equal to the beginning index.
  193. *
  194. * @return The character at the end of the range, or
  195. * <code>DONE</code> if the range is empty.
  196. */
  197. public char last ()
  198. {
  199. if (end == begin)
  200. return DONE;
  201. index = end - 1;
  202. return current ();
  203. }
  204. /*************************************************************************/
  205. /**
  206. * This method returns the current value of the index.
  207. *
  208. * @return The current index value
  209. */
  210. public int getIndex ()
  211. {
  212. return index;
  213. }
  214. /*************************************************************************/
  215. /**
  216. * This method sets the value of the index to the specified value, then
  217. * returns the character at that position.
  218. *
  219. * @param index The new index value.
  220. *
  221. * @return The character at the new index value or <code>DONE</code>
  222. * if the index value is equal to <code>getEndIndex</code>.
  223. *
  224. * @exception IllegalArgumentException If the specified index is not valid
  225. */
  226. public char setIndex (int index)
  227. {
  228. if ((index < begin) || (index > end))
  229. throw new IllegalArgumentException ("Bad index specified");
  230. this.index = index;
  231. return current ();
  232. }
  233. /*************************************************************************/
  234. /**
  235. * This method returns the character position of the first character in the
  236. * range.
  237. *
  238. * @return The index of the first character in the range.
  239. */
  240. public int getBeginIndex ()
  241. {
  242. return begin;
  243. }
  244. /*************************************************************************/
  245. /**
  246. * This method returns the character position of the end of the text range.
  247. * This will actually be the index of the first character following the
  248. * end of the range. In the event the text range is empty, this will be
  249. * equal to the first character in the range.
  250. *
  251. * @return The index of the end of the range.
  252. */
  253. public int getEndIndex ()
  254. {
  255. return end;
  256. }
  257. /*************************************************************************/
  258. /**
  259. * This method creates a copy of this <code>CharacterIterator</code>.
  260. *
  261. * @return A copy of this <code>CharacterIterator</code>.
  262. */
  263. public Object clone ()
  264. {
  265. return new StringCharacterIterator (text, begin, end, index);
  266. }
  267. /*************************************************************************/
  268. /**
  269. * This method tests this object for equality againt the specified
  270. * object. This will be true if and only if the specified object:
  271. * <p>
  272. * <ul>
  273. * <li>is not <code>null</code>.</li>
  274. * <li>is an instance of <code>StringCharacterIterator</code></li>
  275. * <li>has the same text as this object</li>
  276. * <li>has the same beginning, ending, and current index as this object.</li>
  277. * </ul>
  278. *
  279. * @param obj The object to test for equality against.
  280. *
  281. * @return <code>true</code> if the specified object is equal to this
  282. * object, <code>false</code> otherwise.
  283. */
  284. public boolean equals (Object obj)
  285. {
  286. if (! (obj instanceof StringCharacterIterator))
  287. return false;
  288. StringCharacterIterator sci = (StringCharacterIterator) obj;
  289. return (begin == sci.begin
  290. && end == sci.end
  291. && index == sci.index
  292. && text.equals (sci.text));
  293. }
  294. /**
  295. * Return the hash code for this object.
  296. * @return the hash code
  297. */
  298. public int hashCode()
  299. {
  300. // Incorporate all the data in a goofy way.
  301. return begin ^ end ^ index ^ text.hashCode();
  302. }
  303. /*************************************************************************/
  304. /**
  305. * This method allows other classes in java.text to change the value
  306. * of the underlying text being iterated through.
  307. *
  308. * @param text The new <code>String</code> to iterate through.
  309. */
  310. public void setText (String text)
  311. {
  312. this.text = text;
  313. this.begin = 0;
  314. this.end = text.length ();
  315. this.index = 0;
  316. }
  317. }