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