1. /*
  2. * @(#)StringCharacterIterator.java 1.17 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * @(#)StringCharacterIterator.java 1.17 01/11/29
  9. *
  10. * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  11. * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  12. *
  13. * Portions copyright (c) 1996-1998 Sun Microsystems, Inc.
  14. * All Rights Reserved.
  15. *
  16. * The original version of this source code and documentation
  17. * is copyrighted and owned by Taligent, Inc., a wholly-owned
  18. * subsidiary of IBM. These materials are provided under terms
  19. * of a License Agreement between Taligent and Sun. This technology
  20. * is protected by multiple US and International patents.
  21. *
  22. * This notice and attribution to Taligent may not be removed.
  23. * Taligent is a registered trademark of Taligent, Inc.
  24. *
  25. * Permission to use, copy, modify, and distribute this software
  26. * and its documentation for NON-COMMERCIAL purposes and without
  27. * fee is hereby granted provided that this copyright notice
  28. * appears in all copies. Please refer to the file "copyright.html"
  29. * for further important copyright and licensing information.
  30. *
  31. * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  32. * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  33. * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  34. * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  35. * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  36. * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  37. *
  38. */
  39. package java.text;
  40. /**
  41. * <code>StringCharacterIterator</code> implements the
  42. * <code>CharacterIterater</code> protocol for a <code>String</code>.
  43. * The <code>StringCharacterIterator</code> class iterates over the
  44. * entire <code>String</code>.
  45. *
  46. * @see CharacterIterator
  47. */
  48. public final class StringCharacterIterator implements CharacterIterator
  49. {
  50. private String text;
  51. private int begin;
  52. private int end;
  53. // invariant: begin <= pos <= end
  54. private int pos;
  55. /**
  56. * Constructs an iterator with an initial index of 0.
  57. */
  58. public StringCharacterIterator(String text)
  59. {
  60. this(text, 0);
  61. }
  62. /**
  63. * Constructs an iterator with the specified initial index.
  64. *
  65. * @param text The String to be iterated over
  66. * @param pos Initial iterator position
  67. */
  68. public StringCharacterIterator(String text, int pos)
  69. {
  70. this(text, 0, text.length(), pos);
  71. }
  72. /**
  73. * Constructs an iterator over the given range of the given string, with the
  74. * index set at the specified position.
  75. *
  76. * @param text The String to be iterated over
  77. * @param begin Index of the first character
  78. * @param end Index of the character following the last character
  79. * @param pos Initial iterator position
  80. */
  81. public StringCharacterIterator(String text, int begin, int end, int pos) {
  82. if (text == null)
  83. throw new NullPointerException();
  84. this.text = text;
  85. if (begin < 0 || begin > end || end > text.length())
  86. throw new IllegalArgumentException("Invalid substring range");
  87. if (pos < begin || pos > end)
  88. throw new IllegalArgumentException("Invalid position");
  89. this.begin = begin;
  90. this.end = end;
  91. this.pos = pos;
  92. }
  93. /**
  94. * Reset this iterator to point to a new string. This package-visible
  95. * method is used by other java.text classes that want to avoid allocating
  96. * new StringCharacterIterator objects every time their setText method
  97. * is called.
  98. *
  99. * @param text The String to be iterated over
  100. */
  101. public void setText(String text) {
  102. if (text == null)
  103. throw new NullPointerException();
  104. this.text = text;
  105. this.begin = 0;
  106. this.end = text.length();
  107. this.pos = 0;
  108. }
  109. /**
  110. * Implements CharacterIterator.first() for String.
  111. * @see CharacterIterator#first
  112. */
  113. public char first()
  114. {
  115. pos = begin;
  116. return current();
  117. }
  118. /**
  119. * Implements CharacterIterator.last() for String.
  120. * @see CharacterIterator#last
  121. */
  122. public char last()
  123. {
  124. if (end != begin) {
  125. pos = end - 1;
  126. } else {
  127. pos = end;
  128. }
  129. return current();
  130. }
  131. /**
  132. * Implements CharacterIterator.setIndex() for String.
  133. * @see CharacterIterator#setIndex
  134. */
  135. public char setIndex(int p)
  136. {
  137. if (p < begin || p > end)
  138. throw new IllegalArgumentException("Invalid index");
  139. pos = p;
  140. return current();
  141. }
  142. /**
  143. * Implements CharacterIterator.current() for String.
  144. * @see CharacterIterator#current
  145. */
  146. public char current()
  147. {
  148. if (pos >= begin && pos < end) {
  149. return text.charAt(pos);
  150. }
  151. else {
  152. return DONE;
  153. }
  154. }
  155. /**
  156. * Implements CharacterIterator.next() for String.
  157. * @see CharacterIterator#next
  158. */
  159. public char next()
  160. {
  161. if (pos < end - 1) {
  162. pos++;
  163. return text.charAt(pos);
  164. }
  165. else {
  166. pos = end;
  167. return DONE;
  168. }
  169. }
  170. /**
  171. * Implements CharacterIterator.previous() for String.
  172. * @see CharacterIterator#previous
  173. */
  174. public char previous()
  175. {
  176. if (pos > begin) {
  177. pos--;
  178. return text.charAt(pos);
  179. }
  180. else {
  181. return DONE;
  182. }
  183. }
  184. /**
  185. * Implements CharacterIterator.getBeginIndex() for String.
  186. * @see CharacterIterator#getBeginIndex
  187. */
  188. public int getBeginIndex()
  189. {
  190. return begin;
  191. }
  192. /**
  193. * Implements CharacterIterator.getEndIndex() for String.
  194. * @see CharacterIterator#getEndIndex
  195. */
  196. public int getEndIndex()
  197. {
  198. return end;
  199. }
  200. /**
  201. * Implements CharacterIterator.getIndex() for String.
  202. * @see CharacterIterator#getIndex
  203. */
  204. public int getIndex()
  205. {
  206. return pos;
  207. }
  208. /**
  209. * Compares the equality of two StringCharacterIterator objects.
  210. * @param obj the StringCharacterIterator object to be compared with.
  211. * @return true if the given obj is the same as this
  212. * StringCharacterIterator object; false otherwise.
  213. */
  214. public boolean equals(Object obj)
  215. {
  216. if (this == obj)
  217. return true;
  218. if (!(obj instanceof StringCharacterIterator))
  219. return false;
  220. StringCharacterIterator that = (StringCharacterIterator) obj;
  221. if (hashCode() != that.hashCode())
  222. return false;
  223. if (!text.equals(that.text))
  224. return false;
  225. if (pos != that.pos || begin != that.begin || end != that.end)
  226. return false;
  227. return true;
  228. }
  229. /**
  230. * Computes a hashcode for this iterator.
  231. * @return A hash code
  232. */
  233. public int hashCode()
  234. {
  235. return text.hashCode() ^ pos ^ begin ^ end;
  236. }
  237. /**
  238. * Creates a copy of this iterator.
  239. * @return A copy of this
  240. */
  241. public Object clone()
  242. {
  243. try {
  244. StringCharacterIterator other
  245. = (StringCharacterIterator) super.clone();
  246. return other;
  247. }
  248. catch (CloneNotSupportedException e) {
  249. throw new InternalError();
  250. }
  251. }
  252. }