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